impr: switch to refactored feed handler (#654)

* impr: use refactored feed handler for main & community

* refactor: re-add post preloading to feed handler

* fix: remove unused setting & improve consistency of postmedia compact
This commit is contained in:
xylight authored and GitHub committed 2025-09-12 02:18:32 +00:00
1 parent 2d01124bbf
commit 9460eec7e1
26 files changed
+489 -467

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
import { browser } from '$app/environment'
import type {
CommentView,
CommunityView,
GetComments,
GetCommunityResponse,
GetPersonDetails,
GetPersonDetailsResponse,
GetPost,
GetPosts,
PostView,
} from '$lib/client/types'
import { recursiveEqual } from '$lib/util.svelte'
import { SvelteMap } from 'svelte/reactivity'
type FetchFn<P, R> = (params: P) => R
export class Feed<Params, Response> {
#data = $state<Response>()
#fetch: FetchFn<Params, Response>
#lastParams?: Params
constructor(fetch: FetchFn<Params, Response>) {
this.#fetch = fetch
}
async load(params: Params) {
if (
this.#lastParams &&
params &&
!recursiveEqual<Params>(params, this.#lastParams)
)
this.#data = undefined
this.#lastParams = params
if (this.#data == null) this.#data = await this.#fetch(params)
return this.#data
}
peek() {
return this.#data
}
update(value: Response) {
this.#data = value
}
}
interface FeedTypes {
'/': [
GetPosts,
{
posts: PostView[]
next_page?: string
params: GetPosts
client: {
itemHeights?: (number | null)[]
lastSeen?: number
}
},
]
'/c/[name]': [
GetPosts,
{
posts: PostView[]
community: GetCommunityResponse
next_page?: string
params: GetPosts
client: { itemHeights?: (number | null)[]; lastSeen?: number }
},
]
'/u/[name]': [GetPersonDetails, { data: GetPersonDetailsResponse }]
'/post/[instance]/[id=integer]': [
{
posts: GetPost
comments: GetComments
preload?: PostView
},
{
post: PostView
comments: Promise<CommentView[]>
meta: Promise<{
community_view: CommunityView
cross_posts: PostView[]
post_view: PostView
}>
thread: {
showContext?: boolean
singleThread?: boolean
focus?: string
}
params: { posts: GetPost; comments: GetComments }
},
]
}
const feeds = new SvelteMap<keyof FeedTypes, Feed<any, any>>()
export function feed<Type extends keyof FeedTypes>(
id: Type,
init: (params: FeedTypes[Type][0]) => Promise<FeedTypes[Type][1]>,
): Feed<FeedTypes[Type][0], FeedTypes[Type][1]> {
const feed = feeds.get(id)
if (browser && feed) return feed
const feedData = new Feed<any, any>(init)
feeds.set(id, feedData)
return feedData
}
-107
View File
@@ -1,107 +0,0 @@
import { browser } from '$app/environment'
import { instance } from '$lib/instance.svelte'
import { client } from '$lib/client/lemmy.svelte'
import type {
GetPosts,
GetPostsResponse,
ListingType,
SortType,
} from '$lib/client/types'
export const shouldReload = (
cache: PostFeed | undefined,
url: URL,
instance: string,
): boolean =>
cache?.instance != instance || cache?.url.toString() != url.toString()
interface FetchPostFeed extends GetPosts {
sort: SortType
type_?: ListingType
}
export async function postFeed(args: {
id: PostFeedID
request: FetchPostFeed
url: URL
fetch?: (
input: RequestInfo | URL,
init?: RequestInit | undefined,
) => Promise<Response>
}) {
const feed = postFeeds.value[args.id]
const posts = shouldReload(feed, args.url, instance.data)
? await client({ func: args.fetch }).getPosts(args.request)
: feed.data.posts
if (shouldReload(feed, args.url, instance.data) && browser)
postFeeds.value[args.id] = {
id: args.id,
data: {
...args.request,
posts: posts,
cursor: {
next: posts.next_page,
},
type_: args.request.type_,
},
url: args.url,
lastSeen: 0,
instance: instance.data,
clientData: {
itemHeights: [],
},
}
const defaultFeed = $state({
...args.request,
posts: posts,
cursor: {
next: posts.next_page,
},
type_: args.request.type_,
})
return postFeeds.value[args.id]?.data ?? defaultFeed
}
export function getPostFeed(feeds: Map<PostFeedID, PostFeed>, id: PostFeedID) {
return feeds.get(id)
}
class PostFeedState {
#value: Record<PostFeedID, PostFeed> = $state({}) as Record<
PostFeedID,
PostFeed
>
get value() {
return this.#value
}
set value(value) {
this.#value = value
}
}
export type PostFeedID = 'main' | 'community' | 'profile' | 'votes'
export interface PostFeed {
data: GetPosts & {
posts: GetPostsResponse
cursor: {
next?: string
}
sort: SortType
}
url: URL
lastSeen: number
instance: string
id: PostFeedID
clientData: {
itemHeights: (number | null)[]
}
}
export const postFeeds = new PostFeedState()
-98
View File
@@ -1,98 +0,0 @@
import { publishedToDate } from '$lib/components/util/date.js'
import type {
CommentReportView,
CommentView,
Person,
PostReportView,
PostView,
PrivateMessage,
PrivateMessageReportView,
} from '$lib/client/types'
interface CommentReport {
type: 'comment'
item: CommentView
}
interface PostReport {
type: 'post'
item: PostView
}
interface PrivateMessageReport {
type: 'message'
item: PrivateMessage
reportee: Person
}
interface BaseReport {
creator: Person
reason: string
reportee?: Person
timestamp: Date
resolved: boolean
id: number
resolver?: Person
}
export const generalizeCommentReport = (
report: CommentReportView,
): ReportView => ({
creator: report.creator,
item: {
...report,
subscribed: 'NotSubscribed',
saved: false,
creator_blocked: false,
creator: report.comment_creator,
banned_from_community: false,
},
reason: report.comment_report.reason,
type: 'comment',
timestamp: publishedToDate(report.comment_report.published),
resolved: report.comment_report.resolved,
id: report.comment_report.id,
resolver: report.resolver,
})
export const generalizePostReport = (report: PostReportView): ReportView => ({
creator: report.creator,
item: {
...report,
unread_comments: 0,
saved: false,
creator_blocked: false,
read: false,
subscribed: 'NotSubscribed',
creator: report.post_creator,
banned_from_community: false,
},
reason: report.post_report.reason,
type: 'post',
timestamp: publishedToDate(report.post_report.published),
resolved: report.post_report.resolved,
id: report.post_report.id,
resolver: report.resolver,
})
export const generalizePrivateMessageReport = (
report: PrivateMessageReportView,
): ReportView => ({
creator: report.creator,
item: report.private_message,
reason: report.private_message_report.reason,
type: 'message',
reportee: report.private_message_creator,
timestamp: publishedToDate(report.private_message_report.published),
resolved: report.private_message_report.resolved,
id: report.private_message_report.id,
resolver: report.resolver,
})
export type Report =
| CommentReportView
| PostReportView
| PrivateMessageReportView
export type ReportView = BaseReport &
(CommentReport | PostReport | PrivateMessageReport)