Render discussion listings and the room overview on the server
This commit is contained in:
parent
c23428f904
commit
2041f6b899
10 changed files with 175 additions and 11 deletions
20
src/routes/+page.ts
Normal file
20
src/routes/+page.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { MODE, GROUP_ID } from "$lib/config";
|
||||
import {
|
||||
snapshot,
|
||||
type OverviewSnapshot,
|
||||
type ThreadsSnapshot,
|
||||
} from "$lib/forum/snapshot";
|
||||
|
||||
// Simple mode: the single group's listing. Full mode: the room overview.
|
||||
export async function load({ fetch, url }) {
|
||||
if (MODE === "simple") {
|
||||
const sort = url.searchParams.get("sort") === "new" ? "?sort=new" : "";
|
||||
const threads = await snapshot<ThreadsSnapshot>(
|
||||
fetch,
|
||||
`/api/threads/${GROUP_ID}${sort}`,
|
||||
);
|
||||
return { threads, profiles: threads?.profiles ?? null };
|
||||
}
|
||||
const overview = await snapshot<OverviewSnapshot>(fetch, "/api/overview");
|
||||
return { overview, profiles: overview?.profiles ?? null };
|
||||
}
|
||||
20
src/routes/api/overview/+server.ts
Normal file
20
src/routes/api/overview/+server.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import { json } from "@sveltejs/kit";
|
||||
import { MODE, GROUP_ID } from "$lib/config";
|
||||
import { fetchGroups } from "$lib/forum/groups";
|
||||
import { fetchOverview, overviewPubkeys } from "$lib/forum/overview";
|
||||
import { fetchProfiles } from "$lib/forum/profiles";
|
||||
import type { OverviewSnapshot } from "$lib/forum/snapshot";
|
||||
import { forumQuery, profileQuery } from "$lib/ssr/relay";
|
||||
|
||||
// Room activity and the latest discussions: every room in full mode, the
|
||||
// single group in simple mode (mirrors LatestDiscussions' own room set).
|
||||
export async function GET() {
|
||||
const roomIds =
|
||||
MODE === "full"
|
||||
? (await fetchGroups(forumQuery)).map((g) => g.id)
|
||||
: [GROUP_ID];
|
||||
const overview = await fetchOverview(forumQuery, roomIds);
|
||||
const profiles = await fetchProfiles(profileQuery, overviewPubkeys(overview));
|
||||
const body: OverviewSnapshot = { roomIds, ...overview, profiles };
|
||||
return json(body);
|
||||
}
|
||||
43
src/routes/api/threads/[group]/+server.ts
Normal file
43
src/routes/api/threads/[group]/+server.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { error, json } from "@sveltejs/kit";
|
||||
import { MODE, GROUP_ID } from "$lib/config";
|
||||
import { fetchGroups } from "$lib/forum/groups";
|
||||
import {
|
||||
fetchThreadPage,
|
||||
threadPagePubkeys,
|
||||
type SortMode,
|
||||
} from "$lib/forum/threads";
|
||||
import { fetchProfiles } from "$lib/forum/profiles";
|
||||
import type { ThreadsSnapshot } from "$lib/forum/snapshot";
|
||||
import { forumQuery, profileQuery } from "$lib/ssr/relay";
|
||||
|
||||
// First page of a group's listing. Unknown rooms are a 404 so crawlers don't
|
||||
// index empty shells; private rooms are simply empty to the anonymous server.
|
||||
export async function GET({ params, url }) {
|
||||
const groupId = params.group;
|
||||
if (MODE === "simple") {
|
||||
if (groupId !== GROUP_ID) error(404, "Not found");
|
||||
} else {
|
||||
const groups = await fetchGroups(forumQuery);
|
||||
if (!groups.some((g) => g.id === groupId)) error(404, "Not found");
|
||||
}
|
||||
const sort: SortMode =
|
||||
url.searchParams.get("sort") === "new" ? "new" : "active";
|
||||
const snapshotAt = Math.floor(Date.now() / 1000);
|
||||
const page = await fetchThreadPage(forumQuery, groupId, {
|
||||
sort,
|
||||
until: snapshotAt,
|
||||
snapshotAt,
|
||||
});
|
||||
const profiles = await fetchProfiles(
|
||||
profileQuery,
|
||||
threadPagePubkeys(page.threads),
|
||||
);
|
||||
const body: ThreadsSnapshot = {
|
||||
groupId,
|
||||
sort,
|
||||
snapshotAt,
|
||||
...page,
|
||||
profiles,
|
||||
};
|
||||
return json(body);
|
||||
}
|
||||
|
|
@ -1,6 +1,17 @@
|
|||
import { snapshot, type Profiles } from "$lib/forum/snapshot";
|
||||
import {
|
||||
snapshot,
|
||||
type OverviewSnapshot,
|
||||
type Profiles,
|
||||
} from "$lib/forum/snapshot";
|
||||
|
||||
// Admin profiles for the cards, plus the latest-discussions panel beside them.
|
||||
export async function load({ fetch }) {
|
||||
const data = await snapshot<{ profiles: Profiles }>(fetch, "/api/contacts");
|
||||
return { profiles: data?.profiles ?? null };
|
||||
const [contacts, overview] = await Promise.all([
|
||||
snapshot<{ profiles: Profiles }>(fetch, "/api/contacts"),
|
||||
snapshot<OverviewSnapshot>(fetch, "/api/overview"),
|
||||
]);
|
||||
return {
|
||||
overview,
|
||||
profiles: { ...overview?.profiles, ...contacts?.profiles },
|
||||
};
|
||||
}
|
||||
|
|
|
|||
7
src/routes/resource/[slug]/+page.ts
Normal file
7
src/routes/resource/[slug]/+page.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import { snapshot, type OverviewSnapshot } from "$lib/forum/snapshot";
|
||||
|
||||
// Articles show the latest-discussions panel next to the text.
|
||||
export async function load({ fetch }) {
|
||||
const overview = await snapshot<OverviewSnapshot>(fetch, "/api/overview");
|
||||
return { overview, profiles: overview?.profiles ?? null };
|
||||
}
|
||||
10
src/routes/room/[slug]/+page.ts
Normal file
10
src/routes/room/[slug]/+page.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { snapshot, type ThreadsSnapshot } from "$lib/forum/snapshot";
|
||||
|
||||
export async function load({ fetch, params, url }) {
|
||||
const sort = url.searchParams.get("sort") === "new" ? "?sort=new" : "";
|
||||
const threads = await snapshot<ThreadsSnapshot>(
|
||||
fetch,
|
||||
`/api/threads/${params.slug}${sort}`,
|
||||
);
|
||||
return { threads, profiles: threads?.profiles ?? null };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue