mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-02 19:02:50 +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
@@ -197,6 +197,9 @@ export interface BotDatabase {
|
||||
getUserMusicCookie(userId: string, platform: string): string | null;
|
||||
setUserMusicCookie(userId: string, platform: string, cookie: string): void;
|
||||
deleteUserMusicCookie(userId: string, platform: string): boolean;
|
||||
/** null when the member never chose; callers treat that as "prefer own". */
|
||||
getUserMusicPreference(userId: string, platform: string): { preferShared: boolean } | null;
|
||||
setUserMusicPreference(userId: string, platform: string, preferShared: boolean): void;
|
||||
// Admin-owned account pool + assignments (#164 follow-up).
|
||||
listMusicAccounts(platform?: string): MusicAccount[];
|
||||
getMusicAccount(id: string): MusicAccount | null;
|
||||
@@ -453,6 +456,20 @@ function initTables(db: Database.Database): void {
|
||||
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Which login a member wants to use for a platform (#164 follow-up).
|
||||
-- preferShared = 1 means "ignore my own / assigned account and play
|
||||
-- through the bot's shared login", which is how someone whose own account
|
||||
-- has no membership still gets to play paid tracks. Default 0 keeps the
|
||||
-- privacy-first behaviour (own account wins).
|
||||
CREATE TABLE IF NOT EXISTS user_music_prefs (
|
||||
userId TEXT NOT NULL,
|
||||
platform TEXT NOT NULL,
|
||||
preferShared INTEGER NOT NULL DEFAULT 0,
|
||||
updatedAt TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
PRIMARY KEY (userId, platform),
|
||||
FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- Admin-owned music logins that can be handed out to members (#164
|
||||
-- follow-up). Separate from user_music_cookies so a member's own QR login
|
||||
-- and an admin's assignment never overwrite each other.
|
||||
@@ -620,6 +637,14 @@ export function createDatabase(dbPath: string): BotDatabase {
|
||||
`DELETE FROM user_music_cookies WHERE userId = ? AND platform = ?`,
|
||||
);
|
||||
|
||||
const selectUserMusicPref = db.prepare(
|
||||
`SELECT preferShared FROM user_music_prefs WHERE userId = ? AND platform = ?`,
|
||||
);
|
||||
const upsertUserMusicPref = db.prepare(`
|
||||
INSERT INTO user_music_prefs (userId, platform, preferShared) VALUES (?, ?, ?)
|
||||
ON CONFLICT(userId, platform) DO UPDATE SET preferShared = excluded.preferShared, updatedAt = datetime('now')
|
||||
`);
|
||||
|
||||
// ---- Admin-owned account pool (#164 follow-up) -------------------------
|
||||
const MUSIC_ACCOUNT_GRANT_SELECT = `
|
||||
SELECT g.userId, COALESCE(u.username, g.userId) AS username, g.platform,
|
||||
@@ -925,6 +950,18 @@ export function createDatabase(dbPath: string): BotDatabase {
|
||||
return deleteUserMusicCookieStmt.run(userId, platform).changes > 0;
|
||||
},
|
||||
|
||||
getUserMusicPreference(userId, platform) {
|
||||
const row = selectUserMusicPref.get(userId, platform) as
|
||||
| { preferShared: number }
|
||||
| undefined;
|
||||
if (!row) return null;
|
||||
return { preferShared: Number(row.preferShared) === 1 };
|
||||
},
|
||||
|
||||
setUserMusicPreference(userId, platform, preferShared) {
|
||||
upsertUserMusicPref.run(userId, platform, preferShared ? 1 : 0);
|
||||
},
|
||||
|
||||
listMusicAccounts(platform) {
|
||||
const rows = platform
|
||||
? (selectMusicAccountsByPlatform.all(platform) as Record<string, unknown>[])
|
||||
|
||||
Reference in new issue
Block a user