Refactor getEvent to take an opts object
This commit is contained in:
parent
96a2c8b68a
commit
7330cd10e2
|
@ -29,7 +29,7 @@ const createStatusSchema = z.object({
|
||||||
const statusController: AppController = async (c) => {
|
const statusController: AppController = async (c) => {
|
||||||
const id = c.req.param('id');
|
const id = c.req.param('id');
|
||||||
|
|
||||||
const event = await getEvent(id, 1);
|
const event = await getEvent(id, { kind: 1 });
|
||||||
if (event) {
|
if (event) {
|
||||||
return c.json(await toStatus(event as Event<1>));
|
return c.json(await toStatus(event as Event<1>));
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ const createStatusController: AppController = async (c) => {
|
||||||
const contextController: AppController = async (c) => {
|
const contextController: AppController = async (c) => {
|
||||||
const id = c.req.param('id');
|
const id = c.req.param('id');
|
||||||
|
|
||||||
const event = await getEvent(id, 1);
|
const event = await getEvent(id, { kind: 1 });
|
||||||
|
|
||||||
if (event) {
|
if (event) {
|
||||||
const ancestorEvents = await getAncestors(event);
|
const ancestorEvents = await getAncestors(event);
|
||||||
|
@ -105,7 +105,7 @@ const contextController: AppController = async (c) => {
|
||||||
|
|
||||||
const favouriteController: AppController = async (c) => {
|
const favouriteController: AppController = async (c) => {
|
||||||
const id = c.req.param('id');
|
const id = c.req.param('id');
|
||||||
const target = await getEvent(id, 1);
|
const target = await getEvent(id, { kind: 1 });
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
const event = await signEvent({
|
const event = await signEvent({
|
||||||
|
|
|
@ -3,11 +3,24 @@ import { eventDateComparator, type PaginationParams } from '@/utils.ts';
|
||||||
|
|
||||||
import { getFilters as getFiltersMixer } from './mixer.ts';
|
import { getFilters as getFiltersMixer } from './mixer.ts';
|
||||||
|
|
||||||
|
interface GetEventOpts<K extends number> {
|
||||||
|
/** Timeout in milliseconds. */
|
||||||
|
timeout?: number;
|
||||||
|
/** Event kind. */
|
||||||
|
kind?: K;
|
||||||
|
}
|
||||||
|
|
||||||
/** Get a Nostr event by its ID. */
|
/** Get a Nostr event by its ID. */
|
||||||
const getEvent = async <K extends number = number>(id: string, kind?: K): Promise<Event<K> | undefined> => {
|
const getEvent = async <K extends number = number>(
|
||||||
|
id: string,
|
||||||
|
opts: GetEventOpts<K> = {},
|
||||||
|
): Promise<Event<K> | undefined> => {
|
||||||
|
const { kind, timeout = 1000 } = opts;
|
||||||
const filter: Filter<K> = { ids: [id], limit: 1 };
|
const filter: Filter<K> = { ids: [id], limit: 1 };
|
||||||
if (kind) filter.kinds = [kind];
|
if (kind) {
|
||||||
const [event] = await getFiltersMixer([filter], { limit: 1, timeout: 1000 });
|
filter.kinds = [kind];
|
||||||
|
}
|
||||||
|
const [event] = await getFiltersMixer([filter], { limit: 1, timeout });
|
||||||
return event;
|
return event;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -56,7 +69,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise
|
||||||
const inReplyTo = replyTag ? replyTag[1] : undefined;
|
const inReplyTo = replyTag ? replyTag[1] : undefined;
|
||||||
|
|
||||||
if (inReplyTo) {
|
if (inReplyTo) {
|
||||||
const parentEvent = await getEvent(inReplyTo, 1);
|
const parentEvent = await getEvent(inReplyTo, { kind: 1 });
|
||||||
|
|
||||||
if (parentEvent) {
|
if (parentEvent) {
|
||||||
result.push(parentEvent);
|
result.push(parentEvent);
|
||||||
|
|
Loading…
Reference in New Issue