From 4dfd9587181c263a46f3f077e37df638c26fe20c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 30 Mar 2024 16:12:48 -0500 Subject: [PATCH] hydrateEvents: refactor a separate hydrateAuthors function --- src/storages/hydrate.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index a7cdba8..405b196 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -17,18 +17,30 @@ async function hydrateEvents(opts: HydrateEventOpts): Promise { return events; } - if (relations.includes('author')) { - 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); + for (const relation in relations) { + switch (relation) { + case 'author': + await hydrateAuthors({ events, storage, signal }); + break; } } return events; } +async function hydrateAuthors(opts: Omit): Promise { + 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. */ function purifyEvent(event: NostrEvent): NostrEvent { return {