From 6c292b8da47f1b606a47798573b0e026ddcc1f00 Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 25 Apr 2024 16:41:40 -0300 Subject: [PATCH] feat: create UserStore class --- src/storages/UserStore.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/storages/UserStore.ts diff --git a/src/storages/UserStore.ts b/src/storages/UserStore.ts new file mode 100644 index 0000000..02a3b03 --- /dev/null +++ b/src/storages/UserStore.ts @@ -0,0 +1,38 @@ +import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify'; +import { DittoEvent } from '@/interfaces/DittoEvent.ts'; +import { getTagSet } from '@/tags.ts'; + +export class UserStore implements NStore { + store: NStore; + pubkey: string; + #muteList: Promise; + + constructor(pubkey: string, store: NStore) { + this.pubkey = pubkey; + this.store = store; + this.#muteList = this.#getMuteList(); + } + + async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise { + return await this.store.event(event, opts); + } + + /** Query events that `pubkey` did not block */ + async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise { + const allEvents = await this.store.query(filters, opts); + + const blockedUsers = getTagSet((await this.#muteList).tags, 'p'); + + return allEvents.filter((event) => { + blockedUsers.has(event.pubkey) === false; + }); + } + + async #getMuteList(): Promise { + const [muteList] = await this.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }], { + signal: AbortSignal.timeout(5000), + limit: 1, + }); + return muteList; + } +}