diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 03e2235..121f6db 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -29,7 +29,7 @@ const createStatusSchema = z.object({ const statusController: AppController = async (c) => { const id = c.req.param('id'); - const event = await getEvent(id, 1); + const event = await getEvent(id, { kind: 1 }); if (event) { return c.json(await toStatus(event as Event<1>)); } @@ -88,7 +88,7 @@ const createStatusController: AppController = async (c) => { const contextController: AppController = async (c) => { const id = c.req.param('id'); - const event = await getEvent(id, 1); + const event = await getEvent(id, { kind: 1 }); if (event) { const ancestorEvents = await getAncestors(event); @@ -105,7 +105,7 @@ const contextController: AppController = async (c) => { const favouriteController: AppController = async (c) => { const id = c.req.param('id'); - const target = await getEvent(id, 1); + const target = await getEvent(id, { kind: 1 }); if (target) { const event = await signEvent({ diff --git a/src/queries.ts b/src/queries.ts index 2012c18..208fc61 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -3,11 +3,24 @@ import { eventDateComparator, type PaginationParams } from '@/utils.ts'; import { getFilters as getFiltersMixer } from './mixer.ts'; +interface GetEventOpts { + /** Timeout in milliseconds. */ + timeout?: number; + /** Event kind. */ + kind?: K; +} + /** Get a Nostr event by its ID. */ -const getEvent = async (id: string, kind?: K): Promise | undefined> => { +const getEvent = async ( + id: string, + opts: GetEventOpts = {}, +): Promise | undefined> => { + const { kind, timeout = 1000 } = opts; const filter: Filter = { ids: [id], limit: 1 }; - if (kind) filter.kinds = [kind]; - const [event] = await getFiltersMixer([filter], { limit: 1, timeout: 1000 }); + if (kind) { + filter.kinds = [kind]; + } + const [event] = await getFiltersMixer([filter], { limit: 1, timeout }); return event; }; @@ -56,7 +69,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise const inReplyTo = replyTag ? replyTag[1] : undefined; if (inReplyTo) { - const parentEvent = await getEvent(inReplyTo, 1); + const parentEvent = await getEvent(inReplyTo, { kind: 1 }); if (parentEvent) { result.push(parentEvent);