import { getSession } from '@/lib/session' import { createTopic } from '@/lib/forum-write' import { forumExists } from '@/lib/forum' import { plainLength } from '@/lib/forum-sanitize' export async function POST(request: Request) { const session = await getSession() if (!session.accountId || !session.username) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 }) let b: Record = {} try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) } const forumId = Number(b.forumId) const title = String(b.title ?? '').trim() const text = String(b.text ?? '') if (!forumId || title.length < 3) return Response.json({ success: false, error: 'errTitle' }) if (plainLength(text) < 3) return Response.json({ success: false, error: 'emptyError' }) if (!(await forumExists(forumId))) return Response.json({ success: false, error: 'invalidForum' }) const topicId = await createTopic(forumId, title, session.accountId, text) return Response.json({ success: true, topicId }) }