optimize post migration
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { actingClient } from '$lib/server/routeAuth';
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { createPbClient } from '$lib/server/pocketbase';
|
||||
import { createServices, type ChatActor } from '$lib/server/services';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
|
||||
@@ -15,18 +15,20 @@ 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 u = event.locals.user;
|
||||
if (!u || !event.locals.pbToken) throw error(401, 'Unauthorized');
|
||||
const pb = createPbClient(event.locals.pbToken);
|
||||
const actor: ChatActor = {
|
||||
id: userId,
|
||||
type: role === 'parent' ? 'admin' : 'member',
|
||||
name,
|
||||
color
|
||||
id: u.id,
|
||||
type: u.role === 'parent' ? 'admin' : 'member',
|
||||
name: u.name,
|
||||
color: u.color || '#6366f1'
|
||||
};
|
||||
const famId = body.famId || sessionFamId || '';
|
||||
const famId = body.famId || u.famId || '';
|
||||
if (!famId) return json({ error: 'famId required' }, { status: 400 });
|
||||
|
||||
try {
|
||||
const s = createServices(pb, { id: userId, role });
|
||||
const s = createServices(pb, { id: u.id, role: u.role });
|
||||
const data =
|
||||
body.action === 'typing'
|
||||
? await s.chat.typing(famId, actor, { typing: Boolean(body.typing) })
|
||||
|
||||
Reference in New Issue
Block a user