2025-12-08 16:31:30 +03:00

49 lines
1.6 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { redis } from '@/lib/redis'
export async function GET(req: NextRequest) {
try {
const { searchParams } = new URL(req.url)
const fid = searchParams.get('fid')
const filter = searchParams.get('filter') || 'all'
if (!fid) return NextResponse.json({ notifications: [] })
const notificationsData = await redis.lrange(`notifications:${fid}`, 0, 49)
let notifications = notificationsData.map(n => JSON.parse(n))
if (filter === 'unread') {
notifications = notifications.filter(n => !n.read)
} else if (filter === 'mentions') {
notifications = notifications.filter(n => n.type === 'mention')
}
return NextResponse.json({ notifications })
} catch (error) {
console.error('Notifications API error:', error)
return NextResponse.json({ notifications: [] })
}
}
export async function POST(req: NextRequest) {
try {
const { fid } = await req.json()
if (!fid) return NextResponse.json({ error: 'FID required' }, { status: 400 })
const notificationsData = await redis.lrange(`notifications:${fid}`, 0, -1)
const notifications = notificationsData.map(n => JSON.parse(n))
const updated = notifications.map(n => ({ ...n, read: true }))
await redis.del(`notifications:${fid}`)
for (const notif of updated) {
await redis.rpush(`notifications:${fid}`, JSON.stringify(notif))
}
return NextResponse.json({ success: true })
} catch (error) {
console.error('Mark all read error:', error)
return NextResponse.json({ error: 'Failed to mark all as read' }, { status: 500 })
}
}