Improve performance of /context endpoint

This commit is contained in:
Alex Gleason 2024-07-29 23:40:08 -05:00
parent ce18cb928a
commit edc4607c8d
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 5 additions and 5 deletions

View file

@ -247,7 +247,7 @@ const contextController: AppController = async (c) => {
if (event) { if (event) {
const [ancestorEvents, descendantEvents] = await Promise.all([ const [ancestorEvents, descendantEvents] = await Promise.all([
getAncestors(store, event), getAncestors(store, event),
getDescendants(store, event.id), getDescendants(store, event),
]); ]);
await hydrateEvents({ await hydrateEvents({

View file

@ -74,7 +74,7 @@ async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent
const inReplyTo = replyTag ? replyTag[1] : undefined; const inReplyTo = replyTag ? replyTag[1] : undefined;
if (inReplyTo) { if (inReplyTo) {
const [parentEvent] = await store.query([{ kinds: [1], ids: [inReplyTo], limit: 1 }]); const [parentEvent] = await store.query([{ kinds: [1], ids: [inReplyTo], until: event.created_at, limit: 1 }]);
if (parentEvent) { if (parentEvent) {
result.push(parentEvent); result.push(parentEvent);
@ -88,12 +88,12 @@ async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent
async function getDescendants( async function getDescendants(
store: NStore, store: NStore,
eventId: string, event: NostrEvent,
signal = AbortSignal.timeout(2000), signal = AbortSignal.timeout(2000),
): Promise<NostrEvent[]> { ): Promise<NostrEvent[]> {
return await store return await store
.query([{ kinds: [1], '#e': [eventId], limit: 200 }], { signal }) .query([{ kinds: [1], '#e': [event.id], since: event.created_at, limit: 200 }], { signal })
.then((events) => events.filter(({ tags }) => findReplyTag(tags)?.[1] === eventId)); .then((events) => events.filter(({ tags }) => findReplyTag(tags)?.[1] === event.id));
} }
/** Returns whether the pubkey is followed by a local user. */ /** Returns whether the pubkey is followed by a local user. */