Spaces:
Sleeping
Sleeping
| import { txasupabase } from '../../../lib/txasupabase.js'; | |
| export const POST = async ({ request, cookies }) => { | |
| const token = cookies.get('auth_token')?.value; | |
| if (!token) return new Response(JSON.stringify({ error: 'Chưa đăng nhập' }), { status: 401 }); | |
| try { | |
| const { movieSlug, movieName, movieThumb, episodeName, currentTime, duration } = await request.json(); | |
| const userData = JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); | |
| // Tìm người dùng trên Supabase dựa vào ID | |
| const user = await txasupabase.getUserById(userData.id); | |
| if (!user) return new Response(JSON.stringify({ error: 'User not found' }), { status: 404 }); | |
| const history = user.history || []; | |
| // Xóa mục lịch sử cũ của phim này nếu đã có để đưa lên đầu danh sách | |
| const historyIndex = history.findIndex(h => h.slug === movieSlug); | |
| if (historyIndex !== -1) { | |
| history.splice(historyIndex, 1); | |
| } | |
| // Thêm lịch sử xem phim mới lên đầu mảng | |
| history.unshift({ | |
| slug: movieSlug, | |
| name: movieName, | |
| thumb: movieThumb, | |
| episode: episodeName, | |
| currentTime: currentTime || 0, | |
| duration: duration || 0, | |
| watchedAt: new Date().toISOString() | |
| }); | |
| // Giới hạn tối đa 50 lượt xem phim gần nhất | |
| const limitedHistory = history.slice(0, 50); | |
| // Cập nhật lại lịch sử xem phim lên Supabase | |
| await txasupabase.saveUser({ | |
| ...user, | |
| history: limitedHistory | |
| }); | |
| return new Response(JSON.stringify({ success: true }), { status: 200 }); | |
| } catch (err) { | |
| console.error('[History POST Error]:', err); | |
| return new Response(JSON.stringify({ error: 'System error' }), { status: 500 }); | |
| } | |
| }; | |
| export const GET = async ({ cookies, url }) => { | |
| const token = cookies.get('auth_token')?.value; | |
| if (!token) return new Response(JSON.stringify({ history: [] }), { status: 200 }); | |
| try { | |
| const userData = JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); | |
| const user = await txasupabase.getUserById(userData.id); | |
| if (!user) return new Response(JSON.stringify({ history: [] }), { status: 200 }); | |
| const slug = url.searchParams.get('slug'); | |
| if (slug) { | |
| const item = user.history?.find(h => h.slug === slug); | |
| if (item) { | |
| return new Response(JSON.stringify({ | |
| movieSlug: item.slug, | |
| currentTime: item.currentTime, | |
| duration: item.duration, | |
| episodeName: item.episode, | |
| watchedAt: item.watchedAt, | |
| // Hỗ trợ tham số cũ cho trình phát TXAPlayer | |
| current_time: item.currentTime, | |
| episode_name: item.episode | |
| }), { status: 200 }); | |
| } | |
| return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 }); | |
| } | |
| return new Response(JSON.stringify({ history: user.history || [] }), { status: 200 }); | |
| } catch (err) { | |
| console.error('[History GET Error]:', err); | |
| return new Response(JSON.stringify({ history: [] }), { status: 200 }); | |
| } | |
| }; | |
| export const DELETE = async ({ request, cookies }) => { | |
| const token = cookies.get('auth_token')?.value; | |
| if (!token) return new Response(JSON.stringify({ error: 'Chưa đăng nhập' }), { status: 401 }); | |
| try { | |
| const { movieSlugs, clearAll } = await request.json(); | |
| const userData = JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); | |
| const user = await txasupabase.getUserById(userData.id); | |
| if (!user) return new Response(JSON.stringify({ error: 'User not found' }), { status: 404 }); | |
| let history = user.history || []; | |
| if (clearAll) { | |
| history = []; | |
| } else if (movieSlugs && Array.isArray(movieSlugs)) { | |
| history = history.filter(h => !movieSlugs.includes(h.slug)); | |
| } | |
| // Cập nhật lại lịch sử đã lọc lên Supabase | |
| await txasupabase.saveUser({ | |
| ...user, | |
| history | |
| }); | |
| return new Response(JSON.stringify({ success: true, message: 'Đã xóa lịch sử xem!' }), { status: 200 }); | |
| } catch (err) { | |
| console.error('[History DELETE Error]:', err); | |
| return new Response(JSON.stringify({ error: 'System error' }), { status: 500 }); | |
| } | |
| }; | |