/** * Share links: an address that opens a read-only wall display without signing in. The address carries a long random secret; only its * SHA-256 is stored, so the secret can't be read back out of the database or a backup, and it is shown once, when the link is made. */ import { createHash, randomBytes } from "node:crypto"; import { eq } from "drizzle-orm"; import { db } from "../db/client.js"; import { shareLinks, type ShareLinkDetail, type ShareLinkKind } from "../db/schema.js"; export type ShareLinkRow = typeof shareLinks.$inferSelect; /** What the public may be told about a link that doesn't work: nothing that tells "never existed" from "ended" or "removed". */ export const NOT_FOUND = { error: "not_found", message: "This link doesn't work." } as const; const hash = (token: string) => createHash("sha256").update(token).digest("hex"); /** A link isn't recorded as used more than once in this time, so a screen refreshing every few seconds doesn't write the database each time. */ const USED_WRITE_GAP_MS = 60_000; export async function createShareLink(params: { label: string; detail: ShareLinkDetail; kind?: ShareLinkKind; title?: string | null; message?: string | null; expiresInDays: number | null; createdBy: string; now?: Date }): Promise<{ link: ShareLinkRow; token: string }> { const now = params.now ?? new Date(); const token = randomBytes(24).toString("base64url"); const [link] = await db .insert(shareLinks) .values({ label: params.label, tokenHash: hash(token), tokenPrefix: token.slice(0, 6), detail: params.detail, kind: params.kind ?? "wall", title: params.title?.trim() || null, message: params.message?.trim() || null, createdBy: params.createdBy, expiresAt: params.expiresInDays === null ? null : new Date(now.getTime() + params.expiresInDays * 86_400_000).toISOString(), }) .returning(); return { link, token }; } /** The link a secret belongs to, if it exists and has not ended. Notes that it was used. */ export async function resolveShareLink(token: string, now: Date = new Date()): Promise { if (typeof token !== "string" || token.length < 16 || token.length > 128) return null; const [link] = await db.select().from(shareLinks).where(eq(shareLinks.tokenHash, hash(token))).limit(1); if (!link) return null; if (link.expiresAt && Date.parse(link.expiresAt) <= now.getTime()) return null; if (!link.lastUsedAt || now.getTime() - Date.parse(link.lastUsedAt) >= USED_WRITE_GAP_MS) { await db.update(shareLinks).set({ lastUsedAt: now.toISOString() }).where(eq(shareLinks.id, link.id)); } return link; } /** A link as the admin list shows it: everything but the hash. */ export function publicShareLink(link: ShareLinkRow, now: Date = new Date()) { return { id: link.id, label: link.label, tokenPrefix: link.tokenPrefix, detail: link.detail, kind: link.kind, title: link.title, message: link.message, createdBy: link.createdBy, createdAt: link.createdAt, expiresAt: link.expiresAt, lastUsedAt: link.lastUsedAt, expired: !!link.expiresAt && Date.parse(link.expiresAt) <= now.getTime(), }; }