snort/src/Feed/MuteList.ts

47 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-01-26 11:34:18 +00:00
import { useMemo } from "react";
import { HexKey, TaggedRawEvent, Lists } from "Nostr";
2023-01-26 11:34:18 +00:00
import EventKind from "Nostr/EventKind";
import { Subscriptions } from "Nostr/Subscriptions";
import useSubscription, { NoteStore } from "Feed/Subscription";
export default function useMutedFeed(pubkey: HexKey) {
const sub = useMemo(() => {
let sub = new Subscriptions();
sub.Id = `muted:${pubkey}`;
sub.Kinds = new Set([EventKind.Lists]);
sub.Authors = new Set([pubkey]);
sub.DTag = Lists.Muted;
2023-01-26 11:34:18 +00:00
sub.Limit = 1;
return sub;
}, [pubkey]);
return useSubscription(sub);
}
export function getNewest(rawNotes: TaggedRawEvent[]){
2023-01-26 11:34:18 +00:00
const notes = [...rawNotes]
notes.sort((a, b) => a.created_at - b.created_at)
if (notes.length > 0) {
return notes[0]
}
}
export function getMutedKeys(rawNotes: TaggedRawEvent[]): { createdAt: number, keys: HexKey[] } {
const newest = getNewest(rawNotes)
2023-01-26 11:34:18 +00:00
if (newest) {
const { created_at, tags } = newest
const keys = tags.filter(t => t[0] === "p").map(t => t[1])
return {
keys,
createdAt: created_at,
}
2023-01-26 11:34:18 +00:00
}
return { createdAt: 0, keys: [] }
2023-01-26 11:34:18 +00:00
}
export function getMuted(feed: NoteStore, pubkey: HexKey): HexKey[] {
let lists = feed?.notes.filter(a => a.kind === EventKind.Lists && a.pubkey === pubkey);
return getMutedKeys(lists).keys;
}