From 0c0465f131a8c1a92f083d26f5d964cc31080708 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Fri, 10 May 2024 10:31:34 -0300 Subject: [PATCH] refactor(UserStore): move mute logic to separate function & create isMuted() function --- src/storages/UserStore.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/storages/UserStore.ts b/src/storages/UserStore.ts index a3f0726c..b13df420 100644 --- a/src/storages/UserStore.ts +++ b/src/storages/UserStore.ts @@ -1,4 +1,5 @@ import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify'; + import { DittoEvent } from '@/interfaces/DittoEvent.ts'; import { getTagSet } from '@/tags.ts'; @@ -16,25 +17,34 @@ export class UserStore implements NStore { } /** - * Query events that `pubkey` did not block + * Query events that `pubkey` did not mute * https://github.com/nostr-protocol/nips/blob/master/51.md#standard-lists */ async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise { const allEvents = await this.store.query(filters, opts); - const mutedPubkeysEvent = await this.getMuteList(); - if (!mutedPubkeysEvent) { - return allEvents; - } - const mutedPubkeys = getTagSet(mutedPubkeysEvent.tags, 'p'); + const mutedPubkeys = await this.getMutedPubkeys(); return allEvents.filter((event) => { return event.kind === 0 || mutedPubkeys.has(event.pubkey) === false; }); } + async isMuted(pubkey: string): Promise { + const mutedPubkeys = await this.getMutedPubkeys(); + return mutedPubkeys.has(pubkey); + } + private async getMuteList(): Promise { const [muteList] = await this.store.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }]); return muteList; } + + private async getMutedPubkeys(): Promise> { + const mutedPubkeysEvent = await this.getMuteList(); + if (!mutedPubkeysEvent) { + return new Set(); + } + return getTagSet(mutedPubkeysEvent.tags, 'p'); + } }