From 799a9c1e9a6a87c7e7582f7866cf02a5c0b3495b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 30 Aug 2023 15:22:39 -0500 Subject: [PATCH] search: support searching by account_id --- src/controllers/api/search.ts | 47 +++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index 1adc266..a61c0bc 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -28,11 +28,24 @@ const searchController: AppController = async (c) => { return c.json({ error: 'Bad request', schema: result.error }, 422); } - const { q, type, limit } = result.data; + const { q, type, limit, account_id } = result.data; + + const searchAccounts = !type || type === 'accounts'; + const searchStatuses = !type || type === 'statuses'; + + const filter: Filter = { + kinds: searchAccounts ? [0] : [1], + search: q, + limit, + }; + + if (account_id) { + filter.authors = [account_id]; + } const [event, events] = await Promise.all([ lookupEvent(result.data), - !type || type === 'statuses' ? eventsDB.getFilters([{ kinds: [1], search: q, limit }]) : [] as Event[], + searchStatuses ? eventsDB.getFilters([filter]) : [] as Event[], ]); if (event) { @@ -72,6 +85,9 @@ async function lookupEvent(query: SearchQuery): Promise { async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise { const filters: Filter[] = []; + const accounts = !type || type === 'accounts'; + const statuses = !type || type === 'statuses'; + if (!resolve || type === 'hashtags') { return filters; } @@ -81,43 +97,32 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise { - switch (type) { - case 'accounts': - return kinds?.every((kind) => kind === 0); - case 'statuses': - return kinds?.every((kind) => kind === 1); - } - }); + return filters; } export { searchController };