From 8890f6bce5247b51faf1111a7cb713e2f948d741 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 18 Sep 2024 12:58:17 -0500 Subject: [PATCH] searchEvents: fix account_id, simplify code --- src/controllers/api/search.ts | 52 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index 78b8990b..ce7ca9f3 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -80,7 +80,12 @@ async function searchEvents( { q, type, limit, account_id, viewerPubkey }: SearchQuery & { viewerPubkey?: string }, signal: AbortSignal, ): Promise { - if (type === 'hashtags') return Promise.resolve([]); + // Hashtag search is not supported. + if (type === 'hashtags') { + return Promise.resolve([]); + } + + const store = await Storages.search(); const filter: NostrFilter = { kinds: typeToKinds(type), @@ -88,36 +93,33 @@ async function searchEvents( limit, }; + // For account search, use a special index, and prioritize followed accounts. + if (type === 'accounts') { + const kysely = await Storages.kysely(); + + const followedPubkeys = viewerPubkey ? await getFollowedPubkeys(viewerPubkey) : new Set(); + const searchPubkeys = await getPubkeysBySearch(kysely, { q, limit, followedPubkeys }); + + filter.authors = [...searchPubkeys]; + filter.search = undefined; + } + + // Results should only be shown from one author. if (account_id) { filter.authors = [account_id]; } - let pubkeys: Set = new Set(); - if (type === 'accounts') { - const kysely = await Storages.kysely(); - - const followedPubkeys: Set = viewerPubkey ? await getFollowedPubkeys(viewerPubkey) : new Set(); - pubkeys = pubkeys.union(await getPubkeysBySearch(kysely, { q, limit, followedPubkeys })); - - if (!filter?.authors) { - filter.authors = Array.from(pubkeys); - } else { - filter.authors.push(...pubkeys); - } - - filter.search = undefined; - } - - const store = await Storages.search(); - - let events = await store.query([filter], { signal }) + // Query the events. + let events = await store + .query([filter], { signal }) .then((events) => hydrateEvents({ events, store, signal })); - if (type !== 'accounts') return events; - - events = Array.from(pubkeys) - .map((pubkey) => events.find((event) => event.pubkey === pubkey)) - .filter((event) => !!event); + // When using an authors filter, return the events in the same order as the filter. + if (filter.authors) { + events = filter.authors + .map((pubkey) => events.find((event) => event.pubkey === pubkey)) + .filter((event) => !!event); + } return events; }