diff options
| -rw-r--r-- | src/features/messages.js | 52 | ||||
| -rw-r--r-- | tools/own-post-feeds-test.js | 110 |
2 files changed, 143 insertions, 19 deletions
diff --git a/src/features/messages.js b/src/features/messages.js index b1da365..7abbb93 100644 --- a/src/features/messages.js +++ b/src/features/messages.js @@ -5660,12 +5660,16 @@ export function messagesFeature(ctx) { // posts a wider net found in one six-hour window were theirs. const FEED_AUTHORS_MAX = 2000; const REQ_AUTHORS = 400; // authors per REQ, so one filter stays a sane size + const authorChunks = (authors) => { const out = []; for (let i = 0; i < authors.length; i += REQ_AUTHORS) out.push(authors.slice(i, i + REQ_AUTHORS)); return out; }; const feedAuthors = (def = feedDef()) => { if (!def) return []; - const set = new Set(def.follows ? followsNow().set : []); // your own posts belong in the feed of the people you follow, as in - // every other client — not only for the moment after posting - if (def.follows && mePk()) set.add(mePk()); + // every other client — not only for the moment after posting. Yours + // first: a feed's opening ask takes the authors in chunks of REQ_AUTHORS, + // and with 898 follows a key added last sat in the third chunk — the + // feed opened without your newest post until a later refresh. + const set = new Set(def.follows && mePk() ? [mePk()] : []); + if (def.follows) for (const pk of followsNow().set) set.add(pk); for (const pk of def.authors || []) if (pk) set.add(pk); for (const pk of def.priv || []) if (pk) set.add(pk); // a list's private members for (const pk of packAuthors(def)) set.add(pk); @@ -5852,7 +5856,7 @@ export function messagesFeature(ctx) { let feedWarmTimer = null, feedWarmSession = 0; async function prefetchFeed(c) { const def = feedDef(c.id); - const authors = feedAuthors(def).slice(0, REQ_AUTHORS), topics = feedTopics(def); + const authors = feedAuthors(def), topics = feedTopics(def); if (!authors.length && !topics.length && !(def && def.all)) { c.status = 'ready'; return; } c.at = Date.now(); const merge = { newerThan: c.notes.length ? c.notes[0].created_at : Infinity }; @@ -5865,9 +5869,11 @@ export function messagesFeature(ctx) { try { const orT = orTopicsOf(def); await Promise.all([ - // people's reposts with their posts (a topic or firehose ask: posts only) - queryFeed(feedRelaysOr(def, def && def.all ? FIREHOSE_RELAYS : NOTE_RELAYS), { kinds: authors.length && !topics.length ? [...FEED_KINDS, REPOST] : FEED_KINDS, limit: FEED_PAGE, - ...(authors.length ? { authors } : {}), ...(topics.length ? { '#t': topics } : {}) }, merge, c, 3000), + // people's reposts with their posts (a topic or firehose ask: posts only); + // every author, a chunk of them per ask + ...(authors.length ? authorChunks(authors) : [null]).map((chunk) => + queryFeed(feedRelaysOr(def, def && def.all ? FIREHOSE_RELAYS : NOTE_RELAYS), { kinds: chunk && !topics.length ? [...FEED_KINDS, REPOST] : FEED_KINDS, limit: FEED_PAGE, + ...(chunk ? { authors: chunk } : {}), ...(topics.length ? { '#t': topics } : {}) }, merge, c, 3000)), orT.length ? queryFeed(feedRelaysOr(def, TOPIC_RELAYS), { kinds: FEED_KINDS, limit: FEED_PAGE, '#t': orT }, merge, c, 3000) : null]); } catch {} finally { if (!c.stopped) { @@ -5903,11 +5909,12 @@ export function messagesFeature(ctx) { feedsSeededAt = Date.now(); for (const def of feedList()) { if (def.curated || def.of || feedStates.has(def.id) || disk.has(def.id)) continue; - const authors = feedAuthors(def).slice(0, REQ_AUTHORS), topics = feedTopics(def); + const authors = feedAuthors(def), topics = feedTopics(def); if (!authors.length && !topics.length) continue; try { - const events = await queryOn(feedRelaysOr(def, NOTE_RELAYS), { kinds: FEED_KINDS, limit: FEED_SEED_POSTS * 2, - ...(authors.length ? { authors } : {}), ...(topics.length ? { '#t': topics } : {}) }, 3000); + const events = (await Promise.all((authors.length ? authorChunks(authors) : [null]).map((chunk) => + queryOn(feedRelaysOr(def, NOTE_RELAYS), { kinds: FEED_KINDS, limit: FEED_SEED_POSTS * 2, + ...(chunk ? { authors: chunk } : {}), ...(topics.length ? { '#t': topics } : {}) }, 3000)))).flat(); if (feedStates.has(def.id)) continue; // opened while we were asking disk.seed(def.id, events.filter((e) => FEED_KINDS.includes(e.kind) && !isReply(e) && !hidden(e))); } catch {} @@ -10029,17 +10036,26 @@ export function messagesFeature(ctx) { // and withdrawn on failure. async function publishPost(text, media = []) { const id = await requireIdentity(); + // the hashtags in the text as t tags (NIP-24), so topic feeds and other + // clients' searches find the post — up to the count past which a post + // reads as hashtag spam + const hashtags = [...new Set([...String(text || '').matchAll(/(^|[^\w#/&])#([\p{L}\p{N}_]{2,64})(?![\w])/gu)].map((m) => normTopic(m[2])))].slice(0, HASHTAG_MAX); const temp = { id: 'pending:' + Math.random().toString(36).slice(2), pubkey: id.pubkey, content: text, created_at: Math.floor(Date.now() / 1000), - tags: [], pending: true, + tags: hashtags.map((x) => ['t', x]), pending: true, }; const caches = [...new Set([id.pubkey, ui.profilePk].filter(Boolean))] .map((pk) => notesCache.get(pk)).filter(Boolean); for (const c of caches) c.notes = [temp, ...c.notes]; - // your own post belongs at the top of your own feed too, before any - // relay has heard of it - if (feed) { feed.notes = [temp, ...feed.notes]; } + // your own post belongs at the top of the feeds it would reach, before + // any relay has heard of it: the open feed and Following, when they + // carry your posts (Following, a people feed with you in it, Everything) + // or a topic the post wears — not a topic feed open at the time that it + // doesn't match, where it used to land (and then only there) + const carries = (def) => !!def && (!!def.all || feedAuthors(def).includes(id.pubkey) || feedTopics(def).some((x) => hashtags.includes(x))); + const feeds = [...new Set([feed, feedStates.get(FOLLOWING)].filter((c) => c && !c.stopped && carries(feedDef(c.id))))]; + for (const c of feeds) c.notes = [temp, ...c.notes]; render(); try { // NIP-92: what we uploaded, described, so clients needn't sniff the URL @@ -10050,19 +10066,17 @@ export function messagesFeature(ctx) { ? [] : ui.quoteOf ? [['q', ui.quoteOf.id], ['p', ui.quoteOf.pubkey]] : []; // the people it mentions are told (NIP-27 p tags) const mentions = mentionedPks(text).filter((pk) => pk !== id.pubkey && !q.some((x) => x[0] === 'p' && x[1] === pk)).map((pk) => ['p', pk]); - const partial = { kind: 1, content: text, created_at: temp.created_at, tags: [...imeta, ...q, ...mentions, CLIENT_TAG] }; + const partial = { kind: 1, content: text, created_at: temp.created_at, tags: [...imeta, ...q, ...mentions, ...temp.tags, CLIENT_TAG] }; const evt = id.signer instanceof Uint8Array ? finalizeEvent(partial, id.signer) : await id.signer.signEvent(partial); const relays = [...new Set([...(await notesRelays(id.pubkey)), ...wallet.nostrRelays()])]; const ok = await publishOn(relays, evt); if (!ok) throw new Error(t('msgSendFailed')); ui.quoteOf = null; - for (const c of caches) c.notes = c.notes.map((e) => (e.id === temp.id ? evt : e)); - if (feed) feed.notes = feed.notes.map((e) => (e.id === temp.id ? evt : e)); + for (const c of [...caches, ...feeds]) c.notes = c.notes.map((e) => (e.id === temp.id ? evt : e)); render(); return evt; } catch (e) { - for (const c of caches) c.notes = c.notes.filter((e) => e.id !== temp.id); - if (feed) feed.notes = feed.notes.filter((e) => e.id !== temp.id); + for (const c of [...caches, ...feeds]) c.notes = c.notes.filter((e) => e.id !== temp.id); render(); throw e; } diff --git a/tools/own-post-feeds-test.js b/tools/own-post-feeds-test.js new file mode 100644 index 0000000..a3e8fc3 --- /dev/null +++ b/tools/own-post-feeds-test.js @@ -0,0 +1,110 @@ +// Your own posts and the feeds. A Following feed's opening ask takes the +// authors in chunks of 400, and your own key used to be added last — with +// hundreds of follows it sat in a later chunk, and the feed opened without +// your newest post. Now your key leads, and every chunk is asked. And a new +// post goes to the top of the feeds it would reach (Following; a topic feed +// whose tag it wears, which is now a t tag on the event) — not to a topic +// feed that happens to be open and doesn't match. +// Run: bun tools/own-post-feeds-test.js +import puppeteer from './puppeteer.js'; +import { cacheKeyFor } from '../src/wallet.js'; +import { privateKeyFromSeedWords } from 'nostr-tools/nip06'; +import { generateSecretKey, getPublicKey } from 'nostr-tools/pure'; +import { buildHtml } from '../build.js'; +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); +let ok = true; +const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; }; +const FOLLOWS = Array.from({ length: 450 }, () => getPublicKey(generateSecretKey())); + +const html = await buildHtml({ minify: true, pwa: false }); +const server = Bun.serve({ port: 0, fetch: (req) => { + const path = new URL(req.url).pathname; + if (path.startsWith('/punks')) return new Response(Bun.file('dist' + path)); + return new Response(html, { headers: { 'content-type': 'text/html' } }); +} }); +const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); +const page = await browser.newPage(); +await page.evaluateOnNewDocument(() => { + window.__reqs = []; + const Real = window.WebSocket; + window.WebSocket = function (...a) { + const ws = new Real(...a); + const send = ws.send.bind(ws); + ws.send = (data) => { + try { const m = JSON.parse(data); if (m[0] === 'REQ' && m[2] && Array.isArray(m[2].kinds) && m[2].kinds.includes(1)) window.__reqs.push(m[2]); } catch {} + return send(data); + }; + return ws; + }; + window.WebSocket.prototype = Real.prototype; + Object.assign(window.WebSocket, Real); +}); +const errs = []; +page.on('pageerror', (e) => errs.push(String(e))); +const click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase().includes(x)); if (e) { e.click(); return true; } return false; }, t); +const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await page.evaluate(() => document.body.innerText)).toLowerCase().includes(x)) return true; await sleep(250); } return false; }; +const waitFor = async (fn, arg, ms = 10000) => { for (let i = 0; i < ms / 200; i++) { if (await page.evaluate(fn, arg)) return true; await sleep(200); } return false; }; +const chip = (label) => page.evaluate((l) => { const c = [...document.querySelectorAll('.feed-chips > .feed-chip')].find((x) => x.textContent.trim() === l); if (c) c.click(); return !!c; }, label); +const feedTexts = () => page.evaluate(() => [...document.querySelectorAll('.notes-feed [data-key]')].map((r) => r.innerText.replace(/\s+/g, ' ').slice(0, 60))); +const post = async (text) => { + await page.evaluate(() => document.querySelector('.feed-post-fab').click()); + await page.waitForSelector('.post-input', { timeout: 5000 }); + await page.focus('.post-input'); + await page.keyboard.type(text); + await sleep(200); + await page.evaluate(() => document.querySelector('.post-actions .btn-primary').click()); + await sleep(1200); +}; + +try { + await page.setViewport({ width: 390, height: 844 }); + await page.goto(server.url.href, { waitUntil: 'domcontentloaded' }); + await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest')); + await page.reload({ waitUntil: 'domcontentloaded' }); + await sleep(400); + await click('create a new wallet'); await sleep(600); + await page.waitForSelector('.words .w .t'); + const mn = (await page.$$eval('.words .w .t', (l) => l.map((e) => e.textContent.trim()))).join(' '); + await click('skip verification'); + await waitText('receive', 20000); + const base = cacheKeyFor(mn + '\n'); + const ME = getPublicKey(privateKeyFromSeedWords(mn)); + await page.evaluate(([k, pks]) => { + localStorage.setItem(k + ':follows', JSON.stringify({ tags: pks.map((pk) => ['p', pk]), c: '', at: Math.floor(Date.now() / 1000) })); + localStorage.setItem(k + ':feedNotes', JSON.stringify([])); + localStorage.setItem('btc-wallet-feed:' + k, 'following'); + }, [base, FOLLOWS]); + await page.reload({ waitUntil: 'domcontentloaded' }); + await waitText('receive', 20000); + await page.evaluate(() => { window.__reqs.length = 0; document.querySelector('.app-nav-button[aria-label="Feed"]').click(); }); + await sleep(2500); + + console.log('\n[the opening ask, with 450 follows]'); + const reqs = await page.evaluate(() => window.__reqs.filter((f) => Array.isArray(f.authors))); + const union = new Set(reqs.flatMap((f) => f.authors)); + check('every follow is asked for, in chunks', FOLLOWS.every((pk) => union.has(pk)) && reqs.every((f) => f.authors.length <= 400), `${reqs.length} asks, ${union.size} authors`); + check('my own key leads the first chunk', reqs.some((f) => f.authors[0] === ME), JSON.stringify(reqs.map((f) => f.authors.length))); + + console.log('\n[a post from Following]'); + await post('hello from the composer #gardenstr'); + check('it sits at the top of Following', (await feedTexts())[0]?.includes('hello from the composer'), JSON.stringify((await feedTexts()).slice(0, 2))); + const sent = await page.evaluate(() => window.__relayGuard.events.filter((e) => e.kind === 1)); + check('the hashtag went out as a t tag', sent.some((e) => /hello from the composer/.test(e.content) && e.tags.some((x) => x[0] === 't' && x[1] === 'gardenstr')), JSON.stringify(sent.map((e) => e.tags.filter((x) => x[0] === 't')))); + + console.log('\n[a post from a topic feed it does not match]'); + check('the #gardenstr feed opens', await chip('#gardenstr') && await waitFor(() => [...document.querySelectorAll('.feed-chips > .feed-chip')].some((c) => c.classList.contains('on') && c.textContent.trim() === '#gardenstr'), null, 4000)); + await post('no tag this time, just words'); + check('it does not land in #gardenstr', !(await feedTexts()).some((x) => x.includes('no tag this time')), JSON.stringify((await feedTexts()).slice(0, 2))); + await chip('Following'); await sleep(800); + check('…but it is at the top of Following', (await feedTexts())[0]?.includes('no tag this time'), JSON.stringify((await feedTexts()).slice(0, 2))); + + console.log('\n[a tagged post from the topic feed]'); + await chip('#gardenstr'); await sleep(800); + await post('tagged this time #gardenstr'); + check('it lands at the top of #gardenstr', (await feedTexts())[0]?.includes('tagged this time'), JSON.stringify((await feedTexts()).slice(0, 2))); + await chip('Following'); await sleep(800); + check('…and of Following', (await feedTexts())[0]?.includes('tagged this time'), JSON.stringify((await feedTexts()).slice(0, 2))); + check('no page errors', errs.length === 0, errs.slice(0, 2).join(' | ')); +} finally { await browser.close(); server.stop(true); } +console.log(ok ? '\n✅ your posts reach the feeds they belong to' : '\n❌ failed'); +process.exit(ok ? 0 : 1); |
