Add bots files handling logic

This commit is contained in:
2026-10-21 17:56:09 +01:00
parent b0af4db1bf
commit d17a4a7bdd
97 changed files with 5843 additions and 41 deletions
@@ -1,5 +1,6 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import deleteMedia from "@/src/functions/backend/db/media/delete-media";
import purgeStorage, { grabStorageMediaIds } from "@/src/functions/backend/db/bot-files/purge-storage";
import AppError from "@/src/utils/app-error";
import grabUserBot from "./grab-user-bot";
import assertCrewManager from "@/src/functions/backend/db/access/assert-crew-manager";
@@ -27,6 +28,13 @@ export default async function deleteBot({ user_id, bot_id }: Params) {
throw new AppError(`The coordinator can't be deleted.`);
}
/**
* Read before the delete: the files' rows go with the bot.
*/
const file_media_ids = await grabStorageMediaIds({ scope: "bot", owner_id: bot.id! }).catch(
() => [],
);
const res = await BunSQLite.sql({ sql: `DELETE FROM bots WHERE id = ?`, values: [bot.id] });
if (!res.success) {
@@ -37,5 +45,11 @@ export default async function deleteBot({ user_id, bot_id }: Params) {
await deleteMedia({ id: bot.image_media_id, user_id: Number(user_id) }).catch(() => {});
}
/**
* Its folder goes with it: the bytes, their `media` rows and the search
* vectors.
*/
await purgeStorage({ scope: "bot", owner_id: bot.id!, media_ids: file_media_ids });
return bot;
}