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>
This commit is contained in:
@@ -25,14 +25,14 @@ export default async function TopicPage({
|
||||
const t = await getTranslations('Forum')
|
||||
|
||||
const id = Number(topicId)
|
||||
const topic = await getTopic(id)
|
||||
if (!topic) notFound()
|
||||
const total = await countPosts(id)
|
||||
const totalPages = Math.max(1, Math.ceil(total / PER_PAGE))
|
||||
const page = Math.min(Math.max(1, Number((await searchParams).page) || 1), totalPages)
|
||||
const posts = await getPosts(id, (page - 1) * PER_PAGE, PER_PAGE)
|
||||
const session = await getSession()
|
||||
const isMod = await forumIsModerator(session)
|
||||
const topic = await getTopic(id, isMod)
|
||||
if (!topic) notFound()
|
||||
const total = await countPosts(id, isMod)
|
||||
const totalPages = Math.max(1, Math.ceil(total / PER_PAGE))
|
||||
const page = Math.min(Math.max(1, Number((await searchParams).page) || 1), totalPages)
|
||||
const posts = await getPosts(id, (page - 1) * PER_PAGE, PER_PAGE, isMod)
|
||||
const moveForums = isMod ? (await listForumsForMove()).filter((f) => f.id !== topic!.forum_id) : []
|
||||
const canReply = Boolean(session.username) && (!topic!.locked || isMod)
|
||||
|
||||
@@ -46,18 +46,28 @@ export default async function TopicPage({
|
||||
<h1 className="mb-4 text-2xl font-bold text-amber-500">
|
||||
{topic!.sticky && <span className="mr-2 text-sm text-nw-gold-light">[{t('pinned')}]</span>}
|
||||
{topic!.locked && <span className="mr-2 text-sm text-red-400">[{t('locked')}]</span>}
|
||||
{topic!.deleted && <span className="mr-2 text-sm text-red-400">[{t('deletedMark')}]</span>}
|
||||
{topic!.name}
|
||||
</h1>
|
||||
|
||||
{isMod && (
|
||||
<TopicModBar topicId={id} locked={topic!.locked} sticky={topic!.sticky} moveForums={moveForums} />
|
||||
<TopicModBar
|
||||
topicId={id}
|
||||
locked={topic!.locked}
|
||||
sticky={topic!.sticky}
|
||||
deleted={topic!.deleted}
|
||||
moveForums={moveForums}
|
||||
/>
|
||||
)}
|
||||
|
||||
<div className="space-y-4">
|
||||
{posts.map((post) => (
|
||||
<article key={post.id} className="nw-card">
|
||||
<article key={post.id} className={`nw-card ${post.deleted ? 'opacity-50 ring-1 ring-red-900/50' : ''}`}>
|
||||
<div className="mb-2 flex items-center justify-between border-b border-amber-900/40 pb-2 text-sm">
|
||||
<span className="font-semibold text-amber-400">{post.poster}</span>
|
||||
<span className="font-semibold text-amber-400">
|
||||
{post.poster}
|
||||
{post.deleted && <span className="ml-2 text-xs text-red-400">[{t('deletedMark')}]</span>}
|
||||
</span>
|
||||
{post.time && (
|
||||
<span className="text-amber-200/50">
|
||||
{new Date(post.time).toLocaleString(locale)}
|
||||
@@ -70,7 +80,7 @@ export default async function TopicPage({
|
||||
dangerouslySetInnerHTML={{ __html: post.text }}
|
||||
/>
|
||||
{canEditPost(session, isMod, post.poster_id) && (
|
||||
<PostActions postId={post.id} initialText={post.text} />
|
||||
<PostActions postId={post.id} initialText={post.text} deleted={post.deleted} />
|
||||
)}
|
||||
</article>
|
||||
))}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { getTopic, getForum } from '@/lib/forum'
|
||||
import { setTopicFlag, moveTopic } from '@/lib/forum-write'
|
||||
import { forumIsModerator } from '@/lib/forum-perm'
|
||||
|
||||
type Action = 'lock' | 'unlock' | 'sticky' | 'unsticky' | 'delete' | 'move'
|
||||
type Action = 'lock' | 'unlock' | 'sticky' | 'unsticky' | 'delete' | 'restore' | 'move'
|
||||
|
||||
/** Moderación de temas: bloquear, fijar, borrar. Solo moderadores. */
|
||||
export async function POST(request: Request) {
|
||||
@@ -17,7 +17,7 @@ export async function POST(request: Request) {
|
||||
const action = String(b.action ?? '') as Action
|
||||
if (!topicId) return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
|
||||
|
||||
const topic = await getTopic(topicId)
|
||||
const topic = await getTopic(topicId, true) // incluye borrados (ya verificado que es mod)
|
||||
if (!topic) return Response.json({ success: false, error: 'topicNotFound' }, { status: 404 })
|
||||
|
||||
switch (action) {
|
||||
@@ -36,6 +36,9 @@ export async function POST(request: Request) {
|
||||
case 'delete':
|
||||
await setTopicFlag(topicId, 'deleted', true)
|
||||
return Response.json({ success: true, forumId: topic.forum_id })
|
||||
case 'restore':
|
||||
await setTopicFlag(topicId, 'deleted', false)
|
||||
break
|
||||
case 'move': {
|
||||
const newForumId = Number(b.forumId)
|
||||
if (!newForumId || newForumId === topic.forum_id) {
|
||||
|
||||
@@ -6,6 +6,21 @@ 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()
|
||||
|
||||
Reference in New Issue
Block a user