2023-01-26 11:34:18 +00:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
2023-01-27 21:10:14 +00:00
|
|
|
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) {
|
2023-02-07 20:04:50 +00:00
|
|
|
const sub = useMemo(() => {
|
2023-02-07 19:47:57 +00:00
|
|
|
const sub = new Subscriptions();
|
2023-02-07 20:04:50 +00:00
|
|
|
sub.Id = `muted:${pubkey.slice(0, 12)}`;
|
|
|
|
sub.Kinds = new Set([EventKind.Lists]);
|
|
|
|
sub.Authors = new Set([pubkey]);
|
|
|
|
sub.DTags = new Set([Lists.Muted]);
|
|
|
|
sub.Limit = 1;
|
|
|
|
return sub;
|
|
|
|
}, [pubkey]);
|
2023-01-26 11:34:18 +00:00
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
return useSubscription(sub);
|
2023-01-26 11:34:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
export function getNewest(rawNotes: TaggedRawEvent[]) {
|
|
|
|
const notes = [...rawNotes];
|
|
|
|
notes.sort((a, b) => a.created_at - b.created_at);
|
|
|
|
if (notes.length > 0) {
|
|
|
|
return notes[0];
|
|
|
|
}
|
2023-01-27 21:10:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
export function getMutedKeys(rawNotes: TaggedRawEvent[]): {
|
|
|
|
createdAt: number;
|
|
|
|
keys: HexKey[];
|
|
|
|
} {
|
|
|
|
const newest = getNewest(rawNotes);
|
|
|
|
if (newest) {
|
|
|
|
const { created_at, tags } = newest;
|
|
|
|
const keys = tags.filter((t) => t[0] === "p").map((t) => t[1]);
|
|
|
|
return {
|
|
|
|
keys,
|
|
|
|
createdAt: created_at,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return { createdAt: 0, keys: [] };
|
2023-01-26 11:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getMuted(feed: NoteStore, pubkey: HexKey): HexKey[] {
|
2023-02-07 19:47:57 +00:00
|
|
|
const lists = feed?.notes.filter(
|
2023-02-07 20:04:50 +00:00
|
|
|
(a) => a.kind === EventKind.Lists && a.pubkey === pubkey
|
|
|
|
);
|
|
|
|
return getMutedKeys(lists).keys;
|
2023-01-26 11:34:18 +00:00
|
|
|
}
|