From 268b66a3e8dd0bc51b9df2ba96e57e3f53446a1e Mon Sep 17 00:00:00 2001 From: Florian <45694132+flo-bit@users.noreply.github.com> Date: Fri, 10 Apr 2026 22:41:09 +0200 Subject: [PATCH] commit --- src/lib/reddit/db.ts | 17 ++- src/lib/reddit/server/communities.remote.ts | 9 +- src/routes/communities/+page.svelte | 120 +++++++++++++++++--- src/routes/register/+page.svelte | 40 +++---- 4 files changed, 141 insertions(+), 45 deletions(-) diff --git a/src/lib/reddit/db.ts b/src/lib/reddit/db.ts index 9d6aae4..64fe097 100644 --- a/src/lib/reddit/db.ts +++ b/src/lib/reddit/db.ts @@ -45,14 +45,21 @@ export type PostWithCommunity = PostRow & { community_accent_color: string | null; }; -export async function listCommunities(db: D1Database): Promise { +export type CommunityListRow = CommunityRow & { post_count: number }; + +export async function listCommunities(db: D1Database): Promise { + // LEFT JOIN with a grouped subquery so communities with zero posts + // still appear (COALESCE to 0) without needing a second roundtrip. const res = await db .prepare( - `SELECT did, handle, pds, secret_key_ciphertext, secret_key_iv, public_jwk_json, thumbprint, display_name, avatar, description, accent_color, followers_count, created_at - FROM communities - ORDER BY followers_count DESC NULLS LAST, created_at DESC` + `SELECT c.did, c.handle, c.pds, c.secret_key_ciphertext, c.secret_key_iv, c.public_jwk_json, c.thumbprint, c.display_name, c.avatar, c.description, c.accent_color, c.followers_count, c.created_at, + COALESCE(pc.n, 0) AS post_count + FROM communities c + LEFT JOIN (SELECT community_did, COUNT(*) AS n FROM posts GROUP BY community_did) pc + ON pc.community_did = c.did + ORDER BY c.followers_count DESC NULLS LAST, c.created_at DESC` ) - .all(); + .all(); return res.results ?? []; } diff --git a/src/lib/reddit/server/communities.remote.ts b/src/lib/reddit/server/communities.remote.ts index f3d3157..6c87cdd 100644 --- a/src/lib/reddit/server/communities.remote.ts +++ b/src/lib/reddit/server/communities.remote.ts @@ -39,6 +39,9 @@ type PublicCommunity = Omit< > & { accentColor: AccentColor; followersCount: number; + /** Total cached submissions in this community. Populated by + * listCommunities; 0 for single-row reads that don't compute it. */ + postCount: number; }; /** PublicCommunity + viewer-specific access state. */ @@ -46,7 +49,7 @@ type CommunityWithAccess = PublicCommunity & { canSubmit: boolean; }; -function sanitize(row: CommunityRow): PublicCommunity { +function sanitize(row: CommunityRow & { post_count?: number }): PublicCommunity { /* eslint-disable @typescript-eslint/no-unused-vars */ const { secret_key_ciphertext, @@ -55,13 +58,15 @@ function sanitize(row: CommunityRow): PublicCommunity { thumbprint, accent_color, followers_count, + post_count, ...rest } = row; /* eslint-enable @typescript-eslint/no-unused-vars */ return { ...rest, accentColor: isAccentColor(accent_color) ? accent_color : DEFAULT_ACCENT_COLOR, - followersCount: followers_count ?? 0 + followersCount: followers_count ?? 0, + postCount: post_count ?? 0 }; } diff --git a/src/routes/communities/+page.svelte b/src/routes/communities/+page.svelte index 9f2a42c..3754c66 100644 --- a/src/routes/communities/+page.svelte +++ b/src/routes/communities/+page.svelte @@ -1,25 +1,94 @@