diff options
Diffstat (limited to 'src/features')
| -rw-r--r-- | src/features/messages.js | 85 |
1 files changed, 71 insertions, 14 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index 91fc22d..e36750e 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -15,7 +15,7 @@ import { mediaServers } from '../media-servers.js'; // listens for both identities and decrypts with whichever keys are present. import { - subscribeOn, subscribeAuthed, publishOn, queryOn, queryManyOn, queryStreamOn, fetchInboxRelays, relayAlive, liveRelayList, readRelayList, resetRelay, wakeRelays, + subscribeOn, subscribeAuthed, publishOn, queryOn, queryManyOn, queryStreamOn, queryDepthOn, fetchInboxRelays, relayAlive, liveRelayList, readRelayList, resetRelay, wakeRelays, npubOf, neventOf, parseNostrPubkey, parseNostrRef, generateSecretKey, getPublicKey, finalizeEvent, nip44, PROFILE_RELAYS, openWrapsOffthread, unwrapDMsOffthread, verifyEventsAsync, setSyncConfig, DEFAULT_SYNC_RELAYS, } from '../nostr.js'; @@ -5361,7 +5361,8 @@ export function messagesFeature(ctx) { const batch = [...incoming.values()]; incoming.clear(); chain = chain.then(async () => { const byId = new Map(c.notes.map((e) => [e.id, e])); - const fresh = batch.filter((e) => !byId.has(e.id)); + // older than the page already settled: kept back until paging gets there + const fresh = batch.filter((e) => !byId.has(e.id) && !holdBack(c, e)); if (!fresh.length) return; for (const e of fresh) byId.set(e.id, e); const merged = newestArticles([...byId.values()]).sort((a, b) => b.created_at - a.created_at); @@ -5384,16 +5385,19 @@ export function messagesFeature(ctx) { }; const asked = new Set([...NOTE_RELAYS, ...PROFILE_ARCHIVE_RELAYS]); (async () => { - await Promise.all([ - queryStreamOn([...asked], filter, take, 4500), + const depths = await Promise.all([ + queryDepthOn([...asked], filter, take, 4500), // the author's own relays, as soon as their list is known relaysOf(pk).then((own) => { const more = (own || []).filter((r) => !asked.has(r)); - return more.length ? queryStreamOn(more, filter, take, 4500) : null; - }).catch(() => {}), + return more.length ? queryDepthOn(more, filter, take, 4500) : []; + }).catch(() => []), + primalNotes(pk, filter.limit, 0, take), ]); clearTimeout(timer); await flush(); + await chain; + settlePage(c, depths.flat(), filter.limit); if (any) persistPage('notes', pk, c.notes.slice(0, 20).map(slimNote)); // cache stays bounded })().catch(() => {}).finally(() => { c.status = 'ready'; @@ -5512,22 +5516,75 @@ export function messagesFeature(ctx) { whenHide = setTimeout(() => whenEl && whenEl.classList.remove('on'), 250); }); } + // How far back a page of someone's posts can be trusted. Relays each hold + // their own part of an author's history, so the oldest post of a merged + // answer says little: ours held 6 of Ben Arc's posts reaching back to + // April while his own relay's newest 30 ended in late September, and + // "older than the oldest" then asked for posts before April — five months + // never shown, his Napstr launch among them (Adam, 2026-11-20). A source + // that sent a full page has more below its oldest post; the page is + // complete down to the NEWEST such floor. Posts below it wait in c.held + // and join the page that reaches them. No source full: nothing is left. + const fullPage = (n, limit) => n >= Math.ceil(limit / 2); + const holdBack = (c, e) => { + if (!c.floor || e.created_at >= c.floor) return false; + (c.held ||= new Map()).set(e.id, e); + return true; + }; + function settlePage(c, depths, limit) { + const floors = depths.filter((d) => d && fullPage(d.n, limit) && Number.isFinite(d.min)).map((d) => d.min); + const floor = floors.length ? Math.max(...floors) : 0; + // a refresh of a page already scrolled deeper never raises the floor + c.floor = c.floor && floor ? Math.min(c.floor, floor) : (c.floor && !floor ? c.floor : floor); + const all = new Map([...(c.held || new Map()), ...c.notes.map((e) => [e.id, e])]); + c.held = new Map(); + const shown = []; + for (const e of all.values()) (c.floor && e.created_at < c.floor ? c.held.set(e.id, e) : shown.push(e)); + c.notes = newestArticles(shown).sort((a, b) => b.created_at - a.created_at); + if (!floors.length && !c.floor) c.end = true; + else if (floors.length) c.end = false; + return floors.length > 0; + } + // Primal's cache keeps what relays have pruned (of the nine posts around + // the Napstr launch, the relays we ask held none). It answers posts and + // replies as separate feeds; each is a source with its own depth. The + // events are the signed originals, checked like a relay's. + async function primalNotes(pk, limit, until, take) { + const ask = async (notes) => { + const args = { pubkey: pk, notes, limit }; + if (until) args.until = until; + const evs = await primalCache('feed', args, 5000).catch(() => []); + const good = await verifiedPrimal((evs || []).filter((e) => e.pubkey === pk), PROFILE_KINDS); + for (const e of good) take(e); + return { url: 'primal:' + notes, n: good.length, min: good.length ? Math.min(...good.map((e) => e.created_at)) : Infinity }; + }; + return Promise.all([ask('authored'), ask('replies')]); + } async function loadOlderNotes(pk) { const c = notesCache.get(pk); if (!c || c.status !== 'ready' || c.loadingMore || c.end) return; const oldest = c.notes[c.notes.length - 1]; - if (!oldest) { c.end = true; return; } + if (!oldest && !c.floor) { c.end = true; return; } c.loadingMore = true; render(); try { - const evs = await queryOn(await notesRelays(pk), - { kinds: PROFILE_KINDS, authors: [pk], limit: 30, until: oldest.created_at - 1 }, 4500); + const limit = 30; + const until = (c.floor || oldest.created_at) - 1; + const got = new Map(); + const take = (e) => { if (e && e.pubkey === pk && e.created_at <= until) got.set(e.id, e); }; + const depths = await Promise.all([ + queryDepthOn(await notesRelays(pk), { kinds: PROFILE_KINDS, authors: [pk], limit, until }, take, 4500), + primalNotes(pk, limit, until, take), + ]); + const before = c.notes.length; const seen = new Set(c.notes.map((e) => e.id)); - const older = (evs || []) - .filter((e) => !seen.has(e.id) && seen.add(e.id)) - .sort((a, b) => b.created_at - a.created_at); - if (older.length) { await notesReady(older); c.notes = newestArticles([...c.notes, ...older]); } - else c.end = true; // the relays have nothing older + for (const e of got.values()) if (!seen.has(e.id)) (c.held ||= new Map()).set(e.id, e); + // this page reaches down to the new floor (or to the bottom: no source had more) + c.floor = 0; + const more = settlePage(c, depths.flat(), limit); + if (!more) { c.floor = 0; c.end = true; } + const added = c.notes.slice(before); + if (added.length) await notesReady(added.slice(0, FEED_PAGE)); } catch {} finally { c.loadingMore = false; if (ui.profilePk === pk) render(); |
