54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getAllPolls } from '@/lib/polls'
|
|
import { redis } from '@/lib/redis'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url)
|
|
const query = searchParams.get('q')?.toLowerCase() || ''
|
|
const type = searchParams.get('type') || 'polls'
|
|
|
|
if (!query || query.length < 2) {
|
|
return NextResponse.json({ polls: [], users: [] })
|
|
}
|
|
|
|
if (type === 'polls') {
|
|
const polls = await getAllPolls()
|
|
const results = polls.filter(poll =>
|
|
poll.question.toLowerCase().includes(query) ||
|
|
poll.description?.toLowerCase().includes(query) ||
|
|
poll.options.some(opt => opt.text.toLowerCase().includes(query))
|
|
)
|
|
|
|
return NextResponse.json({ polls: results.slice(0, 50) })
|
|
} else {
|
|
const allUsers = await redis.keys('user:*')
|
|
const users = []
|
|
|
|
for (const key of allUsers) {
|
|
const userData = await redis.get(key)
|
|
if (!userData) continue
|
|
|
|
const user = typeof userData === 'string' ? JSON.parse(userData) : userData
|
|
const fid = key.replace('user:', '')
|
|
const username = user.username || `User ${fid}`
|
|
|
|
if (username.toLowerCase().includes(query)) {
|
|
const votes = await redis.lrange(`votes:${fid}`, 0, -1)
|
|
users.push({
|
|
fid,
|
|
username,
|
|
pfpUrl: user.pfpUrl,
|
|
totalVotes: votes.length
|
|
})
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({ users: users.slice(0, 50) })
|
|
}
|
|
} catch (error) {
|
|
console.error('Search API error:', error)
|
|
return NextResponse.json({ polls: [], users: [] })
|
|
}
|
|
}
|