Skip to content

Commit

Permalink
rm duplicate reposts from feed
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Aug 24, 2023
1 parent 3f26686 commit 23b9287
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/js/components/events/note/Note.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,7 +38,7 @@ const Note: React.FC<NoteProps> = ({
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] };
Expand All @@ -47,7 +47,7 @@ const Note: React.FC<NoteProps> = ({
}
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,
Expand Down Expand Up @@ -150,11 +150,11 @@ const Note: React.FC<NoteProps> = ({
/>
</div>
</div>
<Show when={!(isQuote || asInlineQuote)}>
<Show when={!(computedIsQuote || asInlineQuote)}>
<hr className="opacity-10" />
</Show>
<InfiniteScroll>
{replies.reverse().map((r) => (
{replies.slice(0, showReplies).map((r) => (
<EventComponent
key={r.id}
id={r.id}
Expand Down
30 changes: 29 additions & 1 deletion src/js/components/feed/Feed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import InfiniteScroll from '@/components/helpers/InfiniteScroll';
import Show from '@/components/helpers/Show';
import useSubscribe from '@/nostr/hooks/useSubscribe';
import Key from '@/nostr/Key';
import { isRepost } from '@/nostr/utils.ts';
import useHistoryState from '@/state/useHistoryState.ts';
import useLocalState from '@/state/useLocalState.ts';
import Helpers from '@/utils/Helpers';

import { translate as t } from '../../translations/Translation.mjs';
import {useMemo} from "preact/hooks";

Check failure on line 19 in src/js/components/feed/Feed.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `useMemo}·from·"preact/hooks"` with `·useMemo·}·from·'preact/hooks'`

const Feed = (props: FeedProps) => {
const fetchEvents = props.fetchEvents || useSubscribe;
Expand All @@ -41,6 +43,7 @@ const Feed = (props: FeedProps) => {
if (mutedUsers[event.pubkey]) {
return false;
}

if (filterOption.filterFn) {
return filterOption.filterFn(event);
}
Expand All @@ -57,6 +60,27 @@ const Feed = (props: FeedProps) => {
mergeSubscriptions: false,
});

const hiddenEvents = useMemo(() => {
const hiddenEvents = new Set<string>();
const seenReposts = new Set<string>();
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) {
Expand Down Expand Up @@ -116,10 +140,14 @@ const Feed = (props: FeedProps) => {
<Show when={displayAs === 'feed'}>
<InfiniteScroll key={`${infiniteScrollKeyString}feed`} loadMore={loadMore}>
{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 (
<EventComponent key={`${event.id}EC`} id={event.id} {...filterOption.eventProps} />
);
Expand Down

0 comments on commit 23b9287

Please sign in to comment.