Merge branch 'context' into 'main'

Improve performance of /context endpoint

See merge request soapbox-pub/ditto!437
This commit is contained in:
Alex Gleason 2024-07-30 04:46:31 +00:00
commit 67f65b4826
2 changed files with 6 additions and 6 deletions

View file

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

View file

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