"use server"; import { revalidatePath } from "next/cache"; import { auth } from "@/lib/auth"; import { prisma } from "@/lib/prisma"; // radio_shouts.message is TEXT, but cap the write to keep shouts tweet-sized. const MESSAGE_MAX = 255; /** * Post a radio shout. * * The AUTHOR (userId) is re-read from the session via auth() and is never * trusted from the submitted FormData, so a crafted form cannot impersonate * another account. radio_shouts.user_id is an UNSIGNED BIGINT, so the Int * session id is widened to BigInt for the insert. */ export async function postShout(formData: FormData): Promise { const session = await auth(); const userId = Number(session?.user?.id); if (!Number.isInteger(userId) || userId <= 0) return; const message = String(formData.get("message") ?? "") .trim() .slice(0, MESSAGE_MAX); if (!message) return; const now = new Date(); try { await prisma.radioShouts.create({ data: { userId: BigInt(userId), message, createdAt: now, updatedAt: now, }, }); } catch { // DB unavailable — fail soft; nothing to persist. return; } revalidatePath("/radio/shouts"); }