-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfeed.ts
More file actions
464 lines (422 loc) · 14.1 KB
/
Copy pathfeed.ts
File metadata and controls
464 lines (422 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/**
* Feed catalogue → per-user rows: idempotent sync for `feed_default_messages` deliveries.
*/
import {
FEED_CATALOG_FALLBACK_LOCALE,
type FeedCatalogLocale,
resolveFeedCatalogLocaleFromLanguageTag,
} from "../locales/resolveFeedCatalogLocale.js";
import { sql } from "./start.js";
import { normalizeUsername } from "./users.js";
export { resolveFeedCatalogLocaleFromLanguageTag as normalizeFeedLocale };
export type { FeedCatalogLocale };
/** Normalize DB timestamptz/string/Date for JSON (ISO 8601 UTC) so the client can parse local display. */
function feedRowSentAtIso(raw: unknown): string | null {
if (raw == null) return null;
if (typeof raw === "number" && Number.isFinite(raw)) {
const ms = raw < 12_000_000_000 ? raw * 1000 : raw;
const d = new Date(ms);
return Number.isNaN(d.getTime()) ? null : d.toISOString();
}
if (typeof raw === "string") {
const t = raw.trim();
if (!t) return null;
const asDate = new Date(t.includes("T") ? t : t.replace(" ", "T"));
return Number.isNaN(asDate.getTime()) ? null : asDate.toISOString();
}
if (raw instanceof Date) {
const t = raw.getTime();
return Number.isNaN(t) ? null : raw.toISOString();
}
try {
const s = String(raw).trim();
if (!s) return null;
const d = new Date(s.includes("T") ? s : s.replace(" ", "T"));
return Number.isNaN(d.getTime()) ? null : d.toISOString();
} catch {
return null;
}
}
function feedRowSentAtForClient(row: Record<string, unknown>): string | null {
return feedRowSentAtIso(row.sent_at) ?? feedRowSentAtIso(row.created_at);
}
function parseDefaultMessageId(raw: unknown): bigint | null {
if (raw == null) return null;
if (typeof raw === "bigint") return raw;
if (typeof raw === "number" && Number.isFinite(raw)) {
try {
return BigInt(Math.trunc(raw));
} catch {
return null;
}
}
if (typeof raw === "string") {
const t = raw.trim();
if (!/^-?\d+$/.test(t)) return null;
try {
return BigInt(t);
} catch {
return null;
}
}
return null;
}
export type DeliverWelcomeFeedSummary = {
telegramUsernameNorm: string;
locale: string;
explicitCatalogSize: number;
extraCatalogSize: number;
/** How many welcome demo keys matched a catalogue row for this locale. */
welcomeRowsResolved: number;
};
export const FEED_WELCOME_DEFAULT_KEYS_EN = [
"demo_wallet_created",
"demo_creator_likely",
"demo_nft_received",
"demo_token_granted",
"demo_incoming_task",
] as const;
/** Stable prefix on `feed_items.source_id` for catalogue-driven inserts. */
export const FEED_WELCOME_BUNDLE_MARKER = "welcome_bundle_demo_v1";
type TemplateBody = {
card_type?: string;
layout_variant?: string | null;
payload?: Record<string, unknown>;
};
type CatalogRow = { id: bigint | number | string; key: string; body: unknown };
function parseTemplateBody(bodyRaw: unknown): TemplateBody | null {
let bodyRawMut = bodyRaw;
if (typeof bodyRawMut === "string") {
try {
bodyRawMut = JSON.parse(bodyRawMut) as TemplateBody;
} catch {
return null;
}
}
if (!bodyRawMut || typeof bodyRawMut !== "object") return null;
return bodyRawMut as TemplateBody;
}
async function mergeExplicitCatalogKeys(locale: FeedCatalogLocale): Promise<Map<string, CatalogRow>> {
type CatalogRowTyped = CatalogRow & { body: TemplateBody };
const merged = new Map<string, CatalogRowTyped>();
const enRowsUnknown = await sql`
SELECT id, key, body
FROM feed_default_messages
WHERE locale = ${"en"}
AND key IN (
${FEED_WELCOME_DEFAULT_KEYS_EN[0]},
${FEED_WELCOME_DEFAULT_KEYS_EN[1]},
${FEED_WELCOME_DEFAULT_KEYS_EN[2]},
${FEED_WELCOME_DEFAULT_KEYS_EN[3]},
${FEED_WELCOME_DEFAULT_KEYS_EN[4]}
);
`;
for (const r of enRowsUnknown as CatalogRowTyped[]) merged.set(r.key, r);
if (locale !== "en") {
const localeRowsUnknown = await sql`
SELECT id, key, body
FROM feed_default_messages
WHERE locale = ${locale}
AND key IN (
${FEED_WELCOME_DEFAULT_KEYS_EN[0]},
${FEED_WELCOME_DEFAULT_KEYS_EN[1]},
${FEED_WELCOME_DEFAULT_KEYS_EN[2]},
${FEED_WELCOME_DEFAULT_KEYS_EN[3]},
${FEED_WELCOME_DEFAULT_KEYS_EN[4]}
);
`;
for (const r of localeRowsUnknown as CatalogRowTyped[]) merged.set(r.key, r);
}
return merged;
}
/** Other `kind = feed_default` catalogue rows (new keys) not in {@link FEED_WELCOME_DEFAULT_KEYS_EN}. */
async function mergeExtraCatalogRows(locale: FeedCatalogLocale): Promise<Map<string, CatalogRow>> {
const merged = new Map<string, CatalogRow>();
const enExtras = await sql`
SELECT id, key, body
FROM feed_default_messages
WHERE kind = ${"feed_default"}
AND locale = ${"en"}
AND key NOT IN (
${FEED_WELCOME_DEFAULT_KEYS_EN[0]},
${FEED_WELCOME_DEFAULT_KEYS_EN[1]},
${FEED_WELCOME_DEFAULT_KEYS_EN[2]},
${FEED_WELCOME_DEFAULT_KEYS_EN[3]},
${FEED_WELCOME_DEFAULT_KEYS_EN[4]}
);
`;
for (const r of enExtras as CatalogRow[]) merged.set(r.key, r);
if (locale !== "en") {
const localeExtras = await sql`
SELECT id, key, body
FROM feed_default_messages
WHERE kind = ${"feed_default"}
AND locale = ${locale}
AND key NOT IN (
${FEED_WELCOME_DEFAULT_KEYS_EN[0]},
${FEED_WELCOME_DEFAULT_KEYS_EN[1]},
${FEED_WELCOME_DEFAULT_KEYS_EN[2]},
${FEED_WELCOME_DEFAULT_KEYS_EN[3]},
${FEED_WELCOME_DEFAULT_KEYS_EN[4]}
);
`;
for (const r of localeExtras as CatalogRow[]) merged.set(r.key, r);
}
return merged;
}
async function insertCatalogFeedItem(opts: {
telegramUsername: string;
row: CatalogRow;
key: string;
welcomeOrder?: number;
}): Promise<void> {
const telegramUsername = normalizeUsername(opts.telegramUsername);
if (!telegramUsername) return;
const { row, key } = opts;
const defaultId = parseDefaultMessageId(row.id);
if (defaultId == null) return;
const bodyRaw = parseTemplateBody(row.body);
if (!bodyRaw) return;
const cardType =
typeof bodyRaw.card_type === "string" && bodyRaw.card_type.trim() !== ""
? bodyRaw.card_type
: "system_action";
const layoutVariant =
typeof bodyRaw.layout_variant === "string" ? bodyRaw.layout_variant : null;
const rawPayload =
bodyRaw.payload && typeof bodyRaw.payload === "object" ? bodyRaw.payload : {};
const payload: Record<string, unknown> = { ...rawPayload, catalog_key: key };
if (opts.welcomeOrder != null) {
payload.welcome_order = opts.welcomeOrder;
}
const seq = opts.welcomeOrder ?? defaultId;
await sql`
INSERT INTO feed_items (
telegram_username,
sent_at,
source_type,
card_type,
layout_variant,
default_message_id,
source_id,
payload
)
VALUES (
${telegramUsername},
NOW(),
${"welcome_bundle"},
${cardType},
${layoutVariant},
${defaultId},
${`${FEED_WELCOME_BUNDLE_MARKER}:${key}:${seq}`},
${JSON.stringify(payload)}::jsonb
)
ON CONFLICT (telegram_username, default_message_id)
WHERE default_message_id IS NOT NULL
DO NOTHING;
`;
}
/**
* Inserts **`feed_items`** for every **`feed_default_messages`** row (`kind = feed_default`)
* this user has not yet received (matched by **`default_message_id`**). Safe to call on every `/api/feed`.
*/
export async function deliverWelcomeFeedIfNeeded(opts: {
telegramUsername: string;
localePreferred?: string | null;
}): Promise<DeliverWelcomeFeedSummary | null> {
const telegramUsername = normalizeUsername(opts.telegramUsername);
if (!telegramUsername) return null;
const locale = resolveFeedCatalogLocaleFromLanguageTag(opts.localePreferred ?? null);
const explicitMap = await mergeExplicitCatalogKeys(locale);
const extraMap = await mergeExtraCatalogRows(locale);
if (explicitMap.size === 0 && extraMap.size === 0) {
return {
telegramUsernameNorm: telegramUsername,
locale,
explicitCatalogSize: 0,
extraCatalogSize: 0,
welcomeRowsResolved: 0,
};
}
let sortIndex = 0;
for (const key of FEED_WELCOME_DEFAULT_KEYS_EN) {
const row = explicitMap.get(key);
if (!row) continue;
sortIndex += 1;
await insertCatalogFeedItem({
telegramUsername,
row,
key,
welcomeOrder: sortIndex,
});
}
const extraKeys = [...extraMap.keys()].sort((a, b) => a.localeCompare(b));
for (const key of extraKeys) {
const row = extraMap.get(key);
if (!row) continue;
await insertCatalogFeedItem({ telegramUsername, row, key });
}
const welcomeRowsResolved = FEED_WELCOME_DEFAULT_KEYS_EN.filter((k) => explicitMap.has(k)).length;
return {
telegramUsernameNorm: telegramUsername,
locale,
explicitCatalogSize: explicitMap.size,
extraCatalogSize: extraMap.size,
welcomeRowsResolved,
};
}
const WELCOME_TEXT_PAYLOAD_KEYS = ["title", "subtitle", "trailing_label"] as const;
function mergeCatalogTextIntoPayload(
stored: Record<string, unknown>,
catalogPayload: Record<string, unknown>,
): Record<string, unknown> {
const next = { ...stored };
for (const k of WELCOME_TEXT_PAYLOAD_KEYS) {
const v = catalogPayload[k];
if (typeof v === "string" && v.trim()) {
next[k] = v;
}
}
return next;
}
/** Load catalogue payloads for keys: requested locale overrides English per key; missing locale falls back to `en`. */
export async function fetchCatalogPayloadMapForLocale(
keys: string[],
displayLocale: FeedCatalogLocale,
): Promise<Map<string, Record<string, unknown>>> {
const wanted = new Set(keys.filter((k) => k.trim().length > 0));
const out = new Map<string, Record<string, unknown>>();
if (wanted.size === 0) return out;
const explicit = await mergeExplicitCatalogKeys(displayLocale);
const extra = await mergeExtraCatalogRows(displayLocale);
const ingest = (map: Map<string, CatalogRow>) => {
for (const [key, row] of map) {
if (!wanted.has(key)) continue;
const body = parseTemplateBody(row.body);
const p =
body?.payload && typeof body.payload === "object" && !Array.isArray(body.payload)
? (body.payload as Record<string, unknown>)
: {};
out.set(key, p);
}
};
ingest(explicit);
ingest(extra);
return out;
}
function catalogKeyFromSourceId(sourceId: unknown): string | null {
if (typeof sourceId !== "string" || !sourceId.startsWith(`${FEED_WELCOME_BUNDLE_MARKER}:`)) {
return null;
}
const parts = sourceId.split(":");
if (parts.length < 3) return null;
const key = parts[1]?.trim();
return key || null;
}
export async function listFeedItemsForUser(
telegramUsername: string,
limit = 80,
displayLocale: FeedCatalogLocale = FEED_CATALOG_FALLBACK_LOCALE,
): Promise<
Array<{
id: number;
sent_at: string | null;
source_type: string;
card_type: string;
layout_variant: string | null;
payload: unknown;
read_at: string | null;
}>
> {
const u = normalizeUsername(telegramUsername);
if (!u) {
return [];
}
const rows = await sql`
SELECT id, sent_at, card_type, layout_variant, payload, read_at, source_type, default_message_id, source_id
FROM feed_items
WHERE lower(btrim(telegram_username)) = ${u}
ORDER BY
CASE
WHEN trim(COALESCE(payload ->> 'welcome_order', '')) ~ '^[0-9]+$'
THEN trim(payload ->> 'welcome_order')::bigint
ELSE 1000000000000::bigint
END ASC,
sent_at ASC NULLS LAST,
id ASC
LIMIT ${Number(limit)};
`;
const rawRows = rows as Array<Record<string, unknown>>;
const catalogKeys: string[] = [];
for (const r of rawRows) {
const stored =
r.payload && typeof r.payload === "object" && !Array.isArray(r.payload)
? (r.payload as Record<string, unknown>)
: {};
const fromPayload =
typeof stored.catalog_key === "string" && stored.catalog_key.trim()
? stored.catalog_key.trim()
: null;
if (fromPayload) {
catalogKeys.push(fromPayload);
continue;
}
const fromSource = catalogKeyFromSourceId(r.source_id);
if (fromSource) catalogKeys.push(fromSource);
}
const catalogByKey = await fetchCatalogPayloadMapForLocale(catalogKeys, displayLocale);
return rawRows.map((r) => {
const stored =
r.payload && typeof r.payload === "object" && !Array.isArray(r.payload)
? (r.payload as Record<string, unknown>)
: {};
let catalogKey =
typeof stored.catalog_key === "string" && stored.catalog_key.trim()
? stored.catalog_key.trim()
: null;
if (!catalogKey) {
catalogKey = catalogKeyFromSourceId(r.source_id);
}
const sourceType = String(r.source_type);
const isWelcomeCatalog =
sourceType === "welcome_bundle" || catalogKey != null || r.default_message_id != null;
let payload: unknown = r.payload;
if (isWelcomeCatalog && catalogKey) {
const catalogPayload = catalogByKey.get(catalogKey);
if (catalogPayload) {
payload = mergeCatalogTextIntoPayload(stored, catalogPayload);
}
}
return {
id: Number(r.id),
sent_at: feedRowSentAtForClient(r),
source_type: sourceType,
card_type: String(r.card_type),
layout_variant: r.layout_variant == null ? null : String(r.layout_variant),
payload,
read_at: feedRowSentAtIso(r.read_at),
};
});
}
export type FeedItemForClient = Awaited<ReturnType<typeof listFeedItemsForUser>>[number];
/** Deliver missing welcome catalogue rows, then list feed items (no schema migrations). */
export async function bootstrapAuthenticatedFeedItems(opts: {
telegramUsername: string;
catalogLocale?: FeedCatalogLocale;
localePreferred?: string | null;
limit?: number;
}): Promise<FeedItemForClient[]> {
const catalogLocale = opts.catalogLocale ?? FEED_CATALOG_FALLBACK_LOCALE;
const deliverLocalePreferred =
opts.localePreferred ??
(catalogLocale !== FEED_CATALOG_FALLBACK_LOCALE ? catalogLocale : null);
try {
await deliverWelcomeFeedIfNeeded({
telegramUsername: opts.telegramUsername,
localePreferred: deliverLocalePreferred,
});
} catch {
// Best effort — still return whatever rows exist.
}
return listFeedItemsForUser(opts.telegramUsername, opts.limit ?? 80, catalogLocale);
}