- Remove .prettierrc (dead config, Biome replaces Prettier) - Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat - Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit - Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts - Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars - Replace barrel export src/types/index.ts with direct @/types/common imports - Make trustHost conditional (development only) in auth.ts - Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations - Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { redirect } from "next/navigation";
|
|
import { auth } from "@/lib/auth";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { clientIp, rateLimit } from "@/lib/rate-limit";
|
|
|
|
const SONG_MAX = 255;
|
|
const ARTIST_MAX = 255;
|
|
|
|
type RequestOutcome = "posted" | "empty" | "invalid" | "ratelimit" | "error";
|
|
|
|
function requestsRedirect(outcome: RequestOutcome): never {
|
|
if (outcome === "posted") redirect("/radio/requests?posted=1");
|
|
redirect(`/radio/requests?error=${outcome}`);
|
|
}
|
|
|
|
function isNextRedirect(e: unknown): boolean {
|
|
return (
|
|
!!e &&
|
|
typeof e === "object" &&
|
|
"digest" in e &&
|
|
typeof (e as { digest?: unknown }).digest === "string" &&
|
|
(e as { digest: string }).digest.startsWith("NEXT_REDIRECT")
|
|
);
|
|
}
|
|
|
|
export async function submitRequest(formData: FormData): Promise<void> {
|
|
let outcome: RequestOutcome = "error";
|
|
|
|
try {
|
|
const session = await auth();
|
|
const userId = Number(session?.user?.id);
|
|
if (!Number.isInteger(userId) || userId <= 0) {
|
|
redirect("/login");
|
|
}
|
|
|
|
await clientIp();
|
|
if (!(await rateLimit(`radio-req:${userId}`, 5, 30_000)).ok) {
|
|
outcome = "ratelimit";
|
|
} else {
|
|
const songTitle = String(formData.get("songTitle") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, SONG_MAX);
|
|
const artist = String(formData.get("artist") ?? "")
|
|
.normalize("NFC")
|
|
.trim()
|
|
.slice(0, ARTIST_MAX);
|
|
if (!songTitle && !artist) {
|
|
outcome = "empty";
|
|
} else {
|
|
const now = new Date();
|
|
await prisma.radioSongRequests.create({
|
|
data: {
|
|
userId: BigInt(userId),
|
|
songTitle: songTitle || null,
|
|
artist: artist || null,
|
|
submittedAt: now,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
},
|
|
});
|
|
outcome = "posted";
|
|
}
|
|
}
|
|
} catch (e) {
|
|
if (isNextRedirect(e)) throw e;
|
|
outcome = "error";
|
|
}
|
|
|
|
revalidatePath("/radio/requests");
|
|
requestsRedirect(outcome);
|
|
}
|