Files
cobble/src/bots-queue/push-bot-created.ts
T
tben fccaa6e280 Show a newly created bot in the rail at once
The rail was built from page props and only knew bots it already had, so a
bot the coordinator created did not appear until a reload.

create_bot and add_bot_to_workspace now push server:bot-created
(BotCreatedData) to everyone who sees the bot: its rail row, the
workspaces it is in, and whether it belongs in that user's own chat list.
useLiveBots inserts it, after the pinned coordinator outside a workspace
and only when the open workspace is one it joined. liveRelay pushes created
bots too, so a bot added by another process still shows.
2026-10-19 12:17:56 +01:00

60 lines
1.8 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
import type { BotWithLastMessage } from "@/src/types";
import grabBotAudience from "@/src/functions/backend/db/access/grab-bot-audience";
import isBotInOwnChat from "@/src/functions/backend/db/bots/is-bot-in-own-chat";
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
type Params = {
bot: BUN_SQLITE_COBBLE_BOTS;
/**
* Only these users (the connected ones, for the live relay); else
* everyone who sees the bot.
*/
users?: Set<string>;
};
/**
* Pushes `server:bot-created` to everyone who sees a bot that just joined
* the crew or a workspace: its rail row, the workspaces it's in, and
* whether it also belongs in that user's own chat list.
*/
export default async function pushBotCreated({ bot, users }: Params) {
if (!bot.id) return;
const bot_id = Number(bot.id);
const workspace_ids = (
(
await BunSQLite.sql<{ workspace_id: number }>({
sql: `SELECT workspace_id FROM workspace_bots WHERE bot_id = ?`,
values: [bot_id],
})
).payload || []
).map((row) => Number(row.workspace_id));
const data: BotWithLastMessage = {
...bot,
unread: 0,
last_activity_at: null,
last_message_text: null,
last_message_at: null,
};
for (const user_id of await grabBotAudience({ bot_id })) {
if (users && !users.has(String(user_id))) continue;
sendToUser({
user_id,
data: {
event: "server:bot-created",
data: {
bot: data,
workspace_ids,
in_own_chat: await isBotInOwnChat({ bot_id, user_id }),
},
},
});
}
}