Spaces:
Sleeping
Sleeping
Download src/lib/settings.ts from iq7se2/tg-tracker: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/spaces/iq7se2/tg-tracker/resolve/main/src/lib/settings.ts
- Command line
-
hf download hf://spaces/iq7se2/tg-tracker/src/lib/settings.ts
-
curl -L -o settings.ts https://huggingface.co/spaces/iq7se2/tg-tracker/resolve/main/src/lib/settings.ts
2.98 kB
| /** | |
| * Settings store — simplified. Only global basics live here. | |
| * Recipients/channels are now "Monitoring Pages" (stored in their own table). | |
| */ | |
| import { db } from "@/lib/db"; | |
| export type SaleType = "auction" | "sale"; | |
| export interface AppSettings { | |
| botToken: string; | |
| // Monitoring (global) | |
| intervalSeconds: number; | |
| realtimeMode: boolean; | |
| minBidTon: number; // global min bid (pages can override) | |
| maxBidTon: number; // global max bid to fetch (0 = unlimited) | |
| // Data sources | |
| sourceFragment: boolean; | |
| sourceGetgems: boolean; | |
| sourceMock: boolean; | |
| fetchSaleType: "auction" | "sale" | "both"; | |
| // Global default message template (pages can override) | |
| messageTemplate: string; | |
| } | |
| export const DEFAULT_SETTINGS: AppSettings = { | |
| botToken: "", | |
| intervalSeconds: 60, | |
| realtimeMode: false, | |
| minBidTon: 0, | |
| maxBidTon: 0, | |
| sourceFragment: true, | |
| sourceGetgems: true, | |
| sourceMock: true, | |
| fetchSaleType: "auction", | |
| messageTemplate: | |
| "🔨 <b>مزاد جديد على المعرف</b>\n\n" + | |
| "👤 المعرف: <code>@{username}</code>\n" + | |
| "💰 تم وضع مزاد بقيمة: <b>{bid} TON</b>\n" + | |
| "⏳ ينتهي خلال: {timeLeft}\n" + | |
| "🔗 {url}", | |
| }; | |
| const SETTINGS_ID = "app"; | |
| export async function getSettings(): Promise<AppSettings> { | |
| const row = await db.setting.findUnique({ where: { id: SETTINGS_ID } }); | |
| if (!row) { | |
| await db.setting.create({ data: { id: SETTINGS_ID, value: JSON.stringify(DEFAULT_SETTINGS) } }); | |
| return { ...DEFAULT_SETTINGS }; | |
| } | |
| try { | |
| const parsed = JSON.parse(row.value) as Partial<AppSettings>; | |
| const merged: AppSettings = { ...DEFAULT_SETTINGS, ...parsed }; | |
| // Migration: remove deprecated fields | |
| return merged; | |
| } catch { | |
| return { ...DEFAULT_SETTINGS }; | |
| } | |
| } | |
| export async function saveSettings(next: Partial<AppSettings>): Promise<AppSettings> { | |
| const current = await getSettings(); | |
| const clean: Partial<AppSettings> = {}; | |
| for (const [k, v] of Object.entries(next)) { | |
| if (v !== undefined) (clean as Record<string, unknown>)[k] = v; | |
| } | |
| const merged: AppSettings = { ...current, ...clean }; | |
| await db.setting.upsert({ | |
| where: { id: SETTINGS_ID }, | |
| create: { id: SETTINGS_ID, value: JSON.stringify(merged) }, | |
| update: { value: JSON.stringify(merged) }, | |
| }); | |
| return merged; | |
| } | |
| export function formatMessage( | |
| template: string, | |
| data: { | |
| username: string; | |
| bid: number; | |
| source: string; | |
| pattern?: string; | |
| url?: string; | |
| timeLeft?: string; | |
| saleType?: string; | |
| } | |
| ): string { | |
| const saleLabel = data.saleType === "sale" ? "بيع مباشر" : "مزاد"; | |
| return (template || "") | |
| .replace(/\{username\}/g, data.username) | |
| .replace(/\{bid\}/g, String(data.bid)) | |
| .replace(/\{source\}/g, data.source) | |
| .replace(/\{pattern\}/g, data.pattern || "—") | |
| .replace(/\{url\}/g, data.url || "—") | |
| .replace(/\{timeLeft\}/g, data.timeLeft || "—") | |
| .replace(/\{saleType\}/g, saleLabel); | |
| } | |