From 3da9ffab9e32aa6fa4f9c8593ccc964bdd72851b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 30 Apr 2023 17:12:51 -0500 Subject: [PATCH] Try setting a timeout for context endpoint --- src/client.ts | 2 +- src/controllers/api/statuses.ts | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index 374612fc..9d1bc69f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -116,7 +116,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise } function getDescendants(eventId: string): Promise[]> { - return getFilter({ kinds: [1], '#e': [eventId] }) as Promise[]>; + return getFilter({ kinds: [1], '#e': [eventId], limit: 200 }) as Promise[]>; } export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, pool }; diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index eda82342..88a4c7d5 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -42,14 +42,21 @@ const createStatusController = validator('json', async (value, c: AppContext) => } }); +// https://stackoverflow.com/a/64820881/8811886 +function wait(ms: number) { + return new Promise((_, reject) => { + setTimeout(() => reject(new Error('timeout succeeded')), ms); + }); +} + const contextController: AppController = async (c) => { const id = c.req.param('id'); const event = await getEvent(id, 1); if (event) { - const ancestorEvents = await getAncestors(event); - const descendantEvents = await getDescendants(event.id); + const ancestorEvents = await Promise.race([wait(1000), getAncestors(event)]) as Event<1>[]; + const descendantEvents = await Promise.race([wait(1000), getDescendants(event.id)]) as Event<1>[]; return c.json({ ancestors: (await Promise.all((ancestorEvents).map(toStatus))).filter(Boolean),