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; + } +}