import BunSQLite from "@moduletrace/bun-sqlite"; import type { BUN_SQLITE_COBBLE_BOT_FILES, BUN_SQLITE_COBBLE_MEDIA, } from "@/db/types/db"; import grabCrewRole from "../access/grab-crew-role"; import grabUserBot from "../bots/grab-user-bot"; import grabUserWorkspace from "../workspaces/grab-user-workspace"; 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 { const res = await BunSQLite.sql({ sql: `SELECT * FROM bot_files WHERE media_id = ? LIMIT 1`, values: [media_id], }); const file = res.payload?.[0]; if (!file?.id) return null; const role = await grabCrewRole({ user_id }); const reaches = file.scope == "workspace" ? Boolean( await grabUserWorkspace({ workspace_id: file.owner_id!, user_id, }), ) : role == "super_admin" ? true : Boolean(await grabUserBot({ bot_id: file.owner_id!, user_id })); if (!reaches) return null; const media = await BunSQLite.sql({ sql: `SELECT * FROM media WHERE id = ? LIMIT 1`, values: [media_id], }); return media.payload?.[0] || null; }