feat(history): delete single / batch / clear all play history

This commit is contained in:
jiayuLian committed 2026-11-08 03:11:36 +08:00
1 parent 17008ae52e
commit a613d6cac0
4 files changed
+250 -15

No files matched your search

+24
View File
@@ -130,6 +130,10 @@ export interface BotDatabase {
db: Database.Database;
addPlayHistory(entry: PlayHistoryEntry): void;
getPlayHistory(botId: string, limit: number): PlayHistoryRecord[];
/** Deletes the given history rows (by primary key) for one bot. Returns affected rows. */
deletePlayHistory(botId: string, ids: number[]): number;
/** Deletes every history row of one bot. Returns affected rows. */
clearPlayHistory(botId: string): number;
saveBotInstance(instance: BotInstance): void;
getBotInstances(): BotInstance[];
deleteBotInstance(id: string): boolean;
@@ -416,6 +420,10 @@ export function createDatabase(dbPath: string): BotDatabase {
SELECT * FROM play_history WHERE botId = ? ORDER BY id DESC LIMIT ?
`);
// botId is part of every predicate so one bot can never delete another bot's rows.
const deleteHistoryRow = db.prepare("DELETE FROM play_history WHERE botId = ? AND id = ?");
const deleteHistoryAll = db.prepare("DELETE FROM play_history WHERE botId = ?");
const upsertInstance = db.prepare(`
INSERT INTO bot_instances (id, name, serverAddress, serverPort, nickname, defaultChannel, channelId, channelPassword, autoStart, serverProtocol, ts6ApiKey, serverPassword, identity)
VALUES (@id, @name, @serverAddress, @serverPort, @nickname, @defaultChannel, @channelId, @channelPassword, @autoStart, @serverProtocol, @ts6ApiKey, @serverPassword, @identity)
@@ -566,6 +574,22 @@ export function createDatabase(dbPath: string): BotDatabase {
return selectHistory.all(botId, limit) as PlayHistoryRecord[];
},
deletePlayHistory(botId, ids) {
let deleted = 0;
// Single transaction: either the whole selection goes away or nothing does.
const tx = db.transaction((list: number[]) => {
for (const id of list) {
deleted += deleteHistoryRow.run(botId, id).changes;
}
});
tx(ids);
return deleted;
},
clearPlayHistory(botId) {
return deleteHistoryAll.run(botId).changes;
},
saveBotInstance(instance) {
upsertInstance.run({
...instance,
+50
View File
@@ -805,6 +805,9 @@ export function createPlayerRouter(
const limit = parseInt(req.query.limit as string) || 50;
const records = database.getPlayHistory(req.params.botId, limit);
const history = records.map((r) => ({
// rowId is the history row primary key — needed to delete a single record.
// `id` stays the platform song id so existing players keep working.
rowId: r.id,
id: r.songId,
name: r.songName,
artist: r.artist,
@@ -818,5 +821,52 @@ export function createPlayerRouter(
res.json({ history });
});
// Delete one history record: /:botId/history/:rowId
router.delete(
"/:botId/history/:rowId",
authorize({ capability: "player.queue", guestFlag: "removeClear" }),
(req, res) => {
if (!database) {
res.status(404).json({ error: "history unavailable" });
return;
}
const rowId = Number.parseInt(req.params.rowId, 10);
if (!Number.isInteger(rowId) || rowId <= 0) {
res.status(400).json({ error: "invalid rowId" });
return;
}
const deleted = database.deletePlayHistory(req.params.botId, [rowId]);
res.json({ deleted });
}
);
// Batch delete / clear all: body { ids: number[] } or { all: true }
router.delete(
"/:botId/history",
authorize({ capability: "player.queue", guestFlag: "removeClear" }),
(req, res) => {
if (!database) {
res.status(404).json({ error: "history unavailable" });
return;
}
const body = (req.body ?? {}) as { ids?: unknown; all?: unknown };
if (body.all === true) {
const deleted = database.clearPlayHistory(req.params.botId);
res.json({ deleted });
return;
}
const ids = Array.isArray(body.ids)
? body.ids.filter((v): v is number => Number.isInteger(v) && (v as number) > 0)
: [];
if (ids.length === 0) {
res.status(400).json({ error: "ids[] or all:true required" });
return;
}
// Cap the batch so a hostile payload cannot lock the DB for long.
const deleted = database.deletePlayHistory(req.params.botId, ids.slice(0, 500));
res.json({ deleted });
}
);
return router;
}