From d679cc37556f78c928ce3eef35eb3995da02c03a Mon Sep 17 00:00:00 2001 From: Kieran Date: Tue, 10 Jan 2023 12:05:36 +0000 Subject: [PATCH] Follows/Followers --- src/element/FollowListBase.js | 21 +++++++++++++++++++++ src/element/FollowersList.js | 16 ++++++---------- src/element/FollowsList.js | 16 ++++++++++++++++ src/feed/EventPublisher.js | 9 +++++++-- src/feed/FollowsFeed.js | 17 +++++++++++++++++ src/pages/ProfilePage.js | 5 ++++- 6 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 src/element/FollowListBase.js create mode 100644 src/element/FollowsList.js create mode 100644 src/feed/FollowsFeed.js diff --git a/src/element/FollowListBase.js b/src/element/FollowListBase.js new file mode 100644 index 00000000..312fb628 --- /dev/null +++ b/src/element/FollowListBase.js @@ -0,0 +1,21 @@ +import useEventPublisher from "../feed/EventPublisher"; +import ProfilePreview from "./ProfilePreview"; + +export default function FollowListBase({ pubkeys }) { + const publisher = useEventPublisher(); + + async function followAll() { + let ev = await publisher.addFollow(pubkeys); + publisher.broadcast(ev); + } + + return ( + <> +
+
+
followAll()}>Follow All
+
+ {pubkeys?.map(a => )} + + ) +} \ No newline at end of file diff --git a/src/element/FollowersList.js b/src/element/FollowersList.js index aac73278..dc6101a7 100644 --- a/src/element/FollowersList.js +++ b/src/element/FollowersList.js @@ -1,19 +1,15 @@ import { useMemo } from "react"; import useFollowersFeed from "../feed/FollowersFeed"; import EventKind from "../nostr/EventKind"; -import ProfilePreview from "./ProfilePreview"; +import FollowListBase from "./FollowListBase"; -export default function FollowersList(props) { - const feed = useFollowersFeed(props.pubkey); +export default function FollowersList({ pubkey }) { + const feed = useFollowersFeed(pubkey); - const pubKeys = useMemo(() => { - let contactLists = feed?.notes.filter(a => a.kind === EventKind.ContactList && a.tags.some(b => b[0] === "p" && b[1] === props.pubkey)); + const pubkeys = useMemo(() => { + let contactLists = feed?.notes.filter(a => a.kind === EventKind.ContactList && a.tags.some(b => b[0] === "p" && b[1] === pubkey)); return [...new Set(contactLists?.map(a => a.pubkey))]; }, [feed]); - return ( - <> - {pubKeys?.map(a => )} - - ) + return } \ No newline at end of file diff --git a/src/element/FollowsList.js b/src/element/FollowsList.js new file mode 100644 index 00000000..99dab381 --- /dev/null +++ b/src/element/FollowsList.js @@ -0,0 +1,16 @@ +import { useMemo } from "react"; +import useFollowsFeed from "../feed/FollowsFeed"; +import EventKind from "../nostr/EventKind"; +import FollowListBase from "./FollowListBase"; + +export default function FollowsList({ pubkey }) { + const feed = useFollowsFeed(pubkey); + + const pubkeys = useMemo(() => { + let contactLists = feed?.notes.filter(a => a.kind === EventKind.ContactList && a.pubkey === pubkey); + let pTags = contactLists?.map(a => a.tags.filter(b => b[0] === "p").map(c => c[1])); + return [...new Set(pTags?.flat())]; + }, [feed]); + + return +} \ No newline at end of file diff --git a/src/feed/EventPublisher.js b/src/feed/EventPublisher.js index 2f7a9fe8..12548481 100644 --- a/src/feed/EventPublisher.js +++ b/src/feed/EventPublisher.js @@ -103,10 +103,15 @@ export default function useEventPublisher() { let ev = Event.ForPubKey(pubKey); ev.Kind = EventKind.ContactList; ev.Content = JSON.stringify(relays); - for (let pk of follows) { + let temp = new Set(follows); + if (Array.isArray(pkAdd)) { + pkAdd.forEach(a => temp.add(a)); + } else { + temp.add(pkAdd); + } + for (let pk of temp) { ev.Tags.push(new Tag(["p", pk])); } - ev.Tags.push(new Tag(["p", pkAdd])); return await signEvent(ev); }, diff --git a/src/feed/FollowsFeed.js b/src/feed/FollowsFeed.js new file mode 100644 index 00000000..bccd248c --- /dev/null +++ b/src/feed/FollowsFeed.js @@ -0,0 +1,17 @@ +import { useMemo } from "react"; +import EventKind from "../nostr/EventKind"; +import { Subscriptions } from "../nostr/Subscriptions"; +import useSubscription from "./Subscription"; + +export default function useFollowsFeed(pubkey) { + const sub = useMemo(() => { + let x = new Subscriptions(); + x.Id = "follows"; + x.Kinds.add(EventKind.ContactList); + x.Authors.add(pubkey); + + return x; + }, [pubkey]); + + return useSubscription(sub); +} \ No newline at end of file diff --git a/src/pages/ProfilePage.js b/src/pages/ProfilePage.js index 7fcf3f69..8463e6d3 100644 --- a/src/pages/ProfilePage.js +++ b/src/pages/ProfilePage.js @@ -17,10 +17,11 @@ import Nip05 from "../element/Nip05"; import Copy from "../element/Copy"; import ProfilePreview from "../element/ProfilePreview"; import FollowersList from "../element/FollowersList"; +import FollowsList from "../element/FollowsList"; const ProfileTab = { Notes: 0, - Reactions: 1, + //Reactions: 1, Followers: 2, Follows: 3 }; @@ -84,6 +85,8 @@ export default function ProfilePage() { case ProfileTab.Follows: { if (isMe) { return follows.map(a => ) + } else { + return ; } } case ProfileTab.Followers: {