ditto/src/storages/UserStore.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-04-25 19:41:40 +00:00
import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify';
2024-04-25 19:41:40 +00:00
import { DittoEvent } from '@/interfaces/DittoEvent.ts';
2024-05-21 18:08:08 +00:00
import { getTagSet } from '@/utils/tags.ts';
2024-04-25 19:41:40 +00:00
export class UserStore implements NStore {
2024-05-14 23:23:41 +00:00
constructor(private pubkey: string, private store: NStore) {}
2024-04-25 19:41:40 +00:00
async event(event: NostrEvent, opts?: { signal?: AbortSignal }): Promise<void> {
return await this.store.event(event, opts);
}
/**
* Query events that `pubkey` did not mute
* 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[]> {
2024-05-14 23:23:41 +00:00
const events = await this.store.query(filters, opts);
const pubkeys = await this.getMutedPubkeys();
2024-04-25 19:41:40 +00:00
2024-05-14 23:23:41 +00:00
return events.filter((event) => {
return event.kind === 0 || !pubkeys.has(event.pubkey);
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;
}
private async getMutedPubkeys(): Promise<Set<string>> {
const mutedPubkeysEvent = await this.getMuteList();
if (!mutedPubkeysEvent) {
return new Set();
}
return getTagSet(mutedPubkeysEvent.tags, 'p');
}
2024-04-25 19:41:40 +00:00
}