feat: add upvoted and downvoted listings
This commit is contained in:
1 parent
29bfa9104a
commit
7d2089be0e
4 files changed
+85
-31
No files matched your search
+4
-3
@@ -288,9 +288,10 @@ async function userFromJwt(
|
|||||||
if (!site) return
|
if (!site) return
|
||||||
|
|
||||||
if (!versionIsSupported(site.version, MINIMUM_VERSION)) {
|
if (!versionIsSupported(site.version, MINIMUM_VERSION)) {
|
||||||
throw new Error(
|
toast({
|
||||||
`This version of Photon only supports Lemmy instances with version ${MINIMUM_VERSION} or higher. This Lemmy instance is running: ${site.version}`
|
content: `This version of Photon only supports Lemmy instances with version ${MINIMUM_VERSION} or higher. This Lemmy instance is running: ${site.version}`,
|
||||||
)
|
type: 'error',
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const myUser = site.my_user
|
const myUser = site.my_user
|
||||||
|
|||||||
+37
-28
@@ -1,13 +1,18 @@
|
|||||||
import { browser } from "$app/environment"
|
import { browser } from '$app/environment'
|
||||||
import { instance } from "$lib/instance"
|
import { instance } from '$lib/instance'
|
||||||
import { client } from "$lib/lemmy"
|
import { client } from '$lib/lemmy'
|
||||||
import type { GetPosts, GetPostsResponse, ListingType, SortType } from "lemmy-js-client"
|
import type {
|
||||||
import { get, writable } from "svelte/store"
|
GetPosts,
|
||||||
|
GetPostsResponse,
|
||||||
|
ListingType,
|
||||||
|
SortType,
|
||||||
|
} from 'lemmy-js-client'
|
||||||
|
import { get, writable } from 'svelte/store'
|
||||||
|
|
||||||
export const shouldReload = (
|
export const shouldReload = (
|
||||||
cache: PostFeed | undefined,
|
cache: PostFeed | undefined,
|
||||||
url: URL,
|
url: URL,
|
||||||
instance: string,
|
instance: string
|
||||||
): boolean =>
|
): boolean =>
|
||||||
cache?.instance != instance || cache?.url.toString() != url.toString()
|
cache?.instance != instance || cache?.url.toString() != url.toString()
|
||||||
|
|
||||||
@@ -18,27 +23,28 @@ interface FetchPostFeed extends GetPosts {
|
|||||||
|
|
||||||
export async function postFeed(args: {
|
export async function postFeed(args: {
|
||||||
id: PostFeedID
|
id: PostFeedID
|
||||||
request: FetchPostFeed,
|
request: FetchPostFeed
|
||||||
url: URL
|
url: URL
|
||||||
|
fetch?: (
|
||||||
|
input: RequestInfo | URL,
|
||||||
|
init?: RequestInit | undefined
|
||||||
|
) => Promise<Response>
|
||||||
}) {
|
}) {
|
||||||
const feed = get(postFeeds)[args.id]
|
const feed = get(postFeeds)[args.id]
|
||||||
|
|
||||||
const posts = shouldReload(feed, args.url, get(instance))
|
const posts = shouldReload(feed, args.url, get(instance))
|
||||||
? await client().getPosts(args.request)
|
? await client({ func: fetch }).getPosts(args.request)
|
||||||
: feed.data.posts
|
: feed.data.posts
|
||||||
|
|
||||||
if (
|
if (shouldReload(feed, args.url, get(instance)) && browser)
|
||||||
shouldReload(feed, args.url, get(instance)) &&
|
|
||||||
browser
|
|
||||||
)
|
|
||||||
postFeeds.updateFeed(args.id, {
|
postFeeds.updateFeed(args.id, {
|
||||||
data: {
|
data: {
|
||||||
...args.request,
|
...args.request,
|
||||||
posts: posts,
|
posts: posts,
|
||||||
cursor: {
|
cursor: {
|
||||||
next: posts.next_page
|
next: posts.next_page,
|
||||||
},
|
},
|
||||||
type_: args.request.type_
|
type_: args.request.type_,
|
||||||
},
|
},
|
||||||
url: args.url,
|
url: args.url,
|
||||||
lastSeen: 0,
|
lastSeen: 0,
|
||||||
@@ -54,35 +60,38 @@ export function getPostFeed(feeds: Map<PostFeedID, PostFeed>, id: PostFeedID) {
|
|||||||
|
|
||||||
function createPostFeedsStore() {
|
function createPostFeedsStore() {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const { subscribe, update, set } = writable<Record<PostFeedID, PostFeed>>({});
|
const { subscribe, update, set } = writable<Record<PostFeedID, PostFeed>>({})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
subscribe,
|
subscribe,
|
||||||
addFeed: (id: PostFeedID, feed: PostFeed) => update(feeds => ({ ...feeds, [id]: feed })),
|
addFeed: (id: PostFeedID, feed: PostFeed) =>
|
||||||
updateFeed: (id: PostFeedID, feed: Partial<PostFeed>) => update(feeds => {
|
update((feeds) => ({ ...feeds, [id]: feed })),
|
||||||
|
updateFeed: (id: PostFeedID, feed: Partial<PostFeed>) =>
|
||||||
|
update((feeds) => {
|
||||||
return {
|
return {
|
||||||
...feeds,
|
...feeds,
|
||||||
[id]: { ...feeds[id], ...feed } as PostFeed
|
[id]: { ...feeds[id], ...feed } as PostFeed,
|
||||||
};
|
}
|
||||||
}),
|
}),
|
||||||
removeFeed: (id: PostFeedID) => update(feeds => {
|
removeFeed: (id: PostFeedID) =>
|
||||||
const { [id]: _, ...rest } = feeds;
|
update((feeds) => {
|
||||||
return rest as Record<PostFeedID, PostFeed>;
|
const { [id]: _, ...rest } = feeds
|
||||||
|
return rest as Record<PostFeedID, PostFeed>
|
||||||
}),
|
}),
|
||||||
clearAll: () => set({} as Record<PostFeedID, PostFeed>),
|
clearAll: () => set({} as Record<PostFeedID, PostFeed>),
|
||||||
set
|
set,
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PostFeedID = 'main' | 'community' | 'profile'
|
export type PostFeedID = 'main' | 'community' | 'profile' | 'votes'
|
||||||
|
|
||||||
export interface PostFeed {
|
export interface PostFeed {
|
||||||
data: GetPosts & {
|
data: GetPosts & {
|
||||||
posts: GetPostsResponse,
|
posts: GetPostsResponse
|
||||||
cursor: {
|
cursor: {
|
||||||
next?: string
|
next?: string
|
||||||
},
|
}
|
||||||
sort: SortType,
|
sort: SortType
|
||||||
}
|
}
|
||||||
url: URL
|
url: URL
|
||||||
lastSeen: number
|
lastSeen: number
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { browser } from '$app/environment'
|
||||||
|
import PostFeed from '$lib/components/lemmy/post/feed/PostFeed.svelte'
|
||||||
|
import VirtualFeed from '$lib/components/lemmy/post/feed/VirtualFeed.svelte'
|
||||||
|
import Header from '$lib/components/ui/layout/pages/Header.svelte'
|
||||||
|
import { t } from '$lib/translations'
|
||||||
|
|
||||||
|
export let data
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Header>
|
||||||
|
{data.upvoted ? $t('routes.profile.upvoted') : $t('routes.profile.downvoted')}
|
||||||
|
</Header>
|
||||||
|
|
||||||
|
<svelte:component
|
||||||
|
this={browser ? VirtualFeed : PostFeed}
|
||||||
|
posts={data.feed.posts.posts}
|
||||||
|
bind:feedData={data.feed}
|
||||||
|
feedId="main"
|
||||||
|
/>
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { client } from '$lib/lemmy.js'
|
||||||
|
import { postFeed } from '$lib/lemmy/postfeed.js'
|
||||||
|
import { error } from '@sveltejs/kit'
|
||||||
|
|
||||||
|
export async function load({ url, params, fetch }) {
|
||||||
|
if (params.type.toLowerCase() != 'up' && params.type.toLowerCase() != 'down')
|
||||||
|
error(404)
|
||||||
|
|
||||||
|
const upvoted = params.type.toLowerCase() == 'up'
|
||||||
|
|
||||||
|
return {
|
||||||
|
feed: await postFeed({
|
||||||
|
id: 'votes',
|
||||||
|
request: {
|
||||||
|
sort: 'New',
|
||||||
|
liked_only: upvoted || undefined,
|
||||||
|
disliked_only: !upvoted || undefined,
|
||||||
|
},
|
||||||
|
url: url,
|
||||||
|
fetch: fetch,
|
||||||
|
}),
|
||||||
|
upvoted: upvoted,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user