accountSearchController: respect the `limit` param

This commit is contained in:
Alex Gleason 2024-05-19 11:45:42 -05:00
parent 050cfff88d
commit 9754e29603
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 16 additions and 6 deletions

View File

@ -86,25 +86,35 @@ const accountLookupController: AppController = async (c) => {
} }
}; };
const accountSearchController: AppController = async (c) => { const accountSearchQuerySchema = z.object({
const q = c.req.query('q'); q: z.string().transform(decodeURIComponent),
resolve: booleanParamSchema.optional().transform(Boolean),
following: z.boolean().default(false),
limit: z.coerce.number().catch(20).transform((value) => Math.min(Math.max(value, 0), 40)),
});
if (!q) { const accountSearchController: AppController = async (c) => {
return c.json({ error: 'Missing `q` query parameter.' }, 422); const result = accountSearchQuerySchema.safeParse(c.req.query());
const { signal } = c.req.raw;
if (!result.success) {
return c.json({ error: 'Bad request', schema: result.error }, 422);
} }
const { q, limit } = result.data;
const query = decodeURIComponent(q); const query = decodeURIComponent(q);
const store = await Storages.search(); const store = await Storages.search();
const [event, events] = await Promise.all([ const [event, events] = await Promise.all([
lookupAccount(query), lookupAccount(query),
store.query([{ kinds: [0], search: query, limit: 20 }], { signal: c.req.raw.signal }), store.query([{ kinds: [0], search: query, limit }], { signal }),
]); ]);
const results = await hydrateEvents({ const results = await hydrateEvents({
events: event ? [event, ...events] : events, events: event ? [event, ...events] : events,
store, store,
signal: c.req.raw.signal, signal,
}); });
if ((results.length < 1) && query.match(/npub1\w+/)) { if ((results.length < 1) && query.match(/npub1\w+/)) {