Spaces:
Sleeping
Sleeping
| import { txasupabase } from '../../../../lib/txasupabase.js'; | |
| export async function POST({ request, cookies }) { | |
| // Check Admin privilege | |
| const token = cookies.get('auth_token')?.value; | |
| let user = null; | |
| if (token) { | |
| try { | |
| user = JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); | |
| } catch (e) {} | |
| } | |
| if (!user || user.role !== 'admin') { | |
| return new Response(JSON.stringify({ error: 'Bạn không có quyền quản trị để thực hiện!' }), { | |
| status: 403, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| try { | |
| const { commentId, commentIds, isSpoiler, usernames, action } = await request.json(); | |
| if (!action) { | |
| return new Response(JSON.stringify({ error: 'Thiếu hành động!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| // A. BẬT / TẮT SPOILER ĐƠN | |
| if (action === 'toggle_spoiler') { | |
| if (!commentId) { | |
| return new Response(JSON.stringify({ error: 'Thiếu ID bình luận!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| const { error } = await txasupabase.supabase | |
| .from('comments') | |
| .update({ is_spoiler: !!isSpoiler }) | |
| .eq('id', commentId); | |
| if (error) throw error; | |
| return new Response(JSON.stringify({ message: 'Cập nhật trạng thái Spoiler thành công!' }), { | |
| status: 200, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| // B. XÓA HÀNG LOẠT BÌNH LUẬN | |
| if (action === 'bulk_delete') { | |
| if (!Array.isArray(commentIds) || commentIds.length === 0) { | |
| return new Response(JSON.stringify({ error: 'Chưa chọn bình luận nào để xóa!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| const { error } = await txasupabase.supabase | |
| .from('comments') | |
| .delete() | |
| .in('id', commentIds); | |
| if (error) throw error; | |
| return new Response(JSON.stringify({ message: `Đã xóa thành công ${commentIds.length} bình luận!` }), { | |
| status: 200, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| // C. BẬT / TẮT SPOILER HÀNG LOẠT | |
| if (action === 'bulk_spoiler') { | |
| if (!Array.isArray(commentIds) || commentIds.length === 0) { | |
| return new Response(JSON.stringify({ error: 'Chưa chọn bình luận nào để cập nhật!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| const { error } = await txasupabase.supabase | |
| .from('comments') | |
| .update({ is_spoiler: !!isSpoiler }) | |
| .in('id', commentIds); | |
| if (error) throw error; | |
| return new Response(JSON.stringify({ message: `Đã cập nhật Spoiler thành công cho ${commentIds.length} bình luận!` }), { | |
| status: 200, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| // D. CHẶN HÀNG LOẠT NGƯỜI DÙNG | |
| if (action === 'bulk_ban') { | |
| if (!Array.isArray(usernames) || usernames.length === 0) { | |
| return new Response(JSON.stringify({ error: 'Chưa chọn người dùng nào để chặn!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| let bannedUsers = await txasupabase.getSetting('banned_commenters') || []; | |
| if (!Array.isArray(bannedUsers)) bannedUsers = []; | |
| usernames.forEach(uname => { | |
| if (!bannedUsers.includes(uname)) { | |
| bannedUsers.push(uname); | |
| } | |
| }); | |
| await txasupabase.saveSetting('banned_commenters', bannedUsers); | |
| return new Response(JSON.stringify({ message: `Đã chặn bình luận thành công đối với ${usernames.length} tài khoản!` }), { | |
| status: 200, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| return new Response(JSON.stringify({ error: 'Hành động không được hỗ trợ!' }), { | |
| status: 400, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } catch (error) { | |
| return new Response(JSON.stringify({ error: 'Lỗi máy chủ: ' + error.message }), { | |
| status: 500, | |
| headers: { 'Content-Type': 'application/json' } | |
| }); | |
| } | |
| } | |