ditto/src/storages/UserStore.ts

41 lines
1.3 KiB
TypeScript
Raw Normal View History

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