search: refactor/cleanup searchController

This commit is contained in:
Alex Gleason 2023-08-30 15:40:18 -05:00
parent 080cfe817b
commit b7b5e67118
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 18 additions and 13 deletions

View File

@ -29,21 +29,9 @@ const searchController: AppController = async (c) => {
return c.json({ error: 'Bad request', schema: result.error }, 422); 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([ const [event, events] = await Promise.all([
lookupEvent(result.data), lookupEvent(result.data),
(!type || type === 'statuses') ? eventsDB.getFilters([filter]) : [] as Event[], searchEvents(result.data),
]); ]);
if (event) { 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<Event[]> {
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. */ /** Get event kinds to search from `type` query param. */
function typeToKinds(type: SearchQuery['type']): number[] { function typeToKinds(type: SearchQuery['type']): number[] {
switch (type) { switch (type) {