Merge pull request #628 from Xyphyn/post-feed-cache

impr: use posts from post feeds to preload
This commit is contained in:
Xylight authored and GitHub committed 2025-08-05 21:07:52 +00:00
commit 6d2e334730
4 files changed
+318 -236

No files matched your search

@@ -15,12 +15,12 @@
import { publishedToDate } from '$lib/components/util/date.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import { t } from '$lib/i18n/translations.js'
import { getClient } from '$lib/lemmy.svelte.js'
import { client } from '$lib/lemmy.svelte.js'
import { resumables } from '$lib/lemmy/item.js'
import { settings } from '$lib/settings.svelte.js'
import { isImage } from '$lib/ui/image.js'
import { Button, toast } from 'mono-svelte'
import { onMount } from 'svelte'
import { onMount, untrack } from 'svelte'
import {
ArrowLeft,
ArrowRight,
@@ -32,33 +32,51 @@
import { fly } from 'svelte/transition'
import CommentProvider from './CommentProvider.svelte'
import { ReactiveState } from '$lib/promise.svelte'
import { postFeeds } from '$lib/lemmy/postfeed.svelte'
let { data } = $props()
onMount(async () => {
if (
!(data.post.value.post_view.read && settings.markPostsAsRead) &&
profile.current?.jwt
) {
getClient().markPostAsRead({
read: settings.markPostsAsRead,
post_ids: [data.post.value.post_view.post.id],
})
}
onMount(() => {
resumables.add({
name: data.post.value.post_view.post.name,
type: 'post',
url: page.url.toString(),
avatar: data.post.value.post_view.post.thumbnail_url,
})
if (
!(data.post.value.post_view.read && settings.markPostsAsRead) &&
profile.current?.jwt
) {
client()
.markPostAsRead({
read: true,
post_ids: [data.post.value.post_view.post.id],
})
.then(() => (data.post.value.post_view.read = true))
}
})
$effect(() => {
data.post.value.meta.then(
res => (data.post.value.post_view = res.post_view),
)
})
$effect(() => {
if (data.cachedFeed && data.post.value.post_view) {
untrack(() => {
const { id, index } = data.cachedFeed!
postFeeds.value[id].data.posts.posts[index] = data.post.value.post_view
})
}
})
let loading = $state(false)
async function reloadComments() {
loading = true
data.comments.value = getClient().getComments({
data.comments.value = client().getComments({
page: 1,
limit: 25,
type_: 'All',
@@ -117,7 +135,11 @@
<PostMeta
community={data.post.value.post_view.community}
user={data.post.value.post_view.creator}
subscribed={data.post.value.community_view.subscribed}
subscribed={profile.current.user?.follows.find(
i => i.community.id == data.post.value.post_view.community.id,
)
? 'Subscribed'
: 'NotSubscribed'}
badges={{
deleted: data.post.value.post_view.post.deleted,
removed: data.post.value.post_view.post.removed,
@@ -171,34 +193,36 @@
})}
/>
</div>
{#if data.post.value.cross_posts?.length > 0}
{@const crossposts = data.post.value.cross_posts}
<Expandable
class="text-base mt-2 w-full cursor-pointer"
open={crossposts?.length <= 3}
>
{#snippet title()}
<div
class="flex items-center gap-1 w-full text-left text-base font-normal"
>
<span class="font-bold">{crossposts.length}</span>
{$t('routes.post.crosspostCount')}
<hr
class="flex-1 inline-block w-full border-slate-200 dark:border-zinc-800 mx-3"
/>
</div>
{/snippet}
<div
class="divide-y! divide-slate-200 dark:divide-zinc-800 flex flex-col"
{#await data.post.value.meta then meta}
{@const crossposts = meta.cross_posts}
{#if crossposts?.length > 0}
<Expandable
class="text-base mt-2 w-full cursor-pointer"
open={crossposts?.length <= 3}
>
{#key crossposts}
{#each crossposts as crosspost (crosspost.post.id)}
<Post view="compact" post={crosspost} />
{/each}
{/key}
</div>
</Expandable>
{/if}
{#snippet title()}
<div
class="flex items-center gap-1 w-full text-left text-base font-normal"
>
<span class="font-bold">{crossposts.length}</span>
{$t('routes.post.crosspostCount')}
<hr
class="flex-1 inline-block w-full border-slate-200 dark:border-zinc-800 mx-3"
/>
</div>
{/snippet}
<div
class="divide-y! divide-slate-200 dark:divide-zinc-800 flex flex-col"
>
{#key crossposts}
{#each crossposts as crosspost (crosspost.post.id)}
<Post view="compact" post={crosspost} />
{/each}
{/key}
</div>
</Expandable>
{/if}
{/await}
</article>
{#if data.thread.value.showContext || data.thread.value.singleThread}
<div
@@ -266,7 +290,7 @@
{@const comments = new ReactiveState(passedComments)}
<CommentProvider
comments={comments.value}
post={data.post.value}
post={data.post.value.post_view}
focus={data.thread.value.focus}
onupdate={reloadComments}
bind:sort={data.commentSort.value}
@@ -2,10 +2,26 @@ import { resolveRoute } from '$app/paths'
import { profile } from '$lib/auth.svelte.js'
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
import { client } from '$lib/lemmy.svelte.js'
import { postFeeds } from '$lib/lemmy/postfeed.svelte.js'
import { ReactiveState, awaitIfServer } from '$lib/promise.svelte.js'
import { settings } from '$lib/settings.svelte'
import { redirect } from '@sveltejs/kit'
import type { GetComments } from 'lemmy-js-client'
import type {
GetComments,
PostView,
CommunityModeratorView,
CommunityView,
} from 'lemmy-js-client'
interface PartialPost {
post_view: PostView
meta: Promise<{
community_view: CommunityView
moderators: Array<CommunityModeratorView>
cross_posts: Array<PostView>
post_view: PostView
}>
}
function buildContext(thread?: string) {
let parentId: number | undefined
@@ -27,6 +43,33 @@ function buildContext(thread?: string) {
return { parentId, showContext, max_depth, focus: thread?.split('.').at(-1) }
}
function findInFeed(
postId: number,
feedId: 'main' | 'community',
):
| {
post_view: PostView
feedData?: {
id: 'main' | 'community'
index: number
}
}
| undefined {
const index = postFeeds.value[feedId]?.data.posts.posts.findIndex(
i => i.post.id == postId,
)
if ((index ?? -1) == -1) return
return {
post_view: postFeeds.value[feedId]?.data.posts.posts[index],
feedData: {
id: feedId,
index: index,
},
}
}
export async function load({ params, url, fetch }) {
if (profile.current.instance != params.instance)
redirect(302, resolveRoute('/post/[instance]/[id]/confirm', params))
@@ -34,14 +77,19 @@ export async function load({ params, url, fetch }) {
// TODO use Lemmy profile default settings
const sort = settings?.defaultSort?.comments ?? 'Hot'
const { parentId, showContext, max_depth, focus } = buildContext(
const { parentId, showContext, focus } = buildContext(
url.searchParams.get('thread') || undefined,
)
const cachedPost =
findInFeed(Number(params.id), 'main') ??
findInFeed(Number(params.id), 'community')
const max_depth = (cachedPost?.post_view.counts.comments ?? 0) > 100 ? 1 : 3
const commentParams: GetComments = {
post_id: Number(params.id),
type_: 'All',
limit: 50,
page: 1,
max_depth: max_depth,
saved_only: false,
@@ -50,26 +98,34 @@ export async function load({ params, url, fetch }) {
}
const comments = client({ func: fetch }).getComments(commentParams)
const post = await client({ func: fetch }).getPost({
const post = client({ func: fetch }).getPost({
id: Number(params.id),
})
const partialPost: PartialPost = {
post_view: cachedPost ? cachedPost.post_view : (await post).post_view,
meta: post.then(i => ({
...i,
})),
}
return {
thread: new ReactiveState({
showContext: showContext,
singleThread: parentId != undefined,
focus,
}),
post: new ReactiveState(post),
post: new ReactiveState(partialPost),
commentSort: new ReactiveState(sort),
comments: new ReactiveState((await awaitIfServer(comments)).data),
slots: {
sidebar: {
component: CommunityCard,
props: {
community_view: post.community_view,
community_view: post.then(i => i.community_view),
},
},
},
cachedFeed: cachedPost?.feedData,
}
}
@@ -12,7 +12,7 @@
import type {
CommentSortType,
GetCommentsResponse,
GetPostResponse,
PostView,
} from 'lemmy-js-client'
import { Button, Option, Select } from 'mono-svelte'
import { onMount } from 'svelte'
@@ -28,7 +28,7 @@
} from 'svelte-hero-icons'
interface Props {
post: GetPostResponse
post: PostView
comments: GetCommentsResponse
sort?: CommentSortType
onupdate?: () => void
@@ -68,13 +68,12 @@
<Button
color="primary"
rounding="xl"
disabled={(post.post_view.post.locked ||
post.post_view.banned_from_community) &&
disabled={(post.post.locked || post.banned_from_community) &&
!(
profile.current?.user?.local_user_view.local_user.admin ||
profile.current?.user?.moderates
.map(c => c.community.id)
.includes(post.community_view.community.id)
.includes(post.community.id)
)}
onclick={() => (commenting = true)}
>
@@ -116,7 +115,7 @@
</EndPlaceholder>
{:else}
<CommentForm
postId={post.post_view.post.id}
postId={post.post.id}
oncomment={comment => {
comments.comments.unshift(comment.comment_view)
}}
@@ -157,15 +156,11 @@
</div>
{/if}
{#if virtualize}
<CommentListVirtualizer
post={post.post_view.post}
nodes={tree}
scrollTo={focus}
/>
<CommentListVirtualizer post={post.post} nodes={tree} scrollTo={focus} />
{:else}
<div class="divide-y divide-slate-200 dark:divide-zinc-800">
<div class="-mx-3 sm:-mx-6 px-3 sm:px-6">
<Comments nodes={tree} post={post.post_view.post} />
<Comments nodes={tree} post={post.post} />
</div>
</div>
{/if}