File size: 4,354 Bytes
4bea261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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' }
    });
  }
}