mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
feat(music): let a member choose the shared login when their own has no VIP
The order was hardcoded: own account → assigned account → shared. That is the right default (privacy first), but it leaves one common case stuck: a member's own NetEase login is real but has no membership, while the admin's does. They linked their account, so playback keeps using theirs, and every paid track fails — with no way to say "just use the admin's for playback". Two things now: 1. An explicit choice — `user_music_prefs` (userId + platform), exposed as GET/PUT `/api/me/music/:platform/preference` and a radio in the account card. `preferShared: true` outranks EVERYTHING, including `alwaysPersonal` (someone who chose membership over their own recommendations wants the shared login for FM too). It stores a flag, not a cookie — flipping it back restores the personal account untouched. 2. An automatic fallback for people who never touch the switch. Only `getSongUrl` is wrapped: if the personal account returns `url: null` or throws, the same track is retried once on the shared login. Search, playlists and FM stay on the member's own account — that is the point of per-user logins, and widening the fallback would leak their library back to the admin's. Details: - The fallback lives in `resolveProviderForRequest`, so every playback entry point (web click, chat !play, auto-advance, playlists) gets it by construction, and the shared instance is never mutated. - `fallbackToShared: false` is available for callers that must stay strictly on the member's account; the default is on, because "my account has no VIP" is the complaint that produced this feature. - The status payload now carries `preferShared` and `featureEnabled`, so the UI can render the radio without a second round-trip and hide it when the admin has the feature off (PUT answers 409 in that case). 13 new tests: preference overrides own + assigned + alwaysPersonal, feature-off ignored, GET/PUT round-trip, per-platform isolation, stored cookie untouched, non-boolean rejected, 409 when disabled; fallback on null, on throw, keeps the personal result when it works, can be disabled, leaves the shared instance untouched.
This commit is contained in:
1 parent
1ef80be883
commit
d3eb6f2107
6 files changed
+544
-3
No files matched your search
@@ -25,10 +25,16 @@ function fakeProvider(opts: {
|
||||
* `own` is the member's self-linked cookie, `granted` an admin assignment.
|
||||
* Both default to null so a test only states the state it cares about.
|
||||
*/
|
||||
function fakeDb(own: string | null, granted: GrantedMusicAccount | null = null): BotDatabase {
|
||||
function fakeDb(
|
||||
own: string | null,
|
||||
granted: GrantedMusicAccount | null = null,
|
||||
preferShared = false,
|
||||
): BotDatabase {
|
||||
return {
|
||||
getUserMusicCookie: () => own,
|
||||
getGrantedMusicAccount: () => granted,
|
||||
getUserMusicPreference: () =>
|
||||
preferShared ? { preferShared: true } : null,
|
||||
} as unknown as BotDatabase;
|
||||
}
|
||||
|
||||
@@ -298,3 +304,157 @@ describe("provider-scope", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// "Play through the shared account" preference
|
||||
//
|
||||
// The real case: the member's own NetEase login has no VIP, the admin's does.
|
||||
// Switching to shared must override BOTH their own link and any assignment —
|
||||
// it is an explicit instruction, not a fallback.
|
||||
// ---------------------------------------------------------------------------
|
||||
describe("provider-scope — shared-account preference", () => {
|
||||
it("ignores the member's own cookie when they chose the shared login", () => {
|
||||
const resolved = resolveMusicCookie(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
{
|
||||
database: fakeDb("MUSIC_U=mine", null, true),
|
||||
config: { perUserMusicAccounts: true },
|
||||
},
|
||||
);
|
||||
expect(resolved).toBeNull();
|
||||
});
|
||||
|
||||
it("also overrides an admin assignment", () => {
|
||||
const resolved = resolveMusicCookie(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
{
|
||||
database: fakeDb(null, GRANTED, true),
|
||||
config: { perUserMusicAccounts: true },
|
||||
},
|
||||
);
|
||||
expect(resolved).toBeNull();
|
||||
});
|
||||
|
||||
it("outranks alwaysPersonal, so personal FM follows the choice too", () => {
|
||||
const resolved = resolveMusicCookie(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
{
|
||||
database: fakeDb("MUSIC_U=mine", null, true),
|
||||
config: { perUserMusicAccounts: true },
|
||||
},
|
||||
{ alwaysPersonal: true },
|
||||
);
|
||||
expect(resolved).toBeNull();
|
||||
});
|
||||
|
||||
it("hands the untouched shared provider back", () => {
|
||||
const shared = fakeProvider({ withCookie: () => fakeProvider() });
|
||||
const scoped = resolveProviderForRequest(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
shared,
|
||||
{
|
||||
database: fakeDb("MUSIC_U=mine", null, true),
|
||||
config: { perUserMusicAccounts: true },
|
||||
},
|
||||
);
|
||||
expect(scoped).toBe(shared);
|
||||
});
|
||||
|
||||
it("is ignored while the admin has the feature off", () => {
|
||||
const shared = fakeProvider({ withCookie: () => fakeProvider() });
|
||||
const scoped = resolveProviderForRequest(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
shared,
|
||||
{
|
||||
database: fakeDb("MUSIC_U=mine", null, true),
|
||||
config: { perUserMusicAccounts: false },
|
||||
},
|
||||
);
|
||||
expect(scoped).toBe(shared);
|
||||
});
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Automatic playback fallback
|
||||
//
|
||||
// Same scenario, minus an explicit choice: keep the member's own account for
|
||||
// search / playlists, but when THIS track cannot be resolved on it, try the
|
||||
// shared login instead of greying the track out.
|
||||
// ---------------------------------------------------------------------------
|
||||
describe("provider-scope — playback falls back to the shared login", () => {
|
||||
function view(
|
||||
getSongUrl: (songId: string, quality?: string) => Promise<{ url: string } | null>,
|
||||
): MusicProvider {
|
||||
return { platform: "netease", getSongUrl } as unknown as MusicProvider;
|
||||
}
|
||||
|
||||
function scopeWith(
|
||||
personal: MusicProvider,
|
||||
sharedUrl: () => Promise<{ url: string } | null>,
|
||||
options: { fallbackToShared?: boolean } = {},
|
||||
) {
|
||||
const shared = view(sharedUrl);
|
||||
(shared as Partial<MusicProvider>).withCookie = () => personal;
|
||||
return resolveProviderForRequest(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
shared,
|
||||
{
|
||||
database: fakeDb("MUSIC_U=mine"),
|
||||
config: { perUserMusicAccounts: true },
|
||||
},
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
it("uses the shared login when the personal account returns no url", async () => {
|
||||
const scoped = scopeWith(view(async () => null), async () => ({ url: "https://shared/x.mp3" }));
|
||||
expect((await scoped.getSongUrl("1"))?.url).toBe("https://shared/x.mp3");
|
||||
});
|
||||
|
||||
it("uses the shared login when the personal account throws", async () => {
|
||||
const scoped = scopeWith(
|
||||
view(async () => {
|
||||
throw new Error("no membership");
|
||||
}),
|
||||
async () => ({ url: "https://shared/x.mp3" }),
|
||||
);
|
||||
expect((await scoped.getSongUrl("1"))?.url).toBe("https://shared/x.mp3");
|
||||
});
|
||||
|
||||
it("keeps the personal result when it works", async () => {
|
||||
const scoped = scopeWith(
|
||||
view(async () => ({ url: "https://mine/x.mp3" })),
|
||||
async () => ({ url: "https://shared/x.mp3" }),
|
||||
);
|
||||
expect((await scoped.getSongUrl("1"))?.url).toBe("https://mine/x.mp3");
|
||||
});
|
||||
|
||||
it("can be switched off so playback stays on the member's account", async () => {
|
||||
const scoped = scopeWith(
|
||||
view(async () => null),
|
||||
async () => ({ url: "https://shared/x.mp3" }),
|
||||
{ fallbackToShared: false },
|
||||
);
|
||||
expect(await scoped.getSongUrl("1")).toBeNull();
|
||||
});
|
||||
|
||||
it("leaves the shared instance itself untouched", async () => {
|
||||
const shared = view(async () => ({ url: "https://shared/x.mp3" }));
|
||||
(shared as Partial<MusicProvider>).withCookie = () => view(async () => null);
|
||||
resolveProviderForRequest(
|
||||
fakeReq({ id: "u1", role: "member" }),
|
||||
"netease",
|
||||
shared,
|
||||
{ database: fakeDb("MUSIC_U=mine"), config: { perUserMusicAccounts: true } },
|
||||
);
|
||||
// Calling the shared provider directly must still hit its own implementation.
|
||||
expect((await shared.getSongUrl("1"))?.url).toBe("https://shared/x.mp3");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user