Fix missing threads after introducing pagination

This commit is contained in:
dtonon 2026-05-15 22:34:35 +01:00
parent d4982dff0c
commit fdab17c8e2

View file

@ -119,14 +119,21 @@ async function fetchActivitySlice(
for (const e of events) { for (const e of events) {
oldest = Math.min(oldest, e.created_at); oldest = Math.min(oldest, e.created_at);
const id = threadIdOf(e); const id = threadIdOf(e);
if (!id || exclude.has(id) || collected.has(id)) continue; if (!id || exclude.has(id)) continue;
const seen = collected.get(id);
if (seen) {
// Grab the OP from the stream when a thread was first seen via a reply,
// so we avoid the by-id backfill the group relay truncates
if (!seen.op && e.kind === 11) seen.op = e;
continue;
}
if (collected.size >= n) continue; // Page full; keep scanning for OPs
collected.set(id, { collected.set(id, {
id, id,
latestAt: e.created_at, latestAt: e.created_at,
latestPubkey: e.pubkey, latestPubkey: e.pubkey,
op: e.kind === 11 ? e : undefined, op: e.kind === 11 ? e : undefined,
}); });
if (collected.size >= n) break;
} }
if (events.length < WALK_LIMIT) { if (events.length < WALK_LIMIT) {
@ -220,15 +227,17 @@ async function buildThreads(
groupId: string, groupId: string,
items: SliceItem[], items: SliceItem[],
): Promise<ThreadData[]> { ): Promise<ThreadData[]> {
// Backfill OPs for threads first seen via a reply // Backfill OPs that fell outside the activity window. The group relay
const missing = items.filter((it) => !it.op).map((it) => it.id); // truncates multi-id queries, so fetch each one on its own.
const missing = items.filter((it) => !it.op);
if (missing.length > 0) { if (missing.length > 0) {
const ops = await querySync(relay, { const fetched = await Promise.all(
kinds: [11], missing.map((it) =>
"#h": [groupId], querySync(relay, { kinds: [11], "#h": [groupId], ids: [it.id] }),
ids: missing, ),
}); );
const byId = new Map(ops.map((e) => [e.id, e])); const byId = new Map<string, Event>();
for (const evs of fetched) for (const e of evs) byId.set(e.id, e);
for (const it of items) if (!it.op) it.op = byId.get(it.id); for (const it of items) if (!it.op) it.op = byId.get(it.id);
} }