Files
sorsfonalak/services/frontend/src/lib/forum.ts
T

260 lines
6.7 KiB
TypeScript
Raw Normal View History

// 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" })}`;
}