Give every user their own bot face
A bot's face and status line live in bot_user_faces, one row per bot and user, set by jobs for the user whose conversation they're in. Needs-you and working only show to that user; every bot read overlays the viewer's face (botFaceSql) and pushes go to the user a face change is for. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,38 +1,48 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
|
||||
import grabBotAudience from "@/src/functions/backend/db/access/grab-bot-audience";
|
||||
import grabActiveBotTask from "@/src/functions/backend/db/bots/grab-active-bot-task";
|
||||
import grabBotFace from "@/src/functions/backend/db/bots/grab-bot-face";
|
||||
import { sendToUser } from "@/src/websocket/(functions)/send-to-user";
|
||||
|
||||
type Params = {
|
||||
bot: Pick<BUN_SQLITE_COBBLE_BOTS, "id" | "face" | "status_line" | "computer_id">;
|
||||
bot_id: number | string;
|
||||
/**
|
||||
* Only these users (the connected ones, for the live relay); else
|
||||
* everyone who sees the bot.
|
||||
* Only this user: a face change, which is theirs alone. Else everyone
|
||||
* who sees the bot.
|
||||
*/
|
||||
user_id?: number | null;
|
||||
/**
|
||||
* Only these users (the connected ones, for the live relay).
|
||||
*/
|
||||
users?: Set<string>;
|
||||
/**
|
||||
* The computer the bot is on, when the push knows it (a switch, or the
|
||||
* live relay). Null means its main one.
|
||||
*/
|
||||
computer_id?: number | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Pushes `server:bot-updated` (face, status line, computer) to everyone
|
||||
* who sees the bot (`grabBotAudience`), each with their own active task
|
||||
* with it, never another user's.
|
||||
* Pushes `server:bot-updated` to one user, or everyone who sees the bot
|
||||
* (`grabBotAudience`), each with their own face, status line and active
|
||||
* task with it, never another user's.
|
||||
*/
|
||||
export default async function pushBotUpdated({ bot, users }: Params) {
|
||||
if (!bot.id) return;
|
||||
export default async function pushBotUpdated({ bot_id, user_id, users, computer_id }: Params) {
|
||||
if (!bot_id) return;
|
||||
|
||||
for (const user_id of await grabBotAudience({ bot_id: bot.id })) {
|
||||
if (users && !users.has(String(user_id))) continue;
|
||||
const audience = user_id ? [user_id] : await grabBotAudience({ bot_id });
|
||||
|
||||
for (const viewer_id of audience) {
|
||||
if (users && !users.has(String(viewer_id))) continue;
|
||||
|
||||
sendToUser({
|
||||
user_id,
|
||||
user_id: viewer_id,
|
||||
data: {
|
||||
event: "server:bot-updated",
|
||||
data: {
|
||||
id: bot.id,
|
||||
face: bot.face,
|
||||
status_line: bot.status_line || "",
|
||||
...(bot.computer_id !== undefined ? { computer_id: bot.computer_id || null } : {}),
|
||||
task: await grabActiveBotTask({ bot_id: bot.id, user_id }),
|
||||
id: Number(bot_id),
|
||||
...(await grabBotFace({ bot_id, user_id: viewer_id })),
|
||||
...(computer_id !== undefined ? { computer_id: computer_id || null } : {}),
|
||||
task: await grabActiveBotTask({ bot_id, user_id: viewer_id }),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user