From c5c4f47bc2803906348aa784a75f63a638acb6eb Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 5 Jun 2024 11:00:16 -0500 Subject: [PATCH 1/4] Hide muted users from threads --- src/controllers/api/statuses.ts | 5 +++-- src/queries.ts | 16 +++++++++------- src/storages/UserStore.ts | 7 ++++++- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 91b4d45..817f988 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -203,6 +203,7 @@ const deleteStatusController: AppController = async (c) => { const contextController: AppController = async (c) => { const id = c.req.param('id'); + const store = c.get('store'); const event = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'] }); const viewerPubkey = await c.get('signer')?.getPublicKey(); @@ -215,8 +216,8 @@ const contextController: AppController = async (c) => { if (event) { const [ancestors, descendants] = await Promise.all([ - getAncestors(event).then(renderStatuses), - getDescendants(event.id).then(renderStatuses), + getAncestors(store, event).then(renderStatuses), + getDescendants(store, event.id).then(renderStatuses), ]); return c.json({ ancestors, descendants }); diff --git a/src/queries.ts b/src/queries.ts index 1fccb68..bf4fce2 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -1,4 +1,4 @@ -import { NostrEvent, NostrFilter } from '@nostrify/nostrify'; +import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify'; import Debug from '@soapbox/stickynotes/debug'; import { Conf } from '@/config.ts'; @@ -68,7 +68,7 @@ async function getFeedPubkeys(pubkey: string): Promise { return [...authors, pubkey]; } -async function getAncestors(event: NostrEvent, result: NostrEvent[] = []): Promise { +async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent[] = []): Promise { if (result.length < 100) { const replyTag = findReplyTag(event.tags); const inReplyTo = replyTag ? replyTag[1] : undefined; @@ -78,7 +78,7 @@ async function getAncestors(event: NostrEvent, result: NostrEvent[] = []): Promi if (parentEvent) { result.push(parentEvent); - return getAncestors(parentEvent, result); + return getAncestors(store, parentEvent, result); } } } @@ -86,11 +86,13 @@ async function getAncestors(event: NostrEvent, result: NostrEvent[] = []): Promi return result.reverse(); } -async function getDescendants(eventId: string, signal = AbortSignal.timeout(2000)): Promise { - const store = await Storages.db(); - +async function getDescendants( + store: NStore, + eventId: string, + signal = AbortSignal.timeout(2000), +): Promise { const events = await store - .query([{ kinds: [1], '#e': [eventId] }], { limit: 200, signal }) + .query([{ kinds: [1], '#e': [eventId], limit: 200 }], { signal }) .then((events) => events.filter(({ tags }) => findReplyTag(tags)?.[1] === eventId)); return hydrateEvents({ events, store, signal }); diff --git a/src/storages/UserStore.ts b/src/storages/UserStore.ts index 43c1771..2449d8c 100644 --- a/src/storages/UserStore.ts +++ b/src/storages/UserStore.ts @@ -4,6 +4,8 @@ import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { getTagSet } from '@/utils/tags.ts'; export class UserStore implements NStore { + private promise: Promise | undefined; + constructor(private pubkey: string, private store: NStore) {} async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise { @@ -24,7 +26,10 @@ export class UserStore implements NStore { } private async getMuteList(): Promise { - const [muteList] = await this.store.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }]); + if (!this.promise) { + this.promise = this.store.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }]); + } + const [muteList] = await this.promise; return muteList; } From f7a9bf9ffdb2d9ae9c2cb93e43a4b40910ace7a8 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 5 Jun 2024 11:04:25 -0500 Subject: [PATCH 2/4] Fix thread hydration --- src/controllers/api/statuses.ts | 6 ++++++ src/queries.ts | 6 ++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 817f988..01e947a 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -220,6 +220,12 @@ const contextController: AppController = async (c) => { getDescendants(store, event.id).then(renderStatuses), ]); + await hydrateEvents({ + events: [...ancestors, ...descendants], + signal: c.req.raw.signal, + store, + }); + return c.json({ ancestors, descendants }); } diff --git a/src/queries.ts b/src/queries.ts index bf4fce2..a6fb832 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -74,7 +74,7 @@ async function getAncestors(store: NStore, event: NostrEvent, result: NostrEvent const inReplyTo = replyTag ? replyTag[1] : undefined; if (inReplyTo) { - const parentEvent = await getEvent(inReplyTo, { kind: 1 }); + const [parentEvent] = await store.query([{ ids: [inReplyTo], limit: 1 }]); if (parentEvent) { result.push(parentEvent); @@ -91,11 +91,9 @@ async function getDescendants( eventId: string, signal = AbortSignal.timeout(2000), ): Promise { - const events = await store + return await store .query([{ kinds: [1], '#e': [eventId], limit: 200 }], { signal }) .then((events) => events.filter(({ tags }) => findReplyTag(tags)?.[1] === eventId)); - - return hydrateEvents({ events, store, signal }); } /** Returns whether the pubkey is followed by a local user. */ From eead178c6a05e7a622e5ba3ce36ea67a510b8f6f Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 5 Jun 2024 11:05:07 -0500 Subject: [PATCH 3/4] getAncestors: only query kind 1 --- src/queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/queries.ts b/src/queries.ts index a6fb832..745429d 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -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([{ ids: [inReplyTo], limit: 1 }]); + const [parentEvent] = await store.query([{ kinds: [1], ids: [inReplyTo], limit: 1 }]); if (parentEvent) { result.push(parentEvent); From 18d3363b656aa3e0335a1e193d31838af1e8e7c1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 5 Jun 2024 11:10:58 -0500 Subject: [PATCH 4/4] contextController: fix hydration again --- src/controllers/api/statuses.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 01e947a..b5ecf01 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -215,17 +215,22 @@ const contextController: AppController = async (c) => { } if (event) { - const [ancestors, descendants] = await Promise.all([ - getAncestors(store, event).then(renderStatuses), - getDescendants(store, event.id).then(renderStatuses), + const [ancestorEvents, descendantEvents] = await Promise.all([ + getAncestors(store, event), + getDescendants(store, event.id), ]); await hydrateEvents({ - events: [...ancestors, ...descendants], + events: [...ancestorEvents, ...descendantEvents], signal: c.req.raw.signal, store, }); + const [ancestors, descendants] = await Promise.all([ + renderStatuses(ancestorEvents), + renderStatuses(descendantEvents), + ]); + return c.json({ ancestors, descendants }); }