Files
NightSpire/web-next/app/api/forum/post/route.ts
T
Inna 8a3ba8ff40 Foro: restaurar posts y temas borrados (moderadores)
- lib/forum.ts: getTopic/getPosts/countPosts aceptan includeDeleted (mods ven lo
  borrado); Post.deleted y TopicFull.deleted.
- API: PUT /api/forum/post restaura post (mod-only); moderate action 'restore'
  restaura tema (usa includeDeleted al leer el tema).
- UI: posts borrados atenuados con badge y botón Restaurar (PostActions);
  TopicModBar muestra banner + Restaurar tema cuando el tema está borrado.
- i18n es/en: Forum.restore, restoreTopic, deletedMark.

Verificado: build OK, PUT post y moderate restore 401 sin sesión, /forum 200.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 01:07:18 +00:00

71 lines
3.3 KiB
TypeScript

import { getSession } from '@/lib/session'
import { getPost, updatePost, setPostDeleted } from '@/lib/forum-write'
import { getTopic } from '@/lib/forum'
import { forumIsModerator, canEditPost } from '@/lib/forum-perm'
import { plainLength } from '@/lib/forum-sanitize'
const MIN_TEXT_LEN = 5
/** Restaurar un post borrado. Solo moderadores. */
export async function PUT(request: Request) {
const session = await getSession()
if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
if (!(await forumIsModerator(session))) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
let b: Record<string, string> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const postId = Number(b.postId)
if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
await setPostDeleted(postId, false)
return Response.json({ success: true })
}
/** Editar el texto de un post propio (o cualquiera si es moderador). */
export async function PATCH(request: Request) {
const session = await getSession()
if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
let b: Record<string, string> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const postId = Number(b.postId)
const text = String(b.text ?? '').trim()
if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
const post = await getPost(postId)
if (!post) return Response.json({ success: false, error: 'postNotFound' }, { status: 404 })
const isMod = await forumIsModerator(session)
if (!canEditPost(session, isMod, post.poster_id)) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const topic = await getTopic(post.topic_id)
if (topic?.locked && !isMod) return Response.json({ success: false, error: 'topicLocked' })
if (plainLength(text) < MIN_TEXT_LEN) return Response.json({ success: false, error: 'tooShort' })
await updatePost(postId, text)
return Response.json({ success: true })
}
/** Borrar (lógico) un post propio (o cualquiera si es moderador). */
export async function DELETE(request: Request) {
const session = await getSession()
if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
let b: Record<string, string> = {}
try { b = await request.json() } catch { return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 }) }
const postId = Number(b.postId)
if (!postId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
const post = await getPost(postId)
if (!post) return Response.json({ success: false, error: 'postNotFound' }, { status: 404 })
const isMod = await forumIsModerator(session)
if (!canEditPost(session, isMod, post.poster_id)) return Response.json({ success: false, error: 'forbidden' }, { status: 403 })
const topic = await getTopic(post.topic_id)
if (topic?.locked && !isMod) return Response.json({ success: false, error: 'topicLocked' })
await setPostDeleted(postId, true)
return Response.json({ success: true })
}