diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index 1a1bd867..2555109b 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -91,19 +91,35 @@ async function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: filter.authors = [account_id]; } + const pubkeys: string[] = []; if (type === 'accounts') { const kysely = await Storages.kysely(); - const pubkeys = await getPubkeysBySearch(kysely, { q, limit }); + pubkeys.push(...(await getPubkeysBySearch(kysely, { q, limit }))); + + if (!filter?.authors) filter.authors = pubkeys; + else filter.authors.push(...pubkeys); - filter.authors = pubkeys; filter.search = undefined; } const store = await Storages.search(); - return store.query([filter], { signal }) + const events = await store.query([filter], { signal }) .then((events) => hydrateEvents({ events, store, signal })); + + if (type !== 'accounts') return events; + + const orderedEvents: NostrEvent[] = events.map((event, index) => { + const pubkey = pubkeys[index]; + + const orderedEvent = events.find((e) => e.pubkey === pubkey); + if (orderedEvent) return orderedEvent; + + return event; + }); + + return orderedEvents; } /** Get event kinds to search from `type` query param. */