hydrateEvents: refactor a separate hydrateAuthors function

This commit is contained in:
Alex Gleason 2024-03-30 16:12:48 -05:00
parent 2ff96d2403
commit 4dfd958718
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 18 additions and 6 deletions

View File

@ -17,18 +17,30 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise<DittoEvent[]> {
return events; return events;
} }
if (relations.includes('author')) { for (const relation in relations) {
const pubkeys = new Set([...events].map((event) => event.pubkey)); switch (relation) {
const authors = await storage.query([{ kinds: [0], authors: [...pubkeys], limit: pubkeys.size }], { signal }); case 'author':
await hydrateAuthors({ events, storage, signal });
for (const event of events) { break;
event.author = authors.find((author) => author.pubkey === event.pubkey);
} }
} }
return events; return events;
} }
async function hydrateAuthors(opts: Omit<HydrateEventOpts, 'relations'>): Promise<DittoEvent[]> {
const { events, storage, signal } = opts;
const pubkeys = new Set([...events].map((event) => event.pubkey));
const authors = await storage.query([{ kinds: [0], authors: [...pubkeys], limit: pubkeys.size }], { signal });
for (const event of events) {
event.author = authors.find((author) => author.pubkey === event.pubkey);
}
return events;
}
/** Return a normalized event without any non-standard keys. */ /** Return a normalized event without any non-standard keys. */
function purifyEvent(event: NostrEvent): NostrEvent { function purifyEvent(event: NostrEvent): NostrEvent {
return { return {