From 23b92878af5bf49c5b13e4bbe910d8c3bca3e7a7 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Thu, 24 Aug 2023 22:14:52 +0300 Subject: [PATCH] rm duplicate reposts from feed --- src/js/components/events/note/Note.tsx | 10 ++++----- src/js/components/feed/Feed.tsx | 30 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/js/components/events/note/Note.tsx b/src/js/components/events/note/Note.tsx index 3a3608ffc..0982552cc 100644 --- a/src/js/components/events/note/Note.tsx +++ b/src/js/components/events/note/Note.tsx @@ -5,7 +5,7 @@ import { Link, route } from 'preact-router'; import InfiniteScroll from '@/components/helpers/InfiniteScroll'; import useSubscribe from '@/nostr/hooks/useSubscribe.ts'; -import { getEventReplyingTo, getEventRoot } from '@/nostr/utils'; +import { getNoteReplyingTo, getEventRoot } from '@/nostr/utils'; import Key from '../../../nostr/Key'; import { translate as t } from '../../../translations/Translation.mjs'; @@ -38,7 +38,7 @@ const Note: React.FC = ({ isQuote = false, isQuoting = false, }) => { - const replyingTo = useMemo(() => getEventReplyingTo(event), [event.id]); + const replyingTo = useMemo(() => getNoteReplyingTo(event), [event.id]); const repliesFilter = useMemo(() => { const filter: Filter = { '#e': [event.id], kinds: [1] }; @@ -47,7 +47,7 @@ const Note: React.FC = ({ } return filter; }, [event.id, showReplies]); - const repliesFilterFn = useCallback((e) => getEventReplyingTo(e) === event.id, [event.id]); + const repliesFilterFn = useCallback((e) => getNoteReplyingTo(e) === event.id, [event.id]); const { events: replies } = useSubscribe({ filter: repliesFilter, @@ -150,11 +150,11 @@ const Note: React.FC = ({ /> - +
- {replies.reverse().map((r) => ( + {replies.slice(0, showReplies).map((r) => ( { const fetchEvents = props.fetchEvents || useSubscribe; @@ -41,6 +43,7 @@ const Feed = (props: FeedProps) => { if (mutedUsers[event.pubkey]) { return false; } + if (filterOption.filterFn) { return filterOption.filterFn(event); } @@ -57,6 +60,27 @@ const Feed = (props: FeedProps) => { mergeSubscriptions: false, }); + const hiddenEvents = useMemo(() => { + const hiddenEvents = new Set(); + const seenReposts = new Set(); + for (const event of events) { + if (isRepost(event)) { + for (const tag of event.tags) { + if (tag[0] === 'e') { + if (seenReposts.has(tag[1])) { + hiddenEvents.add(event.id); + continue; + } + seenReposts.add(tag[1]); + } + } + } else if (seenReposts.has(event.id)) { + hiddenEvents.add(event.id); + } + } + return hiddenEvents; + }, [events]); + // TODO [shownEvents, setShownEvents] = useHistoryState([], 'shownEvents'); which is only updated when user clicks if (events.length && Key.isMine(events[0].pubkey) && events[0].created_at > showUntil) { @@ -116,10 +140,14 @@ const Feed = (props: FeedProps) => { {events.map((event) => { - // is this inefficient? should we rather pass a component function + list of events? if (event.created_at > showUntil) { return null; } + + if (hiddenEvents.has(event.id)) { + return null; + } + return ( );