refactor(UserStore): change fields to private & handle in case of muteList undefined

This commit is contained in:
P. Reis 2024-04-25 18:43:12 -03:00
parent 6c292b8da4
commit ddc84bf47a
1 changed files with 15 additions and 8 deletions

View File

@ -3,32 +3,39 @@ import { DittoEvent } from '@/interfaces/DittoEvent.ts';
import { getTagSet } from '@/tags.ts'; import { getTagSet } from '@/tags.ts';
export class UserStore implements NStore { export class UserStore implements NStore {
store: NStore; private store: NStore;
pubkey: string; private pubkey: string;
#muteList: Promise<DittoEvent>; private muteList: Promise<DittoEvent | undefined>;
constructor(pubkey: string, store: NStore) { constructor(pubkey: string, store: NStore) {
this.pubkey = pubkey; this.pubkey = pubkey;
this.store = store; this.store = store;
this.#muteList = this.#getMuteList(); this.muteList = this.getMuteList();
} }
async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise<void> { async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise<void> {
return await this.store.event(event, opts); return await this.store.event(event, opts);
} }
/** Query events that `pubkey` did not block */ /**
* Query events that `pubkey` did not block
* https://github.com/nostr-protocol/nips/blob/master/51.md#standard-lists
*/
async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise<DittoEvent[]> { async query(filters: NostrFilter[], opts: { signal?: AbortSignal; limit?: number } = {}): Promise<DittoEvent[]> {
const allEvents = await this.store.query(filters, opts); const allEvents = await this.store.query(filters, opts);
const blockedUsers = getTagSet((await this.#muteList).tags, 'p'); const mutedPubkeysEvent = await this.muteList;
if (!mutedPubkeysEvent) {
return allEvents;
}
const mutedPubkeys = getTagSet(mutedPubkeysEvent.tags, 'p');
return allEvents.filter((event) => { return allEvents.filter((event) => {
blockedUsers.has(event.pubkey) === false; mutedPubkeys.has(event.pubkey) === false;
}); });
} }
async #getMuteList(): Promise<DittoEvent> { private async getMuteList(): Promise<DittoEvent | undefined> {
const [muteList] = await this.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }], { const [muteList] = await this.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }], {
signal: AbortSignal.timeout(5000), signal: AbortSignal.timeout(5000),
limit: 1, limit: 1,