From 5ed83460270770008fcad32086f5020df926e030 Mon Sep 17 00:00:00 2001 From: James Xylight Date: Fri, 12 Sep 2025 16:35:49 -0700 Subject: [PATCH] feat [1/2]: add topics & feeds listing --- src/lib/client/base.ts | 3 +- src/lib/client/piefed/piefed.ts | 33 +++++++ src/lib/client/piefed/rewrite.ts | 22 +++++ src/lib/client/types/schemas/GetPosts.ts | 2 + src/lib/client/types/schemas/schemas.ts | 56 ++++++++++++ src/lib/components/ui/Entity.svelte | 2 +- src/lib/components/ui/LabelStat.svelte | 2 +- .../components/ui/layout/CommonItem.svelte | 29 ++++++ .../components/ui/layout/CommonList.svelte | 22 +++-- src/lib/components/ui/layout/SearchBar.svelte | 4 +- src/lib/i18n/en.json | 5 ++ src/routes/communities/+page.ts | 47 +--------- src/routes/explore/+layout.svelte | 88 +++++++++++++++++++ src/routes/explore/+layout.ts | 25 ++++++ .../{ => explore}/communities/+page.svelte | 67 +------------- src/routes/explore/communities/+page.ts | 35 ++++++++ src/routes/explore/feeds/+page.svelte | 12 +++ src/routes/explore/feeds/+page.ts | 15 ++++ src/routes/explore/feeds/FeedItem.svelte | 42 +++++++++ src/routes/explore/topics/+page.svelte | 12 +++ src/routes/explore/topics/+page.ts | 15 ++++ src/routes/explore/topics/TopicItem.svelte | 12 +++ 22 files changed, 426 insertions(+), 124 deletions(-) create mode 100644 src/lib/components/ui/layout/CommonItem.svelte create mode 100644 src/routes/explore/+layout.svelte create mode 100644 src/routes/explore/+layout.ts rename src/routes/{ => explore}/communities/+page.svelte (61%) create mode 100644 src/routes/explore/communities/+page.ts create mode 100644 src/routes/explore/feeds/+page.svelte create mode 100644 src/routes/explore/feeds/+page.ts create mode 100644 src/routes/explore/feeds/FeedItem.svelte create mode 100644 src/routes/explore/topics/+page.svelte create mode 100644 src/routes/explore/topics/+page.ts create mode 100644 src/routes/explore/topics/TopicItem.svelte diff --git a/src/lib/client/base.ts b/src/lib/client/base.ts index 34a66b51..5942a6b9 100644 --- a/src/lib/client/base.ts +++ b/src/lib/client/base.ts @@ -223,8 +223,9 @@ export abstract class BaseClient { form: types.UploadImage, ): Promise abstract deleteImage(form: types.DeleteImage): Promise - abstract setFlair?(form: types.SetPersonFlair): Promise + abstract getFeeds?(form: types.GetFeeds): Promise + abstract getTopics?(form: types.GetTopics): Promise } export type NullableFnArg = T extends ( diff --git a/src/lib/client/piefed/piefed.ts b/src/lib/client/piefed/piefed.ts index a3db54fe..aa13c40e 100644 --- a/src/lib/client/piefed/piefed.ts +++ b/src/lib/client/piefed/piefed.ts @@ -20,6 +20,7 @@ import { toCommentView, toCommunity, toCommunityView, + toFeedView, toLocalSite, toMyUser, toPerson, @@ -29,6 +30,7 @@ import { toPostView, toPrivateMessageView, toSortType, + toTopicView, } from './rewrite' import type { paths } from './schema' import type { SubscribedType } from '../types' @@ -166,6 +168,7 @@ export class PiefedClient implements BaseClient { ).data! return { + ...response, posts: response.posts.map(toPostView), next_page: (Number(params.page_cursor ?? 1) + 1).toString(), } @@ -969,4 +972,34 @@ export class PiefedClient implements BaseClient { async deleteImage(): ReturnType { throw new Error('unsupported') } + + async getFeeds( + params: NullableFnArg[0], + ): NullableFnReturn { + const response = ( + await this.#client.GET('/api/alpha/feed/list', { + params: { query: params }, + }) + ).data! + + return { + ...response, + feeds: response.feeds.map(toFeedView), + } + } + + async getTopics( + params: NullableFnArg[0], + ): NullableFnReturn { + const response = ( + await this.#client.GET('/api/alpha/topic/list', { + params: { query: params }, + }) + ).data! + + return { + ...response, + topics: response.topics.map(toTopicView), + } + } } diff --git a/src/lib/client/piefed/rewrite.ts b/src/lib/client/piefed/rewrite.ts index 6a1f2f21..2467d3b9 100644 --- a/src/lib/client/piefed/rewrite.ts +++ b/src/lib/client/piefed/rewrite.ts @@ -7,6 +7,7 @@ import type { CommunityView, CreateComment, CreatePost, + FeedView, GetComments, GetPost, GetPosts, @@ -23,6 +24,7 @@ import type { Search, SortType, SubscribedType, + TopicView, } from '../types' import type { components } from './schema' @@ -207,6 +209,26 @@ export function toPersonMentionView( } } +export function toTopicView( + topicView: components['schemas']['TopicView'], +): TopicView { + return { + ...topicView, + communities: topicView.communities?.map(toCommunity), + children: topicView.children.map(toTopicView), + } +} + +export function toFeedView( + feedView: components['schemas']['FeedView'], +): FeedView { + return { + ...feedView, + communities: feedView.communities?.map(toCommunity), + children: feedView.children.map(toFeedView), + } +} + // Query calls export function fromGetPosts( getPosts: GetPosts, diff --git a/src/lib/client/types/schemas/GetPosts.ts b/src/lib/client/types/schemas/GetPosts.ts index c60808a0..3444b5a6 100644 --- a/src/lib/client/types/schemas/GetPosts.ts +++ b/src/lib/client/types/schemas/GetPosts.ts @@ -16,4 +16,6 @@ export interface GetPosts { show_read?: boolean show_nsfw?: boolean page_cursor?: PaginationCursor + feed_id?: string + topic_id?: string } diff --git a/src/lib/client/types/schemas/schemas.ts b/src/lib/client/types/schemas/schemas.ts index dbb628fd..dc23bad6 100644 --- a/src/lib/client/types/schemas/schemas.ts +++ b/src/lib/client/types/schemas/schemas.ts @@ -2001,3 +2001,59 @@ export interface CommunityFlair { blur_images: boolean ap_id: string | null } + +export interface GetFeeds { + include_communities?: boolean + mine_only?: boolean +} + +export interface FeedView { + actor_id: string + ap_domain: string + children: FeedView[] + communities?: Community[] + communities_count: number + id: number + is_instance_feed: boolean + local: boolean + name: string + nsfl: boolean + nsfw: boolean + owner: boolean + public: boolean + published: string + show_posts_from_children: boolean + subscribed: boolean + subscriptions_count: number + title: string + updated: string + user_id: number + banner?: string + description?: string + description_html?: string + icon?: string + parent_feed_id?: number +} + +export interface GetFeedsResponse { + feeds: FeedView[] +} + +export interface GetTopics { + include_communities: boolean +} + +export interface TopicView { + children: TopicView[] + communities?: Community[] + communities_count: number + id: number + name: string + show_posts_from_children: boolean + title: string + parent_topic_id?: number +} + +export interface GetTopicsResponse { + topics: TopicView[] +} diff --git a/src/lib/components/ui/Entity.svelte b/src/lib/components/ui/Entity.svelte index a95ccc3f..1652fef9 100644 --- a/src/lib/components/ui/Entity.svelte +++ b/src/lib/components/ui/Entity.svelte @@ -18,7 +18,7 @@
{#if customIcon}{@render customIcon()}{:else} - + {/if}
{name} diff --git a/src/lib/components/ui/LabelStat.svelte b/src/lib/components/ui/LabelStat.svelte index 35a1a287..64d0a6bc 100644 --- a/src/lib/components/ui/LabelStat.svelte +++ b/src/lib/components/ui/LabelStat.svelte @@ -3,7 +3,7 @@ interface Props { label: string - content: string + content: string | number formatted?: boolean labelClass?: string contentClass?: string diff --git a/src/lib/components/ui/layout/CommonItem.svelte b/src/lib/components/ui/layout/CommonItem.svelte new file mode 100644 index 00000000..1d10bade --- /dev/null +++ b/src/lib/components/ui/layout/CommonItem.svelte @@ -0,0 +1,29 @@ + + +
+ + +

+ {title} +

+

+ {detail} +

+
+ {@render children?.()} +
diff --git a/src/lib/components/ui/layout/CommonList.svelte b/src/lib/components/ui/layout/CommonList.svelte index eaf2bb7a..47d85183 100644 --- a/src/lib/components/ui/layout/CommonList.svelte +++ b/src/lib/components/ui/layout/CommonList.svelte @@ -70,23 +70,21 @@ &:hover { background-color: var(--color-slate-25); } + } - &:not(.xs) { - padding-block: calc(var(--spacing) * 3); - padding-inline: calc(var(--spacing) * 3.5); - } + ul > :global(li:not(.xs) > *) { + padding-block: calc(var(--spacing) * 3); + padding-inline: calc(var(--spacing) * 3.5); @media screen and (min-width: 40rem) { - &:not(.xs) { - padding-block: calc(var(--spacing) * 3); - padding-inline: calc(var(--spacing) * 4); - } + padding-block: calc(var(--spacing) * 3); + padding-inline: calc(var(--spacing) * 4); } + } - .xs { - padding-block: calc(var(--spacing) * 0.5); - padding-block: calc(var(--spacing) * 1); - } + ul > :global(li.xs > *) { + padding-block: calc(var(--spacing) * 0.5); + padding-block: calc(var(--spacing) * 1); } ul > :global(li).selected { diff --git a/src/lib/components/ui/layout/SearchBar.svelte b/src/lib/components/ui/layout/SearchBar.svelte index 4cff5f1d..a29cec09 100644 --- a/src/lib/components/ui/layout/SearchBar.svelte +++ b/src/lib/components/ui/layout/SearchBar.svelte @@ -1,5 +1,5 @@ + +{#if client().getTopics && client().getFeeds} + +{/if} + + + {$t('routes.explore.title')} + + +
+ {$t('routes.explore.title')} + {#snippet extended()} + {#if page.route.id == '/explore/communities'} +
+ + +
+ form?.requestSubmit()} + > + {#if !LINKED_INSTANCE_URL} + {@const instanceSet = new Set( + profile.meta.profiles.map((i) => i.instance), + )} + {#if instanceSet.size > 1} + + {#each instanceSet as instance} + + {/each} + {/if} + {/if} + + form?.requestSubmit()} + /> +
+ + {/if} + {/snippet} +
+ +{@render children?.()} diff --git a/src/routes/explore/+layout.ts b/src/routes/explore/+layout.ts new file mode 100644 index 00000000..14f29c2f --- /dev/null +++ b/src/routes/explore/+layout.ts @@ -0,0 +1,25 @@ +import type { ListingType, SortType } from '$lib/client/types' +import { LINKED_INSTANCE_URL } from '$lib/instance.svelte' + +export function load({ url }) { + const sort = (url.searchParams.get('sort') as SortType) || 'TopDay' + const page = Number(url.searchParams.get('page')) || 1 + const query = url.searchParams.get('q') || '' + + const typeParam = url.searchParams.get('type') + const typeInstance = typeParam?.split('instance-')[1] + + const type = ( + typeInstance + ? 'Local' + : typeParam || (LINKED_INSTANCE_URL ? 'Local' : 'All') + ) as ListingType + + return { + sort: sort, + page: page, + query: query, + type: type, + typeInstance: typeInstance, + } +} diff --git a/src/routes/communities/+page.svelte b/src/routes/explore/communities/+page.svelte similarity index 61% rename from src/routes/communities/+page.svelte rename to src/routes/explore/communities/+page.svelte index d83dc7bf..4d97a60a 100644 --- a/src/routes/communities/+page.svelte +++ b/src/routes/explore/communities/+page.svelte @@ -1,30 +1,20 @@ - { - if (e.target == document.body) searchElement?.focus() - }} -/> - - - {$t('routes.communities.title')} - - -
- {$t('routes.communities.title')} - {#snippet extended()} -
- - -
- form?.requestSubmit()} - > - {#if !LINKED_INSTANCE_URL} - {@const instanceSet = new Set( - profile.meta.profiles.map((i) => i.instance), - )} - {#if instanceSet.size > 1} - - {#each instanceSet as instance} - - {/each} - {/if} - {/if} - - form?.requestSubmit()} - /> -
- - {/snippet} -
- {#if navigating.to?.route.id == '/communities'}
{#each new Array(5) as _, index} diff --git a/src/routes/explore/communities/+page.ts b/src/routes/explore/communities/+page.ts new file mode 100644 index 00000000..984c5527 --- /dev/null +++ b/src/routes/explore/communities/+page.ts @@ -0,0 +1,35 @@ +import { client } from '$lib/client/lemmy.svelte' +import { ReactiveState } from '$lib/util.svelte' + +export async function load({ fetch, parent }) { + const { page, query, sort, type, typeInstance } = await parent() + + const communities = + query != '' + ? await client({ func: fetch, instanceURL: typeInstance }).search({ + limit: 40, + page: page, + sort: sort, + type_: 'Communities', + listing_type: type, + q: query, + }) + : await client({ + func: fetch, + instanceURL: typeInstance, + }).listCommunities({ + limit: 40, + page: page, + sort: sort, + type_: type, + show_nsfw: true, + }) + + return { + communities: new ReactiveState(communities.communities), + type: type, + sort: sort, + query: query, + page: page, + } +} diff --git a/src/routes/explore/feeds/+page.svelte b/src/routes/explore/feeds/+page.svelte new file mode 100644 index 00000000..c3d81137 --- /dev/null +++ b/src/routes/explore/feeds/+page.svelte @@ -0,0 +1,12 @@ + + + + {#snippet item(feed)} + + {/snippet} + diff --git a/src/routes/explore/feeds/+page.ts b/src/routes/explore/feeds/+page.ts new file mode 100644 index 00000000..e2ce6e18 --- /dev/null +++ b/src/routes/explore/feeds/+page.ts @@ -0,0 +1,15 @@ +import { client } from '$lib/client/lemmy.svelte.js' +import { ReactiveState } from '$lib/util.svelte' +import { error } from '@sveltejs/kit' + +export async function load({ fetch }) { + const piefed = client({ func: fetch }) + + if (!piefed.getFeeds) error(404, 'unsupported') + + const feeds = await piefed.getFeeds({ include_communities: false }) + + return { + feeds: new ReactiveState(feeds.feeds), + } +} diff --git a/src/routes/explore/feeds/FeedItem.svelte b/src/routes/explore/feeds/FeedItem.svelte new file mode 100644 index 00000000..011daffa --- /dev/null +++ b/src/routes/explore/feeds/FeedItem.svelte @@ -0,0 +1,42 @@ + + +{#snippet feedModal()} + + + +{/snippet} + + +