diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/features/messages.js | 69 | ||||
| -rw-r--r-- | src/i18n.js | 1 |
2 files changed, 58 insertions, 12 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index fdd5643..80ce959 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -15,7 +15,7 @@ import { subscribeOn, publishOn, queryOn, fetchInboxRelays, relayAlive, liveRelayList, resetRelay, npubOf, neventOf, parseNostrPubkey, parseNostrRef, generateSecretKey, getPublicKey, finalizeEvent, nip44, - PROFILE_RELAYS, openWrapsOffthread, unwrapDMsOffthread, + PROFILE_RELAYS, openWrapsOffthread, unwrapDMsOffthread, verifyEventsAsync, } from '../nostr.js'; import { channelKey, channelStream, channelEpoch, channelIsPrivate, controlKey, guestbookKey, openWrap, wrapRumor, rumorWithId, @@ -32,6 +32,7 @@ import { saveInbox } from '../dm-inbox.js'; import { mergeFeedWindow } from '../feed-window.js'; import { createThreadStore } from '../thread-cache.js'; import { createFeedCache, FEED_CACHE_POSTS } from '../feed-cache.js'; +import { PUBLIC_FEED_RELAYS, popularCandidates } from '../popular-feed.js'; import { makeSearcher, resultRows, fallbackAvatar, warmSearch, punkImageUrl, punkSmallUrl } from '../recipient-search.js'; import { getNetwork } from '../api.js'; import { decodeBolt11 } from '../ark/lightning.js'; @@ -3948,7 +3949,7 @@ export function messagesFeature(ctx) { const RELAY_PICKS = ['wss://relay.coinos.io', 'wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.primal.net', 'wss://relay.snort.social', 'wss://nostr.mom', 'wss://nostr.bitcoiner.social', 'wss://eden.nostr.land', 'wss://nostr.land', 'wss://nostr.oxtr.dev', 'wss://relay.nostrplebs.com', 'wss://offchain.pub', 'wss://nostr21.com', 'wss://relay.nostr.net', 'wss://nostr-pub.wellorder.net']; - const FIREHOSE_RELAYS = ['wss://relay.coinos.io', 'wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.primal.net']; + const FIREHOSE_RELAYS = PUBLIC_FEED_RELAYS; const normRelay = (u) => { let x = String(u || '').trim(); if (!x) return null; @@ -3966,10 +3967,38 @@ export function messagesFeature(ctx) { // latest reactions, reposts and zaps, counted per post by distinct // people, then the posts enough people touched. Older pages walk the // reactions back in time. The prose and language tests run at the door. - const POPULAR_MIN = 2; const POPULAR_ASK = 600; // reactions per round const curateFeed = (evs) => { const lang = getLang(); return evs.filter((e) => !isMachinePost(e.content) && inLanguage(e.content, lang)); }; async function popularPass(extra, merge, c, def) { + // Only the default public feed shares a snapshot. Custom relays and older + // pages keep their own queries. A static/offline install falls back quickly. + if (extra.until == null && !feedRelays(def)) { + try { + const response = await fetch('/api/feed', { signal: AbortSignal.timeout(1500) }); + if (!response.ok || !response.headers.get('content-type')?.includes('application/json')) throw new Error('No feed cache'); + const data = await response.json(); + if (data.version !== 1 || !Number.isFinite(data.generatedAt) || Date.now() - data.generatedAt > 86400_000 + || !Array.isArray(data.notes) || data.notes.length > 240 || !Array.isArray(data.profiles) || data.profiles.length > 240 + || !Number.isFinite(data.popUntil)) throw new Error('Invalid feed cache'); + const events = [...data.notes, ...data.profiles]; + // HTTP is another untrusted source: retain the relay signature checks. + const valid = await verifyEventsAsync(events); + if (c.stopped) return false; + const posts = data.notes.filter((e, i) => valid[i] && e.kind === 1); + const usable = curateFeed(posts).filter((e) => !isReply(e) && !hidden(e)); + if (usable.length) { + warmProfiles(); + for (let i = 0; i < data.profiles.length; i++) { + const e = data.profiles[i]; + if (valid[data.notes.length + i] && e.kind === 0) applyProfile(e.pubkey, e); + } + c.popUntil = c.popUntil || data.popUntil; + const got = await mergeFeed(posts, { ...merge, immediate: true }, c); + if (got) scheduleRepaint(); + return got; // already-known posts also count as a successful cache hit + } + } catch {} // relay path also covers snapshots with no posts in this language + } const relays = feedRelaysOr(def, FIREHOSE_RELAYS); const wide = [...new Set([...relays, ...NOTE_RELAYS])]; let until = extra.until != null && c.popUntil ? c.popUntil - 1 : undefined; @@ -3977,19 +4006,15 @@ export function messagesFeature(ctx) { for (let round = 0; round < 3 && !c.stopped; round++) { const re = await queryOn(wide, { kinds: [6, 7, 9735], limit: POPULAR_ASK, ...(until ? { until } : {}) }, 4500).catch(() => []); if (!re.length) break; - const counts = new Map(); - for (const r of re) { - const who = r.kind === 9735 ? ((r.tags.find((x) => x[0] === 'P') || [])[1] || r.pubkey) : r.pubkey; - for (const x of r.tags) if (x[0] === 'e' && /^[0-9a-f]{64}$/.test(x[1] || '')) (counts.get(x[1]) || counts.set(x[1], new Set()).get(x[1])).add(who); - } + const candidates = popularCandidates(re); const oldest = Math.min(...re.map((r) => r.created_at)); c.popUntil = c.popUntil && until ? Math.min(c.popUntil, oldest) : (c.popUntil || oldest); until = oldest - 1; - const ids = [...counts.entries()].filter(([, s]) => s.size >= POPULAR_MIN).sort((a, b) => b[1].size - a[1].size).slice(0, 80).map(([id]) => id); + const ids = candidates.ids; if (!ids.length) continue; const asks = []; for (let i = 0; i < ids.length; i += 40) asks.push(queryOn(wide, { ids: ids.slice(i, i + 40) }, 4500).catch(() => [])); - const posts = (await Promise.all(asks)).flat().filter((e) => e && e.kind === 1 && (counts.get(e.id) || new Set()).size - (counts.get(e.id)?.has(e.pubkey) ? 1 : 0) >= POPULAR_MIN); + const posts = (await Promise.all(asks)).flat().filter(candidates.qualifies); if (c.stopped) return got; if (await mergeFeed(posts, merge, c)) { got = true; scheduleRepaint(); } if (got && round >= 0 && (c.notes.length >= FEED_PAGE || extra.until != null)) break; @@ -4707,6 +4732,19 @@ export function messagesFeature(ctx) { } if (c.booting) await c.boot; if (c.stopped) return false; + if (opts.immediate && !c.notes.length && !opts.catchup) { + // The public snapshot already contains verified posts and profiles. + // Paint text now using the same progressive rows as the local cache; + // slow media/quotes must not hold the whole first page behind a spinner. + c.booting = true; + admitFeed(add, c); + c.boot = notesReady(c.notes.slice(0, FEED_PAGE)).finally(() => { + c.booting = false; + if (!c.stopped && c === feed) scheduleRepaint(); + }); + scheduleRepaint(); + return true; + } await notesReady(add); if (c.stopped) return false; for (const e of add) staged.delete(e.id); @@ -6357,7 +6395,12 @@ export function messagesFeature(ctx) { // punks. The name and face come from the batch; the page loads when the // profile is actually opened. profileOf(pk); - const isReply = ev.tags.some((x) => x[0] === 'e'); + // An event of a kind this app has no renderer for (a chess game, kind + // 64, reached through a reaction to it) is shown as what its author's + // client said it is (NIP-31 alt), never as raw content dressed as a post. + const foreign = ev.kind !== 1 && ev.kind !== 30023; + const altText = foreign ? ((ev.tags.find((x) => x[0] === 'alt') || [])[1] || t('noteForeignKind', { kind: ev.kind })) : null; + const isReply = !foreign && ev.tags.some((x) => x[0] === 'e'); const canZap = canZapPk(pk); // an optimistic post mid-publish: visible but not yet a real event — // dimmed, and no thread/reply/zap until its signed self takes over @@ -6402,7 +6445,9 @@ export function messagesFeature(ctx) { style: 'flex-shrink:0', onClick: (e) => { e.stopPropagation(); ui.noteSheet = ev; render(); }, }, '\u22ef')), - h('div', { class: 'note-text', style: 'white-space:pre-wrap;overflow-wrap:anywhere' }, ...noteBody(ev.content, 0, emojiTagMap(ev.tags))), + foreign + ? h('div', { class: 'note-text small muted', style: 'white-space:pre-wrap;overflow-wrap:anywhere' }, (ev.kind === 64 ? '\u265f ' : '') + altText) + : h('div', { class: 'note-text', style: 'white-space:pre-wrap;overflow-wrap:anywhere' }, ...noteBody(ev.content, 0, emojiTagMap(ev.tags))), pending ? null : noteActions(pk, ev, { canZap }), !pending && whoOpen(ev.id) ? whoPanel(ev) : null)); } diff --git a/src/i18n.js b/src/i18n.js index 7b92e7d..0f11853 100644 --- a/src/i18n.js +++ b/src/i18n.js @@ -457,6 +457,7 @@ const STR = { arkFeeChain: 'On-chain fee', arkFeeService: 'Service fee', arkCoinsExpired: 'expired', + noteForeignKind: 'A kind {kind} event this app doesn’t show.', arkExpiredDust: '{amount} has expired and is too small to renew. You can forget it from the Manage page.', arkCoinForget: 'Forget', arkCoinForgetTitle: 'Give this coin up: it has expired and is too small to renew or exit.', |
