2023-01-26 11:34:18 +00:00
|
|
|
import { useMemo } from "react";
|
2023-02-10 11:12:11 +00:00
|
|
|
import { useSelector } from "react-redux";
|
2023-01-26 11:34:18 +00:00
|
|
|
|
2023-02-13 15:49:19 +00:00
|
|
|
import { getNewest } from "Util";
|
2023-02-11 20:05:46 +00:00
|
|
|
import { HexKey, TaggedRawEvent, Lists } from "@snort/nostr";
|
|
|
|
import { EventKind, Subscriptions } from "@snort/nostr";
|
2023-01-26 11:34:18 +00:00
|
|
|
import useSubscription, { NoteStore } from "Feed/Subscription";
|
2023-02-10 11:12:11 +00:00
|
|
|
import { RootState } from "State/Store";
|
2023-01-26 11:34:18 +00:00
|
|
|
|
2023-02-27 13:17:13 +00:00
|
|
|
export default function useMutedFeed(pubkey?: HexKey) {
|
2023-02-10 11:12:11 +00:00
|
|
|
const { publicKey, muted } = useSelector((s: RootState) => s.login);
|
|
|
|
const isMe = publicKey === pubkey;
|
|
|
|
|
2023-02-07 20:04:50 +00:00
|
|
|
const sub = useMemo(() => {
|
2023-02-27 13:17:13 +00:00
|
|
|
if (isMe || !pubkey) return null;
|
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)}`;
|
2023-02-13 15:49:19 +00:00
|
|
|
sub.Kinds = new Set([EventKind.PubkeyLists]);
|
2023-02-07 20:04:50 +00:00
|
|
|
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-10 11:12:11 +00:00
|
|
|
const mutedFeed = useSubscription(sub, { leaveOpen: false, cache: true });
|
|
|
|
|
|
|
|
const mutedList = useMemo(() => {
|
2023-02-27 13:17:13 +00:00
|
|
|
if (pubkey) {
|
|
|
|
return getMuted(mutedFeed.store, pubkey);
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}, [mutedFeed.store, pubkey]);
|
2023-02-10 11:12:11 +00:00
|
|
|
|
|
|
|
return isMe ? muted : mutedList;
|
2023-01-26 11:34:18 +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;
|
2023-02-09 12:26:54 +00:00
|
|
|
const keys = tags.filter(t => t[0] === "p").map(t => t[1]);
|
2023-02-07 20:04:50 +00:00
|
|
|
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-13 15:49:19 +00:00
|
|
|
const lists = feed?.notes.filter(a => a.kind === EventKind.PubkeyLists && a.pubkey === pubkey);
|
2023-02-07 20:04:50 +00:00
|
|
|
return getMutedKeys(lists).keys;
|
2023-01-26 11:34:18 +00:00
|
|
|
}
|