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

34 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-10 12:05:36 +00:00
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { HexKey, TaggedRawEvent, EventKind } from "@snort/nostr";
2023-02-10 11:12:11 +00:00
2023-03-28 14:34:01 +00:00
import { PubkeyReplaceableNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-04-14 11:33:19 +00:00
import useLogin from "Hooks/useLogin";
2023-01-10 12:05:36 +00:00
2023-02-27 13:17:13 +00:00
export default function useFollowsFeed(pubkey?: HexKey) {
2023-04-14 11:33:19 +00:00
const { publicKey, follows } = 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(`follows:${pubkey.slice(0, 12)}`);
b.withFilter().kinds([EventKind.ContactList]).authors([pubkey]);
return b;
2023-02-10 11:12:11 +00:00
}, [isMe, pubkey]);
2023-01-10 12:05:36 +00:00
2023-03-28 14:34:01 +00:00
const contactFeed = useRequestBuilder<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
2023-02-24 19:28:29 +00:00
return useMemo(() => {
if (isMe) {
2023-04-14 11:33:19 +00:00
return follows.item;
2023-02-24 19:28:29 +00:00
}
2023-02-27 13:17:13 +00:00
2023-03-28 14:34:01 +00:00
return getFollowing(contactFeed.data ?? [], pubkey);
}, [contactFeed, follows, pubkey]);
2023-01-18 03:26:42 +00:00
}
2023-03-28 14:34:01 +00:00
export function getFollowing(notes: readonly TaggedRawEvent[], pubkey?: HexKey) {
2023-02-10 11:12:11 +00:00
const contactLists = notes.filter(a => a.kind === EventKind.ContactList && a.pubkey === pubkey);
2023-02-09 12:26:54 +00:00
const pTags = contactLists?.map(a => a.tags.filter(b => b[0] === "p").map(c => c[1]));
return [...new Set(pTags?.flat())];
2023-01-18 03:26:42 +00:00
}