migrate from hono
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
export type ChatActor = {
|
||||
id: string;
|
||||
type: 'admin' | 'member';
|
||||
name: string;
|
||||
color: string;
|
||||
};
|
||||
|
||||
export async function chatMe(pb: any, famId: string, actor: ChatActor) {
|
||||
return { famId, actor };
|
||||
}
|
||||
|
||||
export async function send(
|
||||
pb: any,
|
||||
famId: string,
|
||||
actor: ChatActor,
|
||||
body: { content?: string; clientId?: string }
|
||||
) {
|
||||
const content = (body.content || '').trim();
|
||||
if (!content) throw new Error('content required');
|
||||
return pb.collection('messages').create({
|
||||
famId,
|
||||
authorType: actor.type,
|
||||
authorId: actor.id,
|
||||
authorName: actor.name,
|
||||
authorColor: actor.color,
|
||||
content,
|
||||
createdAt: new Date().toISOString(),
|
||||
clientId: body.clientId ? String(body.clientId).slice(0, 64) : ''
|
||||
});
|
||||
}
|
||||
|
||||
export async function typing(
|
||||
pb: any,
|
||||
famId: string,
|
||||
actor: ChatActor,
|
||||
body: { typing?: boolean }
|
||||
) {
|
||||
const existing = await pb.collection('chat_typing').getFullList({
|
||||
filter: `famId = '${famId}' && actorId = '${actor.id}' && actorType = '${actor.type}'`
|
||||
});
|
||||
const row = {
|
||||
famId,
|
||||
actorId: actor.id,
|
||||
actorType: actor.type,
|
||||
authorName: actor.name,
|
||||
authorColor: actor.color,
|
||||
typing: !!body.typing
|
||||
};
|
||||
if (existing?.length) {
|
||||
await pb.collection('chat_typing').update(existing[0].id, row);
|
||||
} else {
|
||||
await pb.collection('chat_typing').create(row);
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
Reference in New Issue
Block a user