From b7b5e67118ec1e495c685fed4185cdba540c6c6f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 30 Aug 2023 15:40:18 -0500 Subject: [PATCH] search: refactor/cleanup searchController --- src/controllers/api/search.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index 0f926ce..6ba6183 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -29,21 +29,9 @@ const searchController: AppController = async (c) => { return c.json({ error: 'Bad request', schema: result.error }, 422); } - const { q, type, limit, account_id } = result.data; - - const filter: Filter = { - kinds: typeToKinds(type), - search: q, - limit, - }; - - if (account_id) { - filter.authors = [account_id]; - } - const [event, events] = await Promise.all([ lookupEvent(result.data), - (!type || type === 'statuses') ? eventsDB.getFilters([filter]) : [] as Event[], + searchEvents(result.data), ]); if (event) { @@ -72,6 +60,23 @@ const searchController: AppController = async (c) => { }); }; +/** Get events for the search params. */ +function searchEvents({ q, type, limit, account_id }: SearchQuery): Promise { + if (type === 'hashtags') return Promise.resolve([]); + + const filter: Filter = { + kinds: typeToKinds(type), + search: q, + limit, + }; + + if (account_id) { + filter.authors = [account_id]; + } + + return eventsDB.getFilters([filter]); +} + /** Get event kinds to search from `type` query param. */ function typeToKinds(type: SearchQuery['type']): number[] { switch (type) {