mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-10-02 04:52:50 +08:00
feat: add configurable default music source (#126)
Make the default playback source a user-configurable setting so servers that mostly play e.g. Bilibili no longer need to type `-b` on every `!play`. Previously defaultPlatform() always picked the first enabled provider by a fixed priority order, with no way to override it. - config: add optional `defaultPlatform: GateableProvider | null`. loadConfig sanitizes it — kept only when it names a known provider that is also currently enabled, else null. defaultPlatform() returns the preference when enabled, otherwise falls back to the fixed priority order. - POST /api/bot/settings accepts `defaultPlatform` (validated against the possibly-updated enabledProviders; null/"" clears it), and reconciles a stored default that a new enabledProviders list no longer allows. GET and POST responses expose the field. - WebUI: new "默认音源" section with a source picker; saving refreshes the store's default source so it takes effect immediately without a restart. - Tests: extend config defaultPlatform priority tests and add coverage for the settings endpoint and /providers routing. - README: document `defaultPlatform` in the enabledProviders section. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
051171b019
commit
987513a5f6
7 files changed
+279
-9
No files matched your search
@@ -82,6 +82,61 @@ describe("config", () => {
|
||||
expect(defaultPlatform(config)).toBe("netease");
|
||||
});
|
||||
|
||||
// --- #126: an explicit operator default source ---
|
||||
|
||||
it("defaultPlatform is null by default (follow the priority order)", () => {
|
||||
expect(getDefaultConfig().defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("defaultPlatform() honors an explicit, enabled preference over the priority order", () => {
|
||||
const config = getDefaultConfig();
|
||||
// Priority would pick netease; a Bilibili-loving server sets B站 instead (#126).
|
||||
config.defaultPlatform = "bilibili";
|
||||
expect(defaultPlatform(config)).toBe("bilibili");
|
||||
});
|
||||
|
||||
it("defaultPlatform() ignores a preference whose source is not enabled", () => {
|
||||
const config = getDefaultConfig();
|
||||
config.defaultPlatform = "jellyfin"; // opt-in, not enabled in the default config
|
||||
// Falls back to the fixed priority order (netease)…
|
||||
expect(defaultPlatform(config)).toBe("netease");
|
||||
// …until the preferred source is actually enabled.
|
||||
config.enabledProviders = [...config.enabledProviders, "jellyfin"];
|
||||
expect(defaultPlatform(config)).toBe("jellyfin");
|
||||
});
|
||||
|
||||
it("loadConfig keeps a valid, enabled defaultPlatform", () => {
|
||||
const dir = makeTmpDir();
|
||||
const path = join(dir, "config.json");
|
||||
writeFileSync(path, JSON.stringify({ defaultPlatform: "bilibili" }));
|
||||
const config = loadConfig(path);
|
||||
expect(config.defaultPlatform).toBe("bilibili");
|
||||
expect(defaultPlatform(config)).toBe("bilibili");
|
||||
});
|
||||
|
||||
it("loadConfig nulls a defaultPlatform that is unknown, disabled, or the wrong type", () => {
|
||||
const dir = makeTmpDir();
|
||||
// Unknown provider name.
|
||||
const p1 = join(dir, "c1.json");
|
||||
writeFileSync(p1, JSON.stringify({ defaultPlatform: "bogus" }));
|
||||
expect(loadConfig(p1).defaultPlatform).toBeNull();
|
||||
// Known provider, but not in enabledProviders.
|
||||
const p2 = join(dir, "c2.json");
|
||||
writeFileSync(p2, JSON.stringify({ enabledProviders: ["netease"], defaultPlatform: "bilibili" }));
|
||||
expect(loadConfig(p2).defaultPlatform).toBeNull();
|
||||
// Wrong type.
|
||||
const p3 = join(dir, "c3.json");
|
||||
writeFileSync(p3, JSON.stringify({ defaultPlatform: 42 }));
|
||||
expect(loadConfig(p3).defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("round-trips defaultPlatform through save/load", () => {
|
||||
const dir = makeTmpDir();
|
||||
const path = join(dir, "config.json");
|
||||
saveConfig(path, { ...getDefaultConfig(), defaultPlatform: "qq" });
|
||||
expect(loadConfig(path).defaultPlatform).toBe("qq");
|
||||
});
|
||||
|
||||
it("respects an explicit jellyfin-only enabledProviders from disk", () => {
|
||||
const dir = makeTmpDir();
|
||||
const path = join(dir, "config.json");
|
||||
|
||||
+38
-7
@@ -61,15 +61,24 @@ export function isProviderEnabled(config: BotConfig, platform: string): boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* The default platform for !play/!add/!playlist/!album and all REST/WebUI calls:
|
||||
* the first enabled provider in a fixed priority order (netease with the default
|
||||
* config; jellyfin ranks after the online music platforms because it is an
|
||||
* opt-in source, but ahead of the video sites for users who run it as their
|
||||
* only music library). Falls back to "netease" when nothing is enabled so
|
||||
* callers always get a provider — the enabled-gate then produces the friendly
|
||||
* error.
|
||||
* The default platform for !play/!add/!playlist/!album and all REST/WebUI calls.
|
||||
*
|
||||
* An explicit user preference (`config.defaultPlatform`) wins whenever it points
|
||||
* at a source that is currently enabled — this lets e.g. a Bilibili-loving server
|
||||
* set B站 as the default so `!play <歌名>` needs no `-b` flag (issue #126). The
|
||||
* enabled-guard here matters at runtime too: if the operator later disables the
|
||||
* preferred source, we must fall through instead of returning a dead default.
|
||||
*
|
||||
* With no (usable) preference we fall back to the first enabled provider in a
|
||||
* fixed priority order (netease with the default config; jellyfin ranks after
|
||||
* the online music platforms because it is an opt-in source, but ahead of the
|
||||
* video sites for users who run it as their only music library). Falls back to
|
||||
* "netease" when nothing is enabled so callers always get a provider — the
|
||||
* enabled-gate then produces the friendly error.
|
||||
*/
|
||||
export function defaultPlatform(config: BotConfig): GateableProvider {
|
||||
const pref = config.defaultPlatform;
|
||||
if (pref && config.enabledProviders.includes(pref)) return pref;
|
||||
for (const p of ["netease", "qq", "kugou", "jellyfin", "bilibili", "youtube"] as const) {
|
||||
if (config.enabledProviders.includes(p)) return p;
|
||||
}
|
||||
@@ -110,6 +119,14 @@ export interface BotConfig {
|
||||
* API servers must not start (or bind ports 3001/3200) unless enabled.
|
||||
*/
|
||||
enabledProviders: GateableProvider[];
|
||||
/**
|
||||
* Optional operator-chosen default source for commands/REST/WebUI calls that
|
||||
* omit a platform (issue #126). When set to an enabled gateable provider it
|
||||
* overrides the fixed priority order in defaultPlatform(); `null` (the default)
|
||||
* keeps that priority order. loadConfig cleans stale/unknown/disabled values
|
||||
* back to null.
|
||||
*/
|
||||
defaultPlatform: GateableProvider | null;
|
||||
}
|
||||
|
||||
export function getDefaultConfig(): BotConfig {
|
||||
@@ -163,6 +180,7 @@ export function getDefaultConfig(): BotConfig {
|
||||
userId: "",
|
||||
},
|
||||
enabledProviders: ["netease", "qq", "bilibili", "youtube", "kugou"],
|
||||
defaultPlatform: null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -317,6 +335,18 @@ export function loadConfig(path: string): BotConfig {
|
||||
)
|
||||
: defaults.enabledProviders;
|
||||
|
||||
// defaultPlatform → an explicit operator default (issue #126). Keep it only
|
||||
// when it names a KNOWN gateable provider that is ALSO currently enabled;
|
||||
// anything else (unknown value, disabled source, wrong type, missing) becomes
|
||||
// null so defaultPlatform() falls back to the fixed priority order.
|
||||
const rawDefault = partial.defaultPlatform;
|
||||
const defaultPlatformPref: GateableProvider | null =
|
||||
typeof rawDefault === "string" &&
|
||||
(GATEABLE_PROVIDERS as readonly string[]).includes(rawDefault) &&
|
||||
enabledProviders.includes(rawDefault as GateableProvider)
|
||||
? (rawDefault as GateableProvider)
|
||||
: null;
|
||||
|
||||
return {
|
||||
...defaults,
|
||||
...partial,
|
||||
@@ -325,6 +355,7 @@ export function loadConfig(path: string): BotConfig {
|
||||
spotify,
|
||||
jellyfin,
|
||||
enabledProviders,
|
||||
defaultPlatform: defaultPlatformPref,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -647,4 +647,54 @@ describe("bot router /settings jellyfin block + enabledProviders", () => {
|
||||
// No jellyfin block in the request → no reconfigure call.
|
||||
expect(configureCalls).toHaveLength(0);
|
||||
});
|
||||
|
||||
// --- #126: operator-chosen default source ---
|
||||
|
||||
it("GET /settings exposes defaultPlatform (null by default)", async () => {
|
||||
const res = await request(mountBot()).get("/api/bot/settings");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings sets an enabled defaultPlatform and persists it", async () => {
|
||||
const res = await request(mountBot()).post("/api/bot/settings").send({
|
||||
defaultPlatform: "bilibili",
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.defaultPlatform).toBe("bilibili");
|
||||
expect(config.defaultPlatform).toBe("bilibili");
|
||||
const onDisk = JSON.parse(readFileSync(configPath, "utf-8"));
|
||||
expect(onDisk.defaultPlatform).toBe("bilibili");
|
||||
});
|
||||
|
||||
it("POST /settings ignores an unknown or disabled defaultPlatform", async () => {
|
||||
const app = mountBot();
|
||||
// jellyfin is opt-in and not enabled in the default config → rejected.
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "jellyfin" });
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
// Unknown value → rejected.
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "bogus" });
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings clears defaultPlatform with null", async () => {
|
||||
const app = mountBot();
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "qq" });
|
||||
expect(config.defaultPlatform).toBe("qq");
|
||||
const res = await request(app).post("/api/bot/settings").send({ defaultPlatform: null });
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
|
||||
it("POST /settings drops a default whose source gets disabled in the same request", async () => {
|
||||
const app = mountBot();
|
||||
await request(app).post("/api/bot/settings").send({ defaultPlatform: "qq" });
|
||||
expect(config.defaultPlatform).toBe("qq");
|
||||
// Disabling qq via enabledProviders clears the now-invalid default.
|
||||
const res = await request(app).post("/api/bot/settings").send({
|
||||
enabledProviders: ["netease", "bilibili"],
|
||||
});
|
||||
expect(res.body.defaultPlatform).toBeNull();
|
||||
expect(config.defaultPlatform).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -77,6 +77,7 @@ export function createBotRouter(
|
||||
spotify: maskedSpotify(),
|
||||
jellyfin: maskedJellyfin(),
|
||||
enabledProviders: config.enabledProviders,
|
||||
defaultPlatform: config.defaultPlatform,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -175,6 +176,29 @@ export function createBotRouter(
|
||||
);
|
||||
}
|
||||
|
||||
// defaultPlatform (issue #126): the operator-chosen default source for
|
||||
// platform-less commands/REST/WebUI calls. Reconciled AFTER enabledProviders
|
||||
// so both are validated against the same (possibly updated) enabled list:
|
||||
// 1) Drop a stored default that the new enabledProviders no longer allows,
|
||||
// keeping the persisted config consistent with loadConfig's invariant.
|
||||
// 2) Apply an explicit change — `null`/`""` clears it (back to priority
|
||||
// order); a known+enabled provider sets it; anything else is ignored.
|
||||
if (config.defaultPlatform && !config.enabledProviders.includes(config.defaultPlatform)) {
|
||||
config.defaultPlatform = null;
|
||||
}
|
||||
if ("defaultPlatform" in req.body) {
|
||||
const dp = req.body.defaultPlatform;
|
||||
if (dp === null || dp === "") {
|
||||
config.defaultPlatform = null;
|
||||
} else if (
|
||||
typeof dp === "string" &&
|
||||
(GATEABLE_PROVIDERS as readonly string[]).includes(dp) &&
|
||||
config.enabledProviders.includes(dp as GateableProvider)
|
||||
) {
|
||||
config.defaultPlatform = dp as GateableProvider;
|
||||
}
|
||||
}
|
||||
|
||||
saveConfig(configPath, config);
|
||||
|
||||
// Hot-apply the (possibly re-pointed) Jellyfin connection to the live
|
||||
@@ -219,6 +243,7 @@ export function createBotRouter(
|
||||
spotify: maskedSpotify(),
|
||||
jellyfin: maskedJellyfin(),
|
||||
enabledProviders: config.enabledProviders,
|
||||
defaultPlatform: config.defaultPlatform,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -118,6 +118,25 @@ describe("music router provider gating (enabledProviders) + jellyfin endpoints",
|
||||
expect(res.body.enabled).not.toContain("spotify"); // spotify.enabled defaults off
|
||||
});
|
||||
|
||||
it("GET /providers reports a configured defaultPlatform override (#126)", async () => {
|
||||
const config = getDefaultConfig();
|
||||
config.defaultPlatform = "qq"; // operator prefers QQ over the priority order
|
||||
const { app } = mount(config);
|
||||
const res = await request(app).get("/api/music/providers");
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.default).toBe("qq");
|
||||
});
|
||||
|
||||
it("routes a platform-less /search to the configured defaultPlatform (#126)", async () => {
|
||||
const config = getDefaultConfig();
|
||||
config.defaultPlatform = "bilibili";
|
||||
const { app, netease } = mount(config);
|
||||
const res = await request(app).get("/api/music/search?q=hello");
|
||||
expect(res.status).toBe(200);
|
||||
// Default is now bilibili, so the netease provider must NOT be hit.
|
||||
expect(netease.search).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/** Default config plus the opt-in jellyfin source enabled. */
|
||||
function configWithJellyfin() {
|
||||
const config = getDefaultConfig();
|
||||
|
||||
Reference in new issue
Block a user