fix: get all zap related authors (receivers, senders) inside gatherAuthors() function

This commit is contained in:
P. Reis 2024-10-18 14:23:38 -03:00
parent 4be6fe004d
commit ead96af16f

View file

@ -65,10 +65,6 @@ async function hydrateEvents(opts: HydrateOpts): Promise<DittoEvent[]> {
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<DittoTables>),
events: await gatherEventStats(cache, kysely as Kysely<DittoTables>),
@ -230,13 +226,26 @@ function gatherQuotes({ events, store, signal }: HydrateOpts): Promise<DittoEven
/** Collect authors from the events. */
function gatherAuthors({ events, store, signal }: HydrateOpts): Promise<DittoEvent[]> {
const pubkeys = new Set(events.map((event) => {
const pubkeys = new Set<string>();
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<DittoEven
);
}
/** Collect author that zapped. */
function gatherZapSender({ events, store, signal }: HydrateOpts): Promise<DittoEvent[]> {
const pubkeys = new Set<string>();
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[],