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 {
|
2024-04-25 21:43:12 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-04-25 21:43:12 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
2024-04-25 21:43:12 +00:00
|
|
|
if (!mutedPubkeysEvent) {
|
|
|
|
return allEvents;
|
|
|
|
}
|
|
|
|
const mutedPubkeys = getTagSet(mutedPubkeysEvent.tags, 'p');
|
2024-04-25 19:41:40 +00:00
|
|
|
|
|
|
|
return allEvents.filter((event) => {
|
2024-05-01 00:28:45 +00:00
|
|
|
return event.kind === 0 || mutedPubkeys.has(event.pubkey) === false;
|
2024-04-25 19:41:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-25 21:43:12 +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;
|
|
|
|
}
|
|
|
|
}
|