Files
cobble/src/functions/backend/db/bot-files/grab-bot-file-media.ts
T
tbenandClaude Opus 5.5 184407d76b Keep file refs working in answers passed on from chatrooms
A ref quoted alone in a code span now links like any ref, and refs
inside longer code stay literal instead of turning into broken link
text. A file chip opens a file from another bot's folder for a user
who reaches that folder, by the same rule as its bytes, so the
asked bot's files open from the asking bot's conversation. Bots are
told to keep refs out of backticks, and the coordinator to pass them
on as given.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 19:02:19 +01:00

42 lines
1.3 KiB
TypeScript

import BunSQLite from "@moduletrace/bun-sqlite";
import type {
BUN_SQLITE_COBBLE_BOT_FILES,
BUN_SQLITE_COBBLE_MEDIA,
} from "@/db/types/db";
import userReachesFileScope from "./user-reaches-file-scope";
type Params = {
media_id: number | string;
user_id: number | string;
};
/**
* A bot file's bytes (its private `media` row) for a user who reaches the
* directory it's in: the bot's own chat (or any worker, for super admins
* and admins; the coordinator only for the super admin), or the workspace
* it's shared in. Files belong to the install, like the crew, so whoever
* sees the bot sees what it saved.
*/
export default async function grabBotFileMedia({
media_id,
user_id,
}: Params): Promise<BUN_SQLITE_COBBLE_MEDIA | null> {
const res = await BunSQLite.sql<BUN_SQLITE_COBBLE_BOT_FILES>({
sql: `SELECT * FROM bot_files WHERE media_id = ? LIMIT 1`,
values: [media_id],
});
const file = res.payload?.[0];
if (!file?.id) return null;
if (!(await userReachesFileScope({ scope: file.scope, owner_id: file.owner_id, user_id }))) return null;
const media = await BunSQLite.sql<BUN_SQLITE_COBBLE_MEDIA>({
sql: `SELECT * FROM media WHERE id = ? LIMIT 1`,
values: [media_id],
});
return media.payload?.[0] || null;
}