migrate from hono
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { createServices, type ChatActor } from '$lib/server/services';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
|
||||
type Body = {
|
||||
action: 'send' | 'typing';
|
||||
famId?: string;
|
||||
content?: string;
|
||||
clientId?: string;
|
||||
typing?: boolean;
|
||||
};
|
||||
|
||||
export async function POST(event: RequestEvent) {
|
||||
const body = (await event.request.json().catch(() => null)) as Body | null;
|
||||
if (!body || !body.action) return json({ error: 'missing action' }, { status: 400 });
|
||||
|
||||
const { pb, famId: sessionFamId, userId, role, name, color } = actingClient(event);
|
||||
const actor: ChatActor = {
|
||||
id: userId,
|
||||
type: role === 'parent' ? 'admin' : 'member',
|
||||
name,
|
||||
color
|
||||
};
|
||||
const famId = body.famId || sessionFamId || '';
|
||||
if (!famId) return json({ error: 'famId required' }, { status: 400 });
|
||||
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
const data =
|
||||
body.action === 'typing'
|
||||
? await s.chat.typing(famId, actor, { typing: Boolean(body.typing) })
|
||||
: await s.chat.send(famId, actor, {
|
||||
content: body.content || '',
|
||||
clientId: body.clientId || ''
|
||||
});
|
||||
return json(data);
|
||||
} catch (e) {
|
||||
return json({ error: e instanceof Error ? e.message : 'chat request failed' }, { status: 400 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user