From ead96af16fb38da8d6b89d92b68ffd79f0e45562 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 18 Oct 2024 14:23:38 -0300 Subject: [PATCH] fix: get all zap related authors (receivers, senders) inside gatherAuthors() function --- src/storages/hydrate.ts | 50 +++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index a30608ca..ea2d8da9 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -65,10 +65,6 @@ async function hydrateEvents(opts: HydrateOpts): Promise { cache.push(event); } - for (const event of await gatherZapSender({ events: cache, store, signal })) { - cache.push(event); - } - const stats = { authors: await gatherAuthorStats(cache, kysely as Kysely), events: await gatherEventStats(cache, kysely as Kysely), @@ -230,13 +226,26 @@ function gatherQuotes({ events, store, signal }: HydrateOpts): Promise { - const pubkeys = new Set(events.map((event) => { + const pubkeys = new Set(); + + for (const event of events) { if (event.kind === 9735) { - const pubkey = event.tags.find(([name]) => name === 'p')?.[1]; - if (pubkey) return pubkey; + const zapReceiver = event.tags.find(([name]) => name === 'p')?.[1]; + if (zapReceiver) { + pubkeys.add(zapReceiver); + } + + const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; + const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); + // By getting the pubkey from the zap request we guarantee who is the sender + // some clients don't put the P tag in the zap receipt... + const zapSender = zapRequest?.pubkey; + if (zapSender) { + pubkeys.add(zapSender); + } } - return event.pubkey; - })); + pubkeys.add(event.pubkey); + } return store.query( [{ kinds: [0], authors: [...pubkeys], limit: pubkeys.size }], @@ -336,29 +345,6 @@ function gatherZapped({ events, store, signal }: HydrateOpts): Promise { - const pubkeys = new Set(); - - for (const event of events) { - if (event.kind === 9735) { - const zapRequestString = event?.tags?.find(([name]) => name === 'description')?.[1]; - const zapRequest = n.json().pipe(n.event()).optional().catch(undefined).parse(zapRequestString); - // By getting the pubkey from the zap request we guarantee who is the sender - // some clients don't put the P tag in the zap receipt... - const zapSender = zapRequest?.pubkey; - if (zapSender) { - pubkeys.add(zapSender); - } - } - } - - return store.query( - [{ kinds: [0], limit: pubkeys.size }], - { signal }, - ); -} - /** Collect author stats from the events. */ async function gatherAuthorStats( events: DittoEvent[],