18 lines
1.1 KiB
TypeScript
18 lines
1.1 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireUser } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
import { externalUrl } from "@/lib/http";
|
|
import { requireSameOrigin } from "@/lib/security";
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
requireSameOrigin(request); const user = await requireUser(); const form = await request.formData();
|
|
const postId = Number(form.get("postId")); const reason = String(form.get("reason") || "").trim();
|
|
if (!postId || reason.length < 3 || reason.length > 500) throw new Error("Invalid report");
|
|
const post = db.prepare("SELECT id FROM posts WHERE id=? AND hidden=0").get(postId);
|
|
if (!post) throw new Error("Post not found");
|
|
db.prepare("INSERT INTO reports(post_id,reporter_id,reason) SELECT ?,?,? WHERE NOT EXISTS (SELECT 1 FROM reports WHERE post_id=? AND reporter_id=? AND resolved=0)").run(postId, user.id, reason, postId, user.id);
|
|
return NextResponse.redirect(externalUrl(request, `/posts/${postId}?reported=1`));
|
|
} catch { return NextResponse.redirect(externalUrl(request, "/")); }
|
|
}
|