Files
sorsfonalak/services/frontend/src/lib/forumServer.ts
T
fecoandClaude Opus 5.5 9ca3f478be Forum: search, muting members, archived topics, the public threads in the sitemap
- Search (/forum/kereses, the magnifier in the forum bar): thread titles
  and posts the viewer may read, Hungarian stems and word beginnings
  without accents ("alvas" finds "alvásban"), one result per thread with
  the matching words marked.
- Muting a member (post menu, profile; the list in the settings): their
  threads leave the lists, their posts fold up, their replies and mentions
  no longer count or send e-mails. They are not told.
- The admin archives a topic (Moderálás → Témák): it stays readable but
  takes no new threads or replies, and folds away on the front page.
- The public topics and threads are in the sitemap; the forum's own pages
  are kept from robots.
- A reply e-mail goes to the one answered, also on the second level.
- Migration 0043 also prepares charts attached to a forum question.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-26 14:31:05 +00:00

45 lines
1.9 KiB
TypeScript

// Forum service calls from server components, with the visitor's session.
// Without FORUM_URL the forum is switched off (no menu item, its pages 404).
import "server-only";
import { cookies } from "next/headers";
import { BackendError, SESSION_COOKIE } from "./server";
export const FORUM_URL = (process.env.FORUM_URL ?? "").replace(/\/$/, "");
export function forumEnabled(): boolean {
return FORUM_URL !== "";
}
/** The answer of a forum GET: the data, or why there is none. */
export type ForumResult<T> = { ok: true; data: T } | { ok: false; status: 401 | 404 };
/** GET from the forum service as the visitor (logged in or not). */
export async function forumGet<T>(path: string): Promise<ForumResult<T>> {
if (!forumEnabled()) return { ok: false, status: 404 };
const token = (await cookies()).get(SESSION_COOKIE)?.value;
const headers: HeadersInit = token ? { authorization: `Bearer ${token}` } : {};
const res = await fetch(`${FORUM_URL}${path}`, { headers, cache: "no-store" });
if (res.status === 401 || res.status === 404) return { ok: false, status: res.status };
// Not allowed (e.g. the moderation log for a member): the page does not exist for them.
if (res.status === 403) return { ok: false, status: 404 };
if (!res.ok) throw new BackendError(res.status, `${path}: HTTP ${res.status}`);
return { ok: true, data: (await res.json()) as T };
}
/** The public forum pages for the sitemap (nothing when the forum is off or does not answer). */
export async function forumSitemap(): Promise<{
topics: { slug: string; updated: string | null }[];
threads: { id: number; slug: string; updated: string }[];
}> {
const empty = { topics: [], threads: [] };
if (!forumEnabled()) return empty;
try {
const res = await fetch(`${FORUM_URL}/v1/forum/sitemap`, { next: { revalidate: 3600 } });
return res.ok ? await res.json() : empty;
} catch {
return empty;
}
}