reroute chat
This commit is contained in:
+2
-26
@@ -2346,31 +2346,6 @@ async function resolveChatActor(c: any) {
|
||||
};
|
||||
}
|
||||
}
|
||||
const cookieHeader = c.req.header("cookie") || "";
|
||||
const cookieMatch = cookieHeader.match(/session=([^;]+)/);
|
||||
if (cookieMatch) {
|
||||
try {
|
||||
const s = JSON.parse(decodeURIComponent(cookieMatch[1]));
|
||||
if (s?.famId && s?.userId) {
|
||||
const admins = await pb.getList(
|
||||
"fam_admins",
|
||||
`famId = '${s.famId}' && userId = '${s.userId}'`,
|
||||
);
|
||||
const admin = admins.items?.[0];
|
||||
if (admin) {
|
||||
return {
|
||||
famId: s.famId,
|
||||
actor: {
|
||||
id: admin.id,
|
||||
type: "admin",
|
||||
name: admin.name,
|
||||
color: admin.color,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
const famId = c.req.header("x-device-famid");
|
||||
const deviceToken = c.req.header("x-device-token");
|
||||
if (famId && deviceToken) {
|
||||
@@ -2399,7 +2374,7 @@ app.post("/api/chat/:famId/messages", async (c) => {
|
||||
try {
|
||||
const auth = await resolveChatActor(c);
|
||||
if (!auth) return c.json({ error: "Unauthorized" }, 401);
|
||||
const { content } = await c.req.json();
|
||||
const { content, clientId } = await c.req.json();
|
||||
if (!content || !content.trim()) {
|
||||
return c.json({ error: "content required" }, 400);
|
||||
}
|
||||
@@ -2411,6 +2386,7 @@ app.post("/api/chat/:famId/messages", async (c) => {
|
||||
authorColor: auth.actor.color,
|
||||
content: content.trim(),
|
||||
createdAt: new Date().toISOString(),
|
||||
clientId: clientId ? String(clientId).slice(0, 64) : "",
|
||||
});
|
||||
return c.json(record);
|
||||
} catch (err) {
|
||||
|
||||
+13
-7
@@ -1451,6 +1451,7 @@ export async function migrate(): Promise<void> {
|
||||
{ name: "authorColor", type: "text", required: false },
|
||||
{ name: "content", type: "text", required: true },
|
||||
{ name: "createdAt", type: "date", required: false },
|
||||
{ name: "clientId", type: "text", required: false, max: 64 },
|
||||
],
|
||||
});
|
||||
} else {
|
||||
@@ -1459,6 +1460,7 @@ export async function migrate(): Promise<void> {
|
||||
// older stores may predate it. Chat filters/sorts on createdAt, so
|
||||
// ensure the field exists even if the collection was created without it.
|
||||
const msgsFields = msgsCol.fields?.map((f: any) => f.name) || [];
|
||||
let msgsChanged = false;
|
||||
if (!msgsFields.includes("createdAt")) {
|
||||
console.log(" ↳ adding missing createdAt field to messages...");
|
||||
msgsCol.fields.push({
|
||||
@@ -1468,14 +1470,18 @@ export async function migrate(): Promise<void> {
|
||||
min: "",
|
||||
max: "",
|
||||
});
|
||||
await updateCollection("messages", {
|
||||
listRule: "",
|
||||
viewRule: "",
|
||||
fields: msgsCol.fields,
|
||||
});
|
||||
} else {
|
||||
await updateCollection("messages", { listRule: "", viewRule: "" });
|
||||
msgsChanged = true;
|
||||
}
|
||||
if (!msgsFields.includes("clientId")) {
|
||||
console.log(" ↳ adding missing clientId field to messages...");
|
||||
msgsCol.fields.push({ name: "clientId", type: "text", required: false, max: 64 });
|
||||
msgsChanged = true;
|
||||
}
|
||||
await updateCollection("messages", {
|
||||
listRule: "",
|
||||
viewRule: "",
|
||||
fields: msgsCol.fields,
|
||||
});
|
||||
}
|
||||
|
||||
// 9b. chat_typing (transient presence rows, one per actor)
|
||||
|
||||
Reference in New Issue
Block a user