From a613d6cac02c1599986b9bdf44991104c5f4c782 Mon Sep 17 00:00:00 2001 From: jiayuLian Date: Sun, 8 Nov 2026 03:11:36 +0800 Subject: [PATCH 1/3] feat(history): delete single / batch / clear all play history --- docs/API.md | 4 +- src/data/database.ts | 24 +++++ src/web/api/player.ts | 50 ++++++++++ web/src/views/History.vue | 187 +++++++++++++++++++++++++++++++++++--- 4 files changed, 250 insertions(+), 15 deletions(-) diff --git a/docs/API.md b/docs/API.md index 3cb9394..e4ecc1e 100644 --- a/docs/API.md +++ b/docs/API.md @@ -225,7 +225,9 @@ interface BotStatus { | GET | `/queue` | `{ queue: QueuedSong[], status: BotStatus }` | | GET | `/elapsed` | `{ elapsed: 42.5 }` | | DELETE | `/queue/:index` | `{ message }`(移除指定下标,能力 `player.queue`) | -| GET | `/history?limit=50` | `{ history: [{ id, name, artist, album, coverUrl, platform, playedAt, requestedBy }] }` | +| GET | `/history?limit=50` | `{ history: [{ rowId, id, name, artist, album, coverUrl, platform, playedAt, requestedBy }] }` | +| DELETE | `/history/:rowId` | `{ deleted }`(删除单条记录,能力 `player.queue`) | +| DELETE | `/history` | `{ deleted }`(请求体 `{ ids: number[] }` 批量删除或 `{ all: true }` 清空,能力 `player.queue`) | | GET | `/profile` | ProfileConfig | | PUT | `/profile` | ProfileConfig(能力 `bot.manage`) | diff --git a/src/data/database.ts b/src/data/database.ts index fc0f6c4..9285685 100644 --- a/src/data/database.ts +++ b/src/data/database.ts @@ -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, diff --git a/src/web/api/player.ts b/src/web/api/player.ts index 487293f..e0681fe 100644 --- a/src/web/api/player.ts +++ b/src/web/api/player.ts @@ -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; } diff --git a/web/src/views/History.vue b/web/src/views/History.vue index a52c0d2..5ac10de 100644 --- a/web/src/views/History.vue +++ b/web/src/views/History.vue @@ -12,23 +12,56 @@ 暂无播放记录 -
- -
+ @@ -167,6 +190,11 @@ onMounted(async () => { margin-bottom: 24px; } +.history-error { + color: var(--text-secondary); + margin-bottom: 12px; +} + .toolbar { display: flex; align-items: center; @@ -231,15 +259,13 @@ onMounted(async () => { display: flex; align-items: center; padding: 4px; - opacity: 0; color: var(--text-tertiary); border-radius: var(--radius-sm); transition: all var(--transition-fast); - &:hover { color: #f2555a; opacity: 1; } + &:hover, &:focus-visible { color: #f2555a; } + &:disabled { opacity: 0.4; cursor: not-allowed; } } - - &:hover .row-del { opacity: 1; } } .loading {