rm duplicate reposts from feed

This commit is contained in:
Martti Malmi 2023-08-24 22:14:52 +03:00
parent 3f26686f23
commit 23b92878af
2 changed files with 34 additions and 6 deletions

View File

@ -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<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] };
@ -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,
@ -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}

View File

@ -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";
const Feed = (props: FeedProps) => {
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<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) {
@ -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} />
);