Files
sorsfonalak/services/frontend/src/lib/forum.ts
T
fecoandClaude Opus 5.5 b37094e481
CI / test (push) Successful in 3m19s
CI / build (push) Skipped
CI / deploy (push) Skipped
Weekly horoscope landing page, link previews for the forum, and fixes from the review
- /heti-horoszkop for visitors: what it is, a real example written for the
  example chart, login and sign-up buttons; its own link preview image.
- Forum: link previews (image, title; a public thread's opening words) for
  every forum page; none of a members-only thread's content.
- Review fixes: a removed thread's photos are no longer served; public photos
  are cached for an hour, not a year (removals take effect); photos of threads
  removed a year ago are cleaned up; deleting one forum question no longer
  revokes the chart link another question still shows; the daily digest
  leaves out closed topics the member is no longer in; typing notices at most
  one a second per connection; the weekly page asks for the text once and
  then only reads it (no repeated paid runs after a failure); turning the
  Monday e-mail back on from the e-mail page waits for next Monday, like the
  site's switch; the week's last day is right in the week the clocks go back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-27 10:12:27 +00:00

435 lines
13 KiB
TypeScript

// 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). */
import type { ForumPhoto } from "./photos";
import type { ChartResult, InterAspect } from "./types";
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;
/** A volunteer companion ("Kísérő") for the threads asking to be heard. */
listener?: boolean;
/** Bought a package and shows the "Támogató" badge. */
supporter?: boolean;
/** One of the first hundred members ("Alapító tag"). */
founder?: boolean;
}
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;
/** Archived: readable, but takes nothing new. */
archived?: boolean;
readable: boolean;
/** A restricted topic: the viewer's place in it. */
membership?: "none" | "pending" | "approved" | null;
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 & { visibility?: string };
author: ForumAuthor | null;
/** The start of the opening post (as much as a card shows). */
excerpt?: string | null;
created_at: string;
last_post_at: string;
reply_count: number;
participants: number;
pinned: boolean;
locked: boolean;
unread: number | null;
/** The site's own thread (the daily sky). */
system?: boolean;
}
export interface ForumHome {
viewer: ForumViewer;
followed: { new_replies: number; threads: number } | null;
/** Unread answers to the member. */
replies: number | null;
/** Listeners: threads asking to be heard that wait for a reply. */
waiting: number | null;
/** "Most itt vannak". */
online: ForumAuthor[];
topics: ForumTopic[];
unanswered: ThreadCard[];
hot: ThreadCard[];
/** Pinned threads, above the topics. */
pinned?: ThreadCard[];
}
export interface TopicPage {
viewer: ForumViewer;
topic: TopicRef & { description: string; visibility: string; listening: boolean; archived?: 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;
/** The nicknames the post names ("@Holdfény"). */
mentions?: string[];
/** Written by the site itself (the daily sky). */
system?: boolean;
/** The post this reply answered (it may sit on the same level). */
reply_to?: { id: number; nickname: string | null } | null;
/** The viewer muted the author: shown folded up. */
muted?: boolean;
/** A chart, forecast or synastry shared with the question. */
share?: ShareCardData | null;
/** Previews of the links a provider knows (YouTube for now). */
links?: LinkPreview[] | null;
/** Photos uploaded with the post. */
photos?: ForumPhoto[] | null;
}
/** A link the forum's link module knows, under a post. */
export interface LinkPreview {
provider: "youtube";
id: string;
/** As written in the post. */
url: string;
/** Where to start playing, in seconds. */
start: number | null;
title: string;
author: string | null;
/** Our own copy of the thumbnail. */
thumbnail: string | null;
}
/** A shared chart in a post: in full for members, `locked` for visitors, `revoked` when taken down. */
export interface ShareCardData {
slug?: string;
kind: "chart" | "synastry" | "forecast";
locked?: boolean;
revoked?: boolean;
title?: string;
summary?: string | null;
points?: { id: string; sign: string; degree: number | null; house: number | null }[];
time_known?: boolean;
/** The chart's wheel (a shared chart). */
chart?: ChartResult | null;
/** The two charts on one wheel (a shared synastry). */
pair?: { chart1: ChartResult; chart2: ChartResult; time1: boolean; time2: boolean; aspects: InterAspect[] } | null;
}
export interface ThreadPage {
viewer: ForumViewer;
thread: {
id: number;
slug: string;
title: string;
intent: "listen" | "advice" | null;
topic: TopicRef & { visibility: string; listening: boolean; archived?: boolean };
author: ForumAuthor | null;
created_at: string;
locked: boolean;
pinned: boolean;
deleted: { reason: string | null } | null;
system?: boolean;
};
posts: ForumPost[];
access?: {
can_reply: boolean;
can_downvote: boolean;
is_thread_author: boolean;
following: boolean;
last_read_post_id: number | null;
};
}
export interface EmailSettings {
replies: "off" | "daily" | "instant";
followed: "off" | "daily";
messages: "off" | "instant";
mentions: "off" | "instant";
listener: "off" | "instant";
weekly: boolean;
}
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;
email: EmailSettings;
listener: boolean;
supporter_badge: boolean;
can_listen: boolean;
can_upload_avatar: boolean;
} | null;
chart: Record<Exclude<ChartShare, "none">, Record<string, string> | null> | null;
/** Bought a package: may show the "Támogató" badge. */
can_supporter: boolean;
}
export interface MemberProfile {
viewer: ForumViewer;
relation: MemberRelation | null;
member: {
author: ForumAuthor;
bio: string;
created_at: string;
post_count: number;
thread_count: number;
last_seen: "online" | "day" | "week" | "earlier" | null;
own: boolean;
/** The viewer muted them. */
muted: 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;
/** `/forum/szal/123-nem-tudom-elengedni`: the number decides, the words are for reading. */
/** The forum's link preview image (Facebook and co.); a thread adds its own title and text. */
export const FORUM_PREVIEW = { url: "/kep/forum.png?v=1", width: 1200, height: 630, alt: "A Sorsfonalak fóruma" };
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;
}
/** The address of an uploaded avatar (`upload:<file>`), or null for a starter one. */
export function uploadedAvatar(avatar: string | null | undefined): string | null {
return avatar?.startsWith("upload:") ? `/api/v1/forum/avatars/${encodeURIComponent(avatar.slice(7))}` : null;
}
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" })}`;
}
/** The viewer's relation to a member (profile page). */
export interface MemberRelation {
conversation_id: number | null;
state: ConversationState | null;
can_request: boolean;
eligible: boolean;
}
export type ConversationState = "accepted" | "incoming" | "outgoing" | "expired" | "blocked_by_me" | "closed";
export interface ConversationItem {
id: number;
other: ForumAuthor | null;
state: ConversationState;
intro: string;
created_at: string;
last_message_at: string | null;
last_message: { excerpt: string; mine: boolean } | null;
unread: number;
}
export interface ChatMessage {
id: number;
mine: boolean;
body: string;
created_at: string;
}
export interface ConversationPage {
viewer: ForumViewer;
conversation: {
id: number;
other: ForumAuthor | null;
state: ConversationState;
intro: string;
requested_by_me: boolean;
created_at: string;
};
messages: ChatMessage[];
}
export type ReportKind = "spam" | "ad" | "rude" | "crisis" | "illegal" | "other";
export const REPORT_KINDS: { kind: ReportKind; label: string }[] = [
{ kind: "rude", label: "Bántó, kioktató vagy sértő" },
{ kind: "ad", label: "Szolgáltatást hirdet (jóslás, gyógyítás, tanfolyam)" },
{ kind: "spam", label: "Kéretlen reklám, spam" },
{ kind: "crisis", label: "Aggódom érte: lelki krízisben lehet" },
{ kind: "illegal", label: "Jogsértő tartalom" },
{ kind: "other", label: "Más (írd le)" },
];
export interface ReportEntry {
id: number;
kind: ReportKind;
note: string;
reporter: string;
status: "open" | "resolved" | "dismissed";
handled_by: string | null;
handled_at: string | null;
created_at: string;
open_reports_of_target: number;
target:
| {
type: "post";
id: number;
thread: { id: number; title: string; slug: string };
author: ForumAuthor | null;
body: string;
deleted: boolean;
}
| {
type: "message";
id: number;
conversation_id: number | null;
messages: { id: number; sender: ForumAuthor | null; body: string; created_at: string; reported: boolean }[];
}
| null;
}
/** The forum menu's counts. */
export interface ForumNavData {
viewer: ForumViewer;
/** Unread answers to the member (a reply to them, in their thread, or naming them). */
replies: number | null;
messages: number | null;
/** Moderators: open reports and requests to join restricted topics. */
reports: number | null;
}
/** An answer to the member, on the "Válaszok" page. */
export interface ReplyEntry {
id: number;
thread: { id: number; title: string; slug: string };
author: ForumAuthor | null;
excerpt: string;
created_at: string;
/** `reply`: to one of their posts; `thread`: in their thread; `mention`: names them. */
kind: "reply" | "thread" | "mention";
unread: boolean;
}
export interface TopicRequest {
topic: TopicRef;
member: ForumAuthor | null;
status: "pending" | "approved";
note: string;
requested_at: string;
}