mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-01 02:12:50 +08:00
Merge main into playback retry
This commit is contained in:
commit
dc2659628e
6 files changed
+374
-37
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import express from "express";
|
||||
import pino from "pino";
|
||||
import request from "supertest";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import { createDatabase, type BotDatabase } from "../../data/database.js";
|
||||
import { createPlayerRouter } from "./player.js";
|
||||
|
||||
describe("play history deletion", () => {
|
||||
let db: BotDatabase;
|
||||
let app: express.Express;
|
||||
let firstId: number;
|
||||
let secondId: number;
|
||||
|
||||
beforeEach(() => {
|
||||
db = createDatabase(":memory:");
|
||||
for (const botId of ["first", "second"]) {
|
||||
db.addPlayHistory({ botId, songId: "song", songName: "Example", artist: "", album: "", coverUrl: "", platform: "netease" });
|
||||
}
|
||||
firstId = db.getPlayHistory("first", 1)[0].id;
|
||||
secondId = db.getPlayHistory("second", 1)[0].id;
|
||||
app = express();
|
||||
app.use(express.json());
|
||||
app.use((req, _res, next) => {
|
||||
req.user = { id: "u", username: "member", role: "member", capabilities: new Set(["player.queue"]), bots: new Set(["first"]) };
|
||||
next();
|
||||
});
|
||||
const bots = { getBot: (id: string) => ({ id }) } as unknown as Parameters<typeof createPlayerRouter>[0];
|
||||
app.use("/player", createPlayerRouter(bots, pino({ level: "silent" }), db));
|
||||
});
|
||||
|
||||
afterEach(() => db.close());
|
||||
|
||||
it("rejects malformed and ambiguous selections without deleting a row", async () => {
|
||||
await request(app).delete(`/player/first/history/${firstId}x`).expect(400);
|
||||
await request(app).delete("/player/first/history").send({ ids: [firstId, "bad"] }).expect(400);
|
||||
await request(app).delete("/player/first/history").send({ all: true, ids: [firstId] }).expect(400);
|
||||
expect(db.getPlayHistory("first", 10)).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("keeps song IDs compatible and scopes deletion to the authorized bot", async () => {
|
||||
const response = await request(app).get("/player/first/history").expect(200);
|
||||
expect(response.body.history[0]).toMatchObject({ id: "song", rowId: firstId });
|
||||
await request(app).delete("/player/second/history").send({ all: true }).expect(403);
|
||||
await request(app).delete("/player/first/history").send({ ids: [firstId, firstId, secondId] }).expect(200, { deleted: 1 });
|
||||
expect(db.getPlayHistory("second", 10)).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,11 @@ import { resolveProviderForRequest } from "./provider-scope.js";
|
||||
* against a pathological catalogue (and against an upstream paging bug). */
|
||||
const MAX_ARTIST_QUEUE = 500;
|
||||
const ARTIST_QUEUE_PAGE = 100;
|
||||
const MAX_HISTORY_DELETE_BATCH = 500;
|
||||
|
||||
function isHistoryRowId(value: unknown): value is number {
|
||||
return typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
||||
}
|
||||
|
||||
/** Walks every page of an artist's catalogue (best-first, de-duplicated). */
|
||||
export async function collectArtistSongs(
|
||||
@@ -827,6 +832,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,
|
||||
@@ -840,5 +848,50 @@ 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(req.params.rowId);
|
||||
if (!/^\d+$/.test(req.params.rowId) || !isHistoryRowId(rowId)) {
|
||||
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 && body.ids === undefined) {
|
||||
const deleted = database.clearPlayHistory(req.params.botId);
|
||||
res.json({ deleted });
|
||||
return;
|
||||
}
|
||||
const { ids } = body;
|
||||
if (body.all !== undefined || !Array.isArray(ids) || ids.length === 0 ||
|
||||
ids.length > MAX_HISTORY_DELETE_BATCH || !ids.every(isHistoryRowId)) {
|
||||
res.status(400).json({ error: "Provide 1 to 500 history IDs or all:true" });
|
||||
return;
|
||||
}
|
||||
const deleted = database.deletePlayHistory(req.params.botId, ids);
|
||||
res.json({ deleted });
|
||||
}
|
||||
);
|
||||
|
||||
return router;
|
||||
}
|
||||
Reference in new issue
Block a user