Forum: a service of its own and the /forum pages
services/forum (axum, port 8083) on the same database and login, in the forum schema (migration 0038, which also adds the moderator role): nicknames, profiles with shared signs, topics, threads with listening or advice intent, two-level replies, read marks and followed threads, reactions as ratings with points and ranks, newcomer limits (lifted by a purchase), moderation with reasons, bans and a log. Integration tests run in CI. The frontend's /forum pages (light by default, dark on request, phone first with a desktop layout) are switched on by FORUM_URL; the /api/v1 proxy sends /api/v1/forum/* to the forum service. Concept and status: docs/kozosseg.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -58,6 +58,7 @@ export const ROLE_LABELS: Record<string, string> = {
|
||||
premium: "Prémium",
|
||||
vip: "VIP",
|
||||
creator: "Szerző",
|
||||
moderator: "Moderátor",
|
||||
admin: "Admin",
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,259 @@
|
||||
// The forum's data as the forum service sends it, and small helpers shared
|
||||
// by its server and client components.
|
||||
|
||||
/** The forum's colours: light by default, dark on request (a cookie). */
|
||||
export const FORUM_THEME_COOKIE = "sf_forum_theme";
|
||||
export type ForumTheme = "light" | "dark";
|
||||
|
||||
export type Rank = "vandor" | "kereso" | "fono" | "csillagfono" | "sorsfono";
|
||||
export type ReactionKind = "with_you" | "thanks" | "me_too" | "insight";
|
||||
export type ChartShare = "none" | "sun" | "big3" | "planets";
|
||||
|
||||
/** How a member appears next to what they wrote. */
|
||||
export interface ForumAuthor {
|
||||
nickname: string;
|
||||
avatar: string;
|
||||
rank: Rank;
|
||||
rank_name: string;
|
||||
moderator: boolean;
|
||||
online: boolean;
|
||||
/** Planet (or `ascendant`) → sign, as the member chose to share. */
|
||||
signs: Record<string, string> | null;
|
||||
}
|
||||
|
||||
export interface ForumViewer {
|
||||
logged_in: boolean;
|
||||
needs_nickname: boolean;
|
||||
moderator: boolean;
|
||||
member: { nickname: string; avatar: string; rank: Rank; rank_name: string; banned: boolean } | null;
|
||||
}
|
||||
|
||||
export interface TopicRef {
|
||||
slug: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
export interface ForumTopic extends TopicRef {
|
||||
id: number;
|
||||
description: string;
|
||||
visibility: "public" | "members" | "restricted";
|
||||
listening: boolean;
|
||||
readable: boolean;
|
||||
thread_count?: number;
|
||||
new_count?: number | null;
|
||||
last?: { author: ForumAuthor | null; at: string } | null;
|
||||
}
|
||||
|
||||
export interface ThreadCard {
|
||||
id: number;
|
||||
slug: string;
|
||||
title: string;
|
||||
intent: "listen" | "advice" | null;
|
||||
topic: TopicRef;
|
||||
author: ForumAuthor | null;
|
||||
created_at: string;
|
||||
last_post_at: string;
|
||||
reply_count: number;
|
||||
participants: number;
|
||||
pinned: boolean;
|
||||
locked: boolean;
|
||||
unread: number | null;
|
||||
}
|
||||
|
||||
export interface ForumHome {
|
||||
viewer: ForumViewer;
|
||||
followed: { new_replies: number; threads: number } | null;
|
||||
topics: ForumTopic[];
|
||||
unanswered: ThreadCard[];
|
||||
hot: ThreadCard[];
|
||||
}
|
||||
|
||||
export interface TopicPage {
|
||||
viewer: ForumViewer;
|
||||
topic: TopicRef & { description: string; visibility: string; listening: boolean };
|
||||
threads: ThreadCard[];
|
||||
page: number;
|
||||
has_more: boolean;
|
||||
}
|
||||
|
||||
export interface ForumPost {
|
||||
id: number;
|
||||
parent_id: number | null;
|
||||
is_opening: boolean;
|
||||
author: ForumAuthor | null;
|
||||
own: boolean;
|
||||
body: string | null;
|
||||
deleted: { by_moderator: boolean; reason: string | null } | null;
|
||||
created_at: string;
|
||||
edited_at: string | null;
|
||||
helped: boolean;
|
||||
reactions: Record<ReactionKind, number>;
|
||||
score: number;
|
||||
collapsed: boolean;
|
||||
mine: ReactionKind | "down" | null;
|
||||
/** Moderators only. */
|
||||
down?: number;
|
||||
}
|
||||
|
||||
export interface ThreadPage {
|
||||
viewer: ForumViewer;
|
||||
thread: {
|
||||
id: number;
|
||||
slug: string;
|
||||
title: string;
|
||||
intent: "listen" | "advice" | null;
|
||||
topic: TopicRef & { visibility: string; listening: boolean };
|
||||
author: ForumAuthor | null;
|
||||
created_at: string;
|
||||
locked: boolean;
|
||||
pinned: boolean;
|
||||
deleted: { reason: string | null } | null;
|
||||
};
|
||||
posts: ForumPost[];
|
||||
access?: {
|
||||
can_reply: boolean;
|
||||
can_downvote: boolean;
|
||||
is_thread_author: boolean;
|
||||
following: boolean;
|
||||
last_read_post_id: number | null;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ForumMe {
|
||||
viewer: ForumViewer;
|
||||
member: {
|
||||
nickname: string;
|
||||
avatar: string;
|
||||
bio: string;
|
||||
chart_share: ChartShare;
|
||||
invisible: boolean;
|
||||
points: number;
|
||||
post_count: number;
|
||||
created_at: string;
|
||||
rank: Rank;
|
||||
rank_name: string;
|
||||
next_rank: { rank: Rank; name: string; points: number; days: number } | null;
|
||||
nickname_change_after: string | null;
|
||||
banned_until: string | null;
|
||||
ban_reason: string | null;
|
||||
} | null;
|
||||
chart: Record<Exclude<ChartShare, "none">, Record<string, string> | null> | null;
|
||||
}
|
||||
|
||||
export interface MemberProfile {
|
||||
viewer: ForumViewer;
|
||||
member: {
|
||||
author: ForumAuthor;
|
||||
bio: string;
|
||||
created_at: string;
|
||||
post_count: number;
|
||||
thread_count: number;
|
||||
last_seen: "online" | "day" | "week" | "earlier" | null;
|
||||
own: boolean;
|
||||
banned_until: string | null;
|
||||
ban_reason: string | null;
|
||||
};
|
||||
recent: {
|
||||
id: number;
|
||||
thread_id: number;
|
||||
thread_slug: string;
|
||||
thread_title: string;
|
||||
topic: TopicRef;
|
||||
created_at: string;
|
||||
excerpt: string;
|
||||
is_opening: boolean;
|
||||
}[];
|
||||
}
|
||||
|
||||
export interface ModLogEntry {
|
||||
id: number;
|
||||
actor: string | null;
|
||||
actor_kind: "human" | "ai";
|
||||
action: string;
|
||||
target: string | null;
|
||||
thread: { id: number; title: string | null } | null;
|
||||
post_id: number | null;
|
||||
reason: string | null;
|
||||
until: string | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export const REACTIONS: { kind: ReactionKind; emoji: string; label: string }[] = [
|
||||
{ kind: "with_you", emoji: "🤍", label: "Veled vagyok" },
|
||||
{ kind: "thanks", emoji: "🙏", label: "Köszönöm" },
|
||||
{ kind: "me_too", emoji: "✨", label: "Rám is igaz" },
|
||||
{ kind: "insight", emoji: "💡", label: "Érdekes gondolat" },
|
||||
];
|
||||
|
||||
export const INTENTS = {
|
||||
listen: { label: "Csak meghallgatást kér", short: "Meghallgatást kér" },
|
||||
advice: { label: "Tanácsot, véleményt kér", short: "Tanácsot kér" },
|
||||
} as const;
|
||||
|
||||
/** The emoji offered first in the composer: the Moon, the signs, warmth. */
|
||||
export const EMOJI = [
|
||||
"🙂",
|
||||
"😊",
|
||||
"🤍",
|
||||
"🙏",
|
||||
"✨",
|
||||
"🌙",
|
||||
"🌕",
|
||||
"🌑",
|
||||
"⭐",
|
||||
"🌸",
|
||||
"🍀",
|
||||
"🕯️",
|
||||
"🌿",
|
||||
"☕",
|
||||
"😢",
|
||||
"🤗",
|
||||
"♈",
|
||||
"♉",
|
||||
"♊",
|
||||
"♋",
|
||||
"♌",
|
||||
"♍",
|
||||
"♎",
|
||||
"♏",
|
||||
"♐",
|
||||
"♑",
|
||||
"♒",
|
||||
"♓",
|
||||
];
|
||||
|
||||
/** `/forum/szal/123-nem-tudom-elengedni`: the number decides, the words are for reading. */
|
||||
export function threadHref(t: { id: number; slug: string }, postId?: number): string {
|
||||
const path = t.slug ? `/forum/szal/${t.id}-${t.slug}` : `/forum/szal/${t.id}`;
|
||||
return postId ? `${path}#h${postId}` : path;
|
||||
}
|
||||
|
||||
export function memberHref(nickname: string): string {
|
||||
return `/forum/tag/${encodeURIComponent(nickname)}`;
|
||||
}
|
||||
|
||||
/** "most", "12 perce", "3 órája", "tegnap", "4 napja", then the date. */
|
||||
export function ago(iso: string, now: Date = new Date()): string {
|
||||
const then = new Date(iso);
|
||||
const minutes = Math.floor((now.getTime() - then.getTime()) / 60000);
|
||||
if (minutes < 1) return "most";
|
||||
if (minutes < 60) return `${minutes} perce`;
|
||||
const hours = Math.floor(minutes / 60);
|
||||
if (hours < 24) return `${hours} órája`;
|
||||
const days = Math.floor(hours / 24);
|
||||
if (days === 1) return "tegnap";
|
||||
if (days < 7) return `${days} napja`;
|
||||
return then.toLocaleDateString("hu-HU", {
|
||||
timeZone: "Europe/Budapest",
|
||||
year: then.getFullYear() === now.getFullYear() ? undefined : "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
});
|
||||
}
|
||||
|
||||
/** A ban's end as the member reads it. */
|
||||
export function banText(until: string | null): string {
|
||||
if (!until) return "";
|
||||
if (until === "infinity") return "véglegesen";
|
||||
return `eddig: ${new Date(until).toLocaleString("hu-HU", { timeZone: "Europe/Budapest", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit" })}`;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// 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 };
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Shapes of the backend responses the frontend uses.
|
||||
|
||||
export type Role = "admin" | "creator" | "premium" | "vip" | "user";
|
||||
export type Role = "admin" | "creator" | "moderator" | "premium" | "vip" | "user";
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
|
||||
Reference in New Issue
Block a user