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-03-28 14:34:01 +00:00
|
|
|
import { HexKey, TaggedRawEvent, Lists, EventKind } from "@snort/nostr";
|
|
|
|
|
2023-02-10 11:12:11 +00:00
|
|
|
import { RootState } from "State/Store";
|
2023-03-28 14:34:01 +00:00
|
|
|
import { ParameterizedReplaceableNoteStore, RequestBuilder } from "System";
|
|
|
|
import useRequestBuilder from "Hooks/useRequestBuilder";
|
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-03-28 14:34:01 +00:00
|
|
|
const b = new RequestBuilder(`muted:${pubkey.slice(0, 12)}`);
|
|
|
|
b.withFilter().authors([pubkey]).kinds([EventKind.PubkeyLists]).tag("d", [Lists.Muted]);
|
|
|
|
return b;
|
2023-02-07 20:04:50 +00:00
|
|
|
}, [pubkey]);
|
2023-01-26 11:34:18 +00:00
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
const mutedFeed = useRequestBuilder<ParameterizedReplaceableNoteStore>(ParameterizedReplaceableNoteStore, sub);
|
2023-02-10 11:12:11 +00:00
|
|
|
|
|
|
|
const mutedList = useMemo(() => {
|
2023-03-28 14:34:01 +00:00
|
|
|
if (pubkey && mutedFeed.data) {
|
|
|
|
return getMuted(mutedFeed.data, pubkey);
|
2023-02-27 13:17:13 +00:00
|
|
|
}
|
|
|
|
return [];
|
2023-03-28 14:34:01 +00:00
|
|
|
}, [mutedFeed, 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
|
|
|
}
|
|
|
|
|
2023-03-28 14:34:01 +00:00
|
|
|
export function getMuted(feed: readonly TaggedRawEvent[], pubkey: HexKey): HexKey[] {
|
|
|
|
const lists = feed.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
|
|
|
}
|