fix: duplicate posts in feeds

This commit is contained in:
James Xylight committed 2025-08-03 10:07:18 -07:00
1 parent c8def7c50b
commit f37b5592fd
2 files changed
+34 -18

No files matched your search

@@ -14,13 +14,13 @@
import { settings } from '$lib/settings.svelte.js' import { settings } from '$lib/settings.svelte.js'
import type { PostView } from 'lemmy-js-client' import type { PostView } from 'lemmy-js-client'
import { Button } from 'mono-svelte' import { Button } from 'mono-svelte'
import { onMount, untrack } from 'svelte' import { onDestroy, onMount, untrack } from 'svelte'
import { import {
ArchiveBox, ArchiveBox,
ArrowTopRightOnSquare,
ChevronDoubleUp, ChevronDoubleUp,
ExclamationTriangle, ExclamationTriangle,
Icon, Icon,
Plus,
} from 'svelte-hero-icons' } from 'svelte-hero-icons'
import InfiniteScroll from 'svelte-infinite-scroll' import InfiniteScroll from 'svelte-infinite-scroll'
@@ -49,14 +49,20 @@
let loading = $state(false) let loading = $state(false)
let hasMore = $state(true) let hasMore = $state(true)
const abortLoad = new AbortController()
async function loadMore() { async function loadMore() {
console.log(hasMore, loading)
if (!hasMore || loading) return if (!hasMore || loading) return
try { try {
loading = true loading = true
const newPosts = await client() console.log('fetching with cursor', feedData.cursor.next)
const newPosts = await client({
func: (input, init) =>
fetch(input, { ...init, signal: abortLoad.signal }),
})
.getPosts({ .getPosts({
page_cursor: feedData.cursor.next, page_cursor: feedData.cursor.next,
disliked_only: feedData.disliked_only, disliked_only: feedData.disliked_only,
@@ -78,6 +84,15 @@
hasMore = newPosts.posts.length != 0 hasMore = newPosts.posts.length != 0
let postIds: number[] = []
let duplicates: number[] = []
for (const post of newPosts.posts) {
if (postIds.includes(post.post.id)) duplicates.push(post.post.id)
else postIds.push(post.post.id)
}
console.log('are there duplicates?', duplicates)
feedData.cursor.next = newPosts.next_page feedData.cursor.next = newPosts.next_page
feedData.posts.posts.push(...newPosts.posts) feedData.posts.posts.push(...newPosts.posts)
@@ -150,6 +165,10 @@
}) })
let initialOffset = $derived(listEl?.offsetTop) let initialOffset = $derived(listEl?.offsetTop)
onDestroy(() => {
abortLoad?.abort()
})
</script> </script>
<ul class="flex flex-col list-none" bind:this={listEl}> <ul class="flex flex-col list-none" bind:this={listEl}>
@@ -158,14 +177,14 @@
<div class="h-full grid place-items-center"> <div class="h-full grid place-items-center">
<Placeholder <Placeholder
icon={ArchiveBox} icon={ArchiveBox}
title="No posts" title={$t('routes.frontpage.empty.title')}
description="There are no posts that match this filter." description={$t('routes.frontpage.empty.description')}
> >
<Button href="/communities"> <Button href="/communities" rounding="pill" color="primary">
{#snippet prefix()} {#snippet prefix()}
<Icon src={Plus} size="16" mini /> <Icon src={ArrowTopRightOnSquare} size="16" mini />
{/snippet} {/snippet}
<span>Follow some communities</span> {$t('nav.communities')}
</Button> </Button>
</Placeholder> </Placeholder>
</div> </div>
@@ -256,14 +275,7 @@
</EndPlaceholder> </EndPlaceholder>
</div> </div>
{/if} {/if}
<InfiniteScroll <InfiniteScroll window threshold={600} on:loadMore={loadMore} />
window
threshold={600}
on:loadMore={() => {
console.log('loadMore triggered')
loadMore()
}}
/>
{/if} {/if}
{@render children?.()} {@render children?.()}
</ul> </ul>
+5 -1
View File
@@ -213,7 +213,11 @@
"frontpage": { "frontpage": {
"title": "Frontpage", "title": "Frontpage",
"footer": "{{users:number}} active {{users; 1:user; default:users;}}", "footer": "{{users:number}} active {{users; 1:user; default:users;}}",
"endFeed": "You have reached the end of {{community_name; undefined:the feed.; default:{{community_name}}.}}" "endFeed": "You have reached the end of {{community_name; undefined:the feed.; default:{{community_name}}.}}",
"empty": {
"title": "No posts",
"description": "There are no posts that match this filter."
}
}, },
"modlog": { "modlog": {
"title": "Modlog", "title": "Modlog",