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()
|
||||
|
||||
@@ -5,7 +5,15 @@ import { useTranslations } from 'next-intl'
|
||||
import { useRouter } from '@/i18n/navigation'
|
||||
import { RichTextArea } from './RichTextArea'
|
||||
|
||||
export function PostActions({ postId, initialText }: { postId: number; initialText: string }) {
|
||||
export function PostActions({
|
||||
postId,
|
||||
initialText,
|
||||
deleted = false,
|
||||
}: {
|
||||
postId: number
|
||||
initialText: string
|
||||
deleted?: boolean
|
||||
}) {
|
||||
const t = useTranslations('Forum')
|
||||
const router = useRouter()
|
||||
const [editing, setEditing] = useState(false)
|
||||
@@ -38,6 +46,26 @@ export function PostActions({ postId, initialText }: { postId: number; initialTe
|
||||
}
|
||||
}
|
||||
|
||||
async function restore() {
|
||||
if (busy) return
|
||||
setBusy(true)
|
||||
setError(null)
|
||||
try {
|
||||
const res = await fetch('/api/forum/post', {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({ postId }),
|
||||
})
|
||||
if ((await res.json()).success) router.refresh()
|
||||
else setError(t('genericError'))
|
||||
} catch {
|
||||
setError(t('genericError'))
|
||||
} finally {
|
||||
setBusy(false)
|
||||
}
|
||||
}
|
||||
|
||||
async function remove() {
|
||||
if (busy || !confirm(t('confirmDeletePost'))) return
|
||||
setBusy(true)
|
||||
@@ -80,6 +108,17 @@ export function PostActions({ postId, initialText }: { postId: number; initialTe
|
||||
)
|
||||
}
|
||||
|
||||
if (deleted) {
|
||||
return (
|
||||
<div className="mt-2 flex items-center gap-3 text-xs">
|
||||
<button type="button" onClick={restore} disabled={busy} className="text-green-400 hover:text-green-300 disabled:opacity-60">
|
||||
{t('restore')}
|
||||
</button>
|
||||
{error && <span className="text-red-400">{error}</span>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mt-2 flex items-center gap-3 text-xs">
|
||||
<button type="button" onClick={() => setEditing(true)} className="text-nw-muted hover:text-nw-gold-light">
|
||||
|
||||
@@ -8,11 +8,13 @@ export function TopicModBar({
|
||||
topicId,
|
||||
locked,
|
||||
sticky,
|
||||
deleted = false,
|
||||
moveForums = [],
|
||||
}: {
|
||||
topicId: number
|
||||
locked: boolean
|
||||
sticky: boolean
|
||||
deleted?: boolean
|
||||
moveForums?: { id: number; name: string }[]
|
||||
}) {
|
||||
const t = useTranslations('Forum')
|
||||
@@ -43,6 +45,22 @@ export function TopicModBar({
|
||||
}
|
||||
}
|
||||
|
||||
if (deleted) {
|
||||
return (
|
||||
<div className="mb-6 flex flex-wrap items-center gap-2 rounded-lg border border-red-900/60 bg-red-950/20 px-3 py-2 text-sm">
|
||||
<span className="mr-1 font-semibold text-red-400">{t('deletedMark')}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => act('restore')}
|
||||
disabled={busy}
|
||||
className="text-xs text-green-400 hover:text-green-300 disabled:opacity-60"
|
||||
>
|
||||
{t('restoreTopic')}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="mb-6 flex flex-wrap items-center gap-2 rounded-lg border border-nw-border/60 bg-nw-panel-2/40 px-3 py-2 text-sm">
|
||||
<span className="mr-1 font-semibold text-nw-gold-light">{t('moderation')}:</span>
|
||||
|
||||
+13
-6
@@ -37,6 +37,7 @@ export interface Post {
|
||||
text: string
|
||||
time: string | null
|
||||
edited: boolean
|
||||
deleted: boolean
|
||||
}
|
||||
|
||||
export interface TopicFull {
|
||||
@@ -46,6 +47,7 @@ export interface TopicFull {
|
||||
poster_id: number | null
|
||||
locked: boolean
|
||||
sticky: boolean
|
||||
deleted: boolean
|
||||
}
|
||||
|
||||
export interface SearchResult {
|
||||
@@ -148,10 +150,11 @@ export async function getTopics(forumId: number, offset = 0, limit = 20): Promis
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTopic(id: number): Promise<TopicFull | null> {
|
||||
export async function getTopic(id: number, includeDeleted = false): Promise<TopicFull | null> {
|
||||
try {
|
||||
const where = includeDeleted ? 'id = ?' : 'id = ? AND deleted = 0'
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
'SELECT id, name, forum_id, poster_id, locked, sticky FROM forum_topics WHERE id = ? AND deleted = 0',
|
||||
`SELECT id, name, forum_id, poster_id, locked, sticky, deleted FROM forum_topics WHERE ${where}`,
|
||||
[id],
|
||||
)
|
||||
const r = rows[0]
|
||||
@@ -163,6 +166,7 @@ export async function getTopic(id: number): Promise<TopicFull | null> {
|
||||
poster_id: r.poster_id ?? null,
|
||||
locked: r.locked === 1,
|
||||
sticky: r.sticky === 1,
|
||||
deleted: r.deleted === 1,
|
||||
}
|
||||
: null
|
||||
} catch {
|
||||
@@ -170,10 +174,11 @@ export async function getTopic(id: number): Promise<TopicFull | null> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function countPosts(topicId: number): Promise<number> {
|
||||
export async function countPosts(topicId: number, includeDeleted = false): Promise<number> {
|
||||
try {
|
||||
const del = includeDeleted ? '' : 'AND deleted = 0'
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
'SELECT COUNT(*) AS n FROM forum_posts WHERE topic_id = ? AND deleted = 0',
|
||||
`SELECT COUNT(*) AS n FROM forum_posts WHERE topic_id = ? ${del}`,
|
||||
[topicId],
|
||||
)
|
||||
return Number(rows[0]?.n ?? 0)
|
||||
@@ -182,10 +187,11 @@ export async function countPosts(topicId: number): Promise<number> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPosts(topicId: number, offset = 0, limit = 15): Promise<Post[]> {
|
||||
export async function getPosts(topicId: number, offset = 0, limit = 15, includeDeleted = false): Promise<Post[]> {
|
||||
try {
|
||||
const del = includeDeleted ? '' : 'AND deleted = 0'
|
||||
const [rows] = await db(DB.web).query<RowDataPacket[]>(
|
||||
'SELECT id, poster, poster_id, text, time, created_at, updated_at FROM forum_posts WHERE topic_id = ? AND deleted = 0 ORDER BY time ASC, id ASC LIMIT ? OFFSET ?',
|
||||
`SELECT id, poster, poster_id, text, time, created_at, updated_at, deleted FROM forum_posts WHERE topic_id = ? ${del} ORDER BY time ASC, id ASC LIMIT ? OFFSET ?`,
|
||||
[topicId, limit, offset],
|
||||
)
|
||||
return rows.map((r) => ({
|
||||
@@ -198,6 +204,7 @@ export async function getPosts(topicId: number, offset = 0, limit = 15): Promise
|
||||
edited: Boolean(
|
||||
r.updated_at && r.created_at && new Date(r.updated_at).getTime() - new Date(r.created_at).getTime() > 2000,
|
||||
),
|
||||
deleted: r.deleted === 1,
|
||||
}))
|
||||
} catch {
|
||||
return []
|
||||
|
||||
@@ -294,7 +294,10 @@
|
||||
"resultsCount": "{count} result(s) for “{query}”.",
|
||||
"in": "in",
|
||||
"replyOnLastPage": "Go to the last page to reply",
|
||||
"moveTopic": "Move to"
|
||||
"moveTopic": "Move to",
|
||||
"restore": "Restore",
|
||||
"restoreTopic": "Restore topic",
|
||||
"deletedMark": "Deleted"
|
||||
},
|
||||
"Admin": {
|
||||
"title": "Admin panel",
|
||||
|
||||
@@ -294,7 +294,10 @@
|
||||
"resultsCount": "{count} resultado(s) para «{query}».",
|
||||
"in": "en",
|
||||
"replyOnLastPage": "Ir a la última página para responder",
|
||||
"moveTopic": "Mover a"
|
||||
"moveTopic": "Mover a",
|
||||
"restore": "Restaurar",
|
||||
"restoreTopic": "Restaurar tema",
|
||||
"deletedMark": "Borrado"
|
||||
},
|
||||
"Admin": {
|
||||
"title": "Panel de administración",
|
||||
|
||||
Reference in New Issue
Block a user