snort/packages/app/src/Feed/MuteList.ts

54 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-26 11:34:18 +00:00
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { HexKey, TaggedRawEvent, Lists, EventKind } from "@snort/nostr";
2023-04-14 11:33:19 +00:00
import { getNewest } from "Util";
2023-03-28 14:34:01 +00:00
import { ParameterizedReplaceableNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
2023-01-26 11:34:18 +00:00
2023-02-27 13:17:13 +00:00
export default function useMutedFeed(pubkey?: HexKey) {
2023-04-14 11:33:19 +00:00
const { publicKey, muted } = useLogin();
2023-02-10 11:12:11 +00:00
const isMe = publicKey === pubkey;
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;
}, [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
2023-04-14 11:33:19 +00:00
return isMe ? muted.item : mutedList;
2023-01-26 11:34:18 +00:00
}
export function getMutedKeys(rawNotes: TaggedRawEvent[]): {
createdAt: number;
keys: HexKey[];
2023-04-14 11:33:19 +00:00
raw?: TaggedRawEvent;
} {
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]);
return {
2023-04-14 11:33:19 +00:00
raw: newest,
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);
return getMutedKeys(lists).keys;
2023-01-26 11:34:18 +00:00
}