Foro: edición/borrado de posts, moderación de temas y búsqueda
Portado desde forum/db.py + forum/permissions.py de Django: - lib/forum-perm.ts: forumIsModerator (gmlevel >= FORUM_MOD_GMLEVEL) y canEditPost (autor o moderador). - Edición y borrado lógico de posts (autor o mod), respetando tema bloqueado, con validación de longitud mínima (plainLength). - Moderación de temas (solo mod): bloquear/desbloquear, fijar/no fijar, borrar. - Búsqueda de temas por título/contenido (searchTopics/countSearchTopics) con página /forum/search y buscador en el índice. - API: PATCH/DELETE /api/forum/post, POST /api/forum/moderate (401/403 correctos). - UI: PostActions, TopicModBar, ForumSearchBox; marca "editado" y "[Fijado]". - i18n: 22 claves nuevas en es/en. Verificado: build OK, /forum y /forum/search 200, APIs 401 sin sesión. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
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
|
||||
|
||||
/** 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 })
|
||||
}
|
||||
Reference in New Issue
Block a user