search: support searching by account_id

This commit is contained in:
Alex Gleason 2023-08-30 15:22:39 -05:00
parent 8079679f18
commit 799a9c1e9a
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 26 additions and 21 deletions

View File

@ -28,11 +28,24 @@ 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 } = 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([ const [event, events] = await Promise.all([
lookupEvent(result.data), lookupEvent(result.data),
!type || type === 'statuses' ? eventsDB.getFilters<number>([{ kinds: [1], search: q, limit }]) : [] as Event[], searchStatuses ? eventsDB.getFilters([filter]) : [] as Event[],
]); ]);
if (event) { if (event) {
@ -72,6 +85,9 @@ async function lookupEvent(query: SearchQuery): Promise<Event | undefined> {
async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise<Filter[]> { async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise<Filter[]> {
const filters: Filter[] = []; const filters: Filter[] = [];
const accounts = !type || type === 'accounts';
const statuses = !type || type === 'statuses';
if (!resolve || type === 'hashtags') { if (!resolve || type === 'hashtags') {
return filters; return filters;
} }
@ -81,43 +97,32 @@ async function getLookupFilters({ q, type, resolve }: SearchQuery): Promise<Filt
const result = nip19.decode(q); const result = nip19.decode(q);
switch (result.type) { switch (result.type) {
case 'npub': case 'npub':
filters.push({ kinds: [0], authors: [result.data] }); if (accounts) filters.push({ kinds: [0], authors: [result.data] });
break; break;
case 'nprofile': case 'nprofile':
filters.push({ kinds: [0], authors: [result.data.pubkey] }); if (accounts) filters.push({ kinds: [0], authors: [result.data.pubkey] });
break; break;
case 'note': case 'note':
filters.push({ kinds: [1], ids: [result.data] }); if (statuses) filters.push({ kinds: [1], ids: [result.data] });
break; break;
case 'nevent': case 'nevent':
filters.push({ kinds: [1], ids: [result.data.id] }); if (statuses) filters.push({ kinds: [1], ids: [result.data.id] });
break; break;
} }
} catch (_e) { } catch (_e) {
// do nothing // do nothing
} }
} else if (/^[0-9a-f]{64}$/.test(q)) { } else if (/^[0-9a-f]{64}$/.test(q)) {
filters.push({ kinds: [0], authors: [q] }); if (accounts) filters.push({ kinds: [0], authors: [q] });
filters.push({ kinds: [1], ids: [q] }); if (statuses) filters.push({ kinds: [1], ids: [q] });
} else if ((!type || type === 'accounts') && ACCT_REGEX.test(q)) { } else if (accounts && ACCT_REGEX.test(q)) {
const pubkey = await lookupNip05Cached(q); const pubkey = await lookupNip05Cached(q);
if (pubkey) { if (pubkey) {
filters.push({ kinds: [0], authors: [pubkey] }); filters.push({ kinds: [0], authors: [pubkey] });
} }
} }
if (!type) { return filters;
return filters;
}
return filters.filter(({ kinds }) => {
switch (type) {
case 'accounts':
return kinds?.every((kind) => kind === 0);
case 'statuses':
return kinds?.every((kind) => kind === 1);
}
});
} }
export { searchController }; export { searchController };