mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +08:00
fix(qq): check collection playback with the room account
This commit is contained in:
1 parent
2db8c4cc05
commit
20ad2d44a7
2 files changed
+69
-37
No files matched your search
@@ -6,6 +6,7 @@ import request from "supertest";
|
||||
import { createPlayerRouter } from "./player.js";
|
||||
import { BotInstance } from "../../bot/instance.js";
|
||||
import { PlayQueue } from "../../audio/queue.js";
|
||||
import type { BotDatabase } from "../../data/database.js";
|
||||
|
||||
function song(id: string): Song {
|
||||
return {
|
||||
@@ -137,6 +138,55 @@ describe("collection playback serialization", () => {
|
||||
});
|
||||
|
||||
describe("collection preflight", () => {
|
||||
it.each(["playlist", "album", "artist"])("checks QQ %s availability using the room's playback account", async kind => {
|
||||
const queue = new PlayQueue();
|
||||
const personalTracks = [{ ...song("personal-library-track"), platform: "qq" }];
|
||||
const readLibrary = vi.fn(async () => personalTracks);
|
||||
const personal = {
|
||||
platform: "qq",
|
||||
getPlaylistSongs: readLibrary,
|
||||
getAlbumSongs: readLibrary,
|
||||
getArtistSongs: readLibrary,
|
||||
getPlayableSongIds: vi.fn(async () => new Set<string>()),
|
||||
};
|
||||
const shared = {
|
||||
platform: "qq",
|
||||
withCookie: vi.fn(() => personal),
|
||||
getPlayableSongIds: vi.fn(async () => new Set(["personal-library-track"])),
|
||||
};
|
||||
const bot = {
|
||||
getProviderFor: () => shared,
|
||||
getQueueManager: () => queue,
|
||||
getPlayer: () => ({ stop: vi.fn(), resetFailures: vi.fn() }),
|
||||
runExclusive: (work: () => Promise<unknown>) => work(),
|
||||
resolveAndPlay: vi.fn(async () => true),
|
||||
emit: vi.fn(),
|
||||
};
|
||||
const db = {
|
||||
getUserMusicPreference: () => null,
|
||||
getUserMusicCookie: () => null,
|
||||
getGrantedMusicAccount: () => ({ cookie: "private-test-cookie", label: "Member's account", accountId: "account" }),
|
||||
} as unknown as BotDatabase;
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use((req, _res, next) => {
|
||||
(req as { user?: unknown }).user = { id: "member", role: "member", bots: new Set(["b"]), capabilities: new Set(["player.control"]) };
|
||||
next();
|
||||
});
|
||||
app.use("/api/player", createPlayerRouter({ getBot: () => bot } as any, { error: vi.fn() } as any,
|
||||
db, undefined, undefined, undefined, { perUserMusicAccounts: true }));
|
||||
|
||||
const response = await request(app).post(`/api/player/b/play-${kind}`)
|
||||
.send({ [`${kind}Id`]: "id", platform: "qq" }).expect(200);
|
||||
|
||||
expect(response.body).toMatchObject({ ok: true });
|
||||
expect(response.text).not.toContain("private-test-cookie");
|
||||
expect(readLibrary).toHaveBeenCalledOnce();
|
||||
expect(personal.getPlayableSongIds).not.toHaveBeenCalled();
|
||||
expect(shared.getPlayableSongIds).toHaveBeenCalledWith(["personal-library-track"]);
|
||||
expect(queue.current()?.id).toBe("personal-library-track");
|
||||
});
|
||||
|
||||
it.each([
|
||||
["playlist", "empty", 200],
|
||||
["playlist", "unavailable", 200],
|
||||
@@ -169,6 +219,7 @@ describe("collection preflight", () => {
|
||||
const response = await request(app).post(`/api/player/b/play-${kind}`).send({ [`${kind}Id`]: "id", platform: "qq" });
|
||||
|
||||
expect(response.status).toBe(status);
|
||||
if (status === 200) expect(response.body).toMatchObject({ ok: false });
|
||||
expect(player.stop).not.toHaveBeenCalled();
|
||||
expect(player.resetFailures).not.toHaveBeenCalled();
|
||||
expect(queue.current()?.id).toBe("playing");
|
||||
|
||||
+18
-37
@@ -43,6 +43,21 @@ export async function collectArtistSongs(
|
||||
return songs;
|
||||
}
|
||||
|
||||
async function filterCollectionForRoom(
|
||||
bot: BotInstance,
|
||||
platform: Song["platform"],
|
||||
songs: Song[],
|
||||
): Promise<Song[]> {
|
||||
if (platform !== "qq") return songs;
|
||||
// Metadata can come from the caller's account. Room playback uses the
|
||||
// bot's shared account, so its entitlements must drive the batch check too.
|
||||
const playbackProvider = bot.getProviderFor("qq") as MusicProvider & {
|
||||
getPlayableSongIds?: (ids: string[]) => Promise<Set<string> | null>;
|
||||
};
|
||||
const available = await playbackProvider.getPlayableSongIds?.(songs.map(song => song.id));
|
||||
return available ? songs.filter(song => available.has(song.id)) : songs;
|
||||
}
|
||||
|
||||
export function createPlayerRouter(
|
||||
botManager: BotManager,
|
||||
logger: Logger,
|
||||
@@ -423,24 +438,8 @@ export function createPlayerRouter(
|
||||
return;
|
||||
}
|
||||
|
||||
// QQ-specific optimization: many users' QQ playlists contain a
|
||||
// large fraction of songs that return result=104003 (region/copyright
|
||||
// restricted). Batch-resolve URLs once and only queue the playable
|
||||
// ones, otherwise the playback retry loop wastes time guessing.
|
||||
let queueable = songs;
|
||||
const totalCount = songs.length;
|
||||
const qqLike = provider as { getPlayableSongIds?: (ids: string[]) => Promise<Set<string> | null> };
|
||||
if (typeof qqLike.getPlayableSongIds === "function") {
|
||||
const playable = await qqLike.getPlayableSongIds(songs.map((s: { id: string }) => s.id));
|
||||
if (playable !== null) {
|
||||
// Authoritative answer from upstream — even an empty set means
|
||||
// "we know none are playable", short-circuit immediately rather
|
||||
// than wasting 20+ retries.
|
||||
queueable = songs.filter((s: { id: string }) => playable.has(s.id));
|
||||
}
|
||||
// If null, the batch endpoint itself errored — fall through to
|
||||
// the sequential retry path, which still has a chance.
|
||||
}
|
||||
const queueable = await filterCollectionForRoom(bot, provider.platform, songs);
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `歌单 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
@@ -471,17 +470,8 @@ export function createPlayerRouter(
|
||||
return;
|
||||
}
|
||||
|
||||
// QQ-specific optimization: batch-resolve playable IDs to avoid
|
||||
// wasting retries on region/copyright-restricted tracks.
|
||||
let queueable = songs;
|
||||
const totalCount = songs.length;
|
||||
const qqLike = provider as { getPlayableSongIds?: (ids: string[]) => Promise<Set<string> | null> };
|
||||
if (typeof qqLike.getPlayableSongIds === "function") {
|
||||
const playable = await qqLike.getPlayableSongIds(songs.map((s: { id: string }) => s.id));
|
||||
if (playable !== null) {
|
||||
queueable = songs.filter((s: { id: string }) => playable.has(s.id));
|
||||
}
|
||||
}
|
||||
const queueable = await filterCollectionForRoom(bot, provider.platform, songs);
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `专辑 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
@@ -528,17 +518,8 @@ export function createPlayerRouter(
|
||||
return;
|
||||
}
|
||||
|
||||
// Same QQ batch-resolve optimization as play-album: drop tracks that are
|
||||
// region/copyright blocked instead of burning retries on them.
|
||||
let queueable = songs;
|
||||
const totalCount = songs.length;
|
||||
const qqLike = provider as { getPlayableSongIds?: (ids: string[]) => Promise<Set<string> | null> };
|
||||
if (typeof qqLike.getPlayableSongIds === "function") {
|
||||
const playable = await qqLike.getPlayableSongIds(songs.map((s: { id: string }) => s.id));
|
||||
if (playable !== null) {
|
||||
queueable = songs.filter((s: { id: string }) => playable.has(s.id));
|
||||
}
|
||||
}
|
||||
const queueable = await filterCollectionForRoom(bot, provider.platform, songs);
|
||||
if (queueable.length === 0) {
|
||||
res.json({ ok: false, message: `歌手 ${totalCount} 首歌曲均无法播放` });
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user