/** * GET /api/chat/search?q= * Searches the caller's own 1:1 conversations (LIKE on message body) and returns * the newest matches first, each annotated with `peer` (the other party) so the * drawer can open that conversation. Gated to staff — see gateFromEvent. */ import { gateFromEvent } from '../../utils/chatAccess' import { searchMessages } from '../../utils/chatDb' export default defineEventHandler(async (event) => { const config = useRuntimeConfig() if (config.chatEnabled !== true) { setResponseStatus(event, 404) return { error: 'chat disabled' } } let userId: number try { ({ userId } = await gateFromEvent(event)) } catch (err: any) { setResponseStatus(event, err?.statusCode || 403) return { error: 'forbidden' } } const q = (getQuery(event).q ?? '').toString().trim() if (q.length < 2) return { records: [] } // too short → no scan return { records: searchMessages(userId, q, 30) } })