refactor(UserStore): move mute logic to separate function & create isMuted() function

This commit is contained in:
P. Reis 2024-05-10 10:31:34 -03:00
parent c85f31f63f
commit 0c0465f131
1 changed files with 16 additions and 6 deletions

View File

@ -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<DittoEvent[]> {
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<boolean> {
const mutedPubkeys = await this.getMutedPubkeys();
return mutedPubkeys.has(pubkey);
}
private async getMuteList(): Promise<DittoEvent | undefined> {
const [muteList] = await this.store.query([{ authors: [this.pubkey], kinds: [10000], limit: 1 }]);
return muteList;
}
private async getMutedPubkeys(): Promise<Set<string>> {
const mutedPubkeysEvent = await this.getMuteList();
if (!mutedPubkeysEvent) {
return new Set();
}
return getTagSet(mutedPubkeysEvent.tags, 'p');
}
}