2026-11-19 07:37:06 +00:00
|
|
|
|
// Public share links (`/s/<slug>`): loading the shared content and the
|
|
|
|
|
|
// texts of the page, its link preview and its preview image.
|
|
|
|
|
|
import "server-only";
|
|
|
|
|
|
|
|
|
|
|
|
import { headers } from "next/headers";
|
|
|
|
|
|
import { cache } from "react";
|
|
|
|
|
|
|
|
|
|
|
|
import { BACKEND_URL } from "./server";
|
|
|
|
|
|
import type { ChartResult } from "./types";
|
|
|
|
|
|
|
|
|
|
|
|
export interface PublicSummary {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
language: string;
|
|
|
|
|
|
title: string | null;
|
|
|
|
|
|
summary: string | null;
|
|
|
|
|
|
completed_at: string | null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** `GET /v1/public/shares/{slug}` of a shared chart. */
|
|
|
|
|
|
export interface PublicShare {
|
|
|
|
|
|
kind: "chart" | "synastry" | "analysis";
|
|
|
|
|
|
title: string | null;
|
|
|
|
|
|
created_at: string;
|
|
|
|
|
|
chart?: { label: string; chart: ChartResult & { time_known: boolean } };
|
|
|
|
|
|
summary?: PublicSummary | null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const SLUG = /^[A-Za-z0-9_-]{8,64}$/;
|
|
|
|
|
|
|
2026-11-19 09:26:06 +00:00
|
|
|
|
/** Cache tag of the shared content; revoking a link clears it (see the /api/v1 proxy). */
|
|
|
|
|
|
export const SHARES_TAG = "shares";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The shared content, or `null` if the link is unknown or revoked. Kept for
|
|
|
|
|
|
* five minutes, so robots opening the page again and again do not reach the
|
|
|
|
|
|
* backend; one backend call per request at most.
|
|
|
|
|
|
*/
|
2026-11-19 07:37:06 +00:00
|
|
|
|
export const loadShare = cache(async (slug: string): Promise<PublicShare | null> => {
|
|
|
|
|
|
if (!SLUG.test(slug)) return null;
|
2026-11-19 09:26:06 +00:00
|
|
|
|
const res = await fetch(`${BACKEND_URL}/v1/public/shares/${slug}`, {
|
|
|
|
|
|
next: { revalidate: 300, tags: [SHARES_TAG] },
|
|
|
|
|
|
});
|
2026-11-19 07:37:06 +00:00
|
|
|
|
if (res.status === 404) return null;
|
|
|
|
|
|
if (!res.ok) throw new Error(`share ${slug}: HTTP ${res.status}`);
|
|
|
|
|
|
return (await res.json()) as PublicShare;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/** The site's own address, for absolute links in the preview tags. */
|
|
|
|
|
|
export async function siteUrl(): Promise<string> {
|
|
|
|
|
|
const fixed = process.env.PUBLIC_BASE_URL;
|
|
|
|
|
|
if (fixed) return fixed.replace(/\/$/, "");
|
|
|
|
|
|
const h = await headers();
|
|
|
|
|
|
const host = h.get("x-forwarded-host") ?? h.get("host") ?? "localhost:3001";
|
|
|
|
|
|
const proto = h.get("x-forwarded-proto") ?? (host.startsWith("localhost") ? "http" : "https");
|
|
|
|
|
|
return `${proto}://${host}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Model text without the `**bold**` marks. */
|
|
|
|
|
|
export function plain(text: string): string {
|
|
|
|
|
|
return text.replace(/\*\*/g, "").replace(/\s+/g, " ").trim();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** At most `max` characters, cut at a word, with an ellipsis. */
|
|
|
|
|
|
export function shorten(text: string, max: number): string {
|
|
|
|
|
|
if (text.length <= max) return text;
|
|
|
|
|
|
const cut = text.slice(0, max - 1);
|
|
|
|
|
|
const space = cut.lastIndexOf(" ");
|
|
|
|
|
|
return `${(space > max * 0.6 ? cut.slice(0, space) : cut).replace(/[\s,;:–-]+$/, "")}…`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Changes whenever the summary is rewritten, so link previews fetch the new image. */
|
|
|
|
|
|
export function imageVersion(share: PublicShare): string {
|
|
|
|
|
|
const s = share.summary;
|
|
|
|
|
|
if (!s) return "kep";
|
|
|
|
|
|
const time = s.completed_at ? Date.parse(s.completed_at).toString(36) : "0";
|
|
|
|
|
|
return `${s.id.replace(/-/g, "").slice(-8)}${time}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** "Nap Nyilas · Hold Bika · Aszcendens Mérleg" (the ascendant only with a known time). */
|
|
|
|
|
|
export function bigThree(chart: ChartResult & { time_known: boolean }): { label: string; sign: string }[] {
|
|
|
|
|
|
const sign = (id: string) => chart.points.find((p) => p.id === id)?.sign;
|
|
|
|
|
|
const asc = chart.angles?.ascendant;
|
|
|
|
|
|
const out: { label: string; sign: string }[] = [];
|
|
|
|
|
|
const sun = sign("sun");
|
|
|
|
|
|
const moon = sign("moon");
|
|
|
|
|
|
if (sun) out.push({ label: "Nap", sign: sun });
|
|
|
|
|
|
if (moon) out.push({ label: "Hold", sign: moon });
|
|
|
|
|
|
if (chart.time_known && typeof asc === "object" && asc) out.push({ label: "Aszcendens", sign: asc.sign });
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}
|