feat(music): per-user music accounts with an admin-owned account pool

Every member saw the admin's library: the bot keeps one shared login per
platform (data/cookies/<platform>.json) and search, playlists and playback
URLs all read through it. #164 added a personal NetEase login, but only for
私人FM — the rest still came from the shared account.

On top of that, most members do not own a paid login, so "everyone links their
own" is not enough: the admin has to be able to hand out accounts.

What this adds, behind one admin switch (`perUserMusicAccounts`, default off):

- One login per request, resolved in this order: the caller's OWN linked
  account, then an account an admin ASSIGNED, then the bot's shared login.
- An admin-owned pool (`music_accounts`) plus per-member assignments
  (`music_account_grants`, PK userId+platform = one per platform). Assignments
  are capacity-checked inside a transaction, so an exclusive account cannot be
  handed to two members by two concurrent clicks. `enabled = 0` is an instant
  rollback of every grant without deleting them.
- Own-beats-assigned, and the two live in separate tables: a member scanning
  their own QR code never overwrites an admin's assignment, and unlinking on
  the member side never revokes what the admin granted.
- QQ / Kugou / Bilibili now implement `withCookie`, so all four sources can be
  isolated. Only NetEase exposes QR self-linking (it is the only one with
  `pollQrLogin`); the rest accept an assigned account, which is the workflow
  that actually matters for members without a paid login.
- `/api/music-accounts` is admin-only (`requireAdmin`, not `bot.manage`):
  members must never see which account was handed to whom. Cookies are masked
  in every response.

Switch off = no behaviour change at all, except 私人FM keeps the #164 behaviour
through `alwaysPersonal`.
This commit is contained in:
jiayuLian committed 2026-11-08 17:07:42 +08:00
1 parent 17008ae52e
commit 61c24cc219
20 files changed
+2568 -114

No files matched your search

+15
View File
@@ -130,6 +130,16 @@ export interface BotConfig {
* persisted/restored until an operator opts in.
*/
savedQueuesEnabled: boolean;
/**
* Per-user music accounts (admin-controlled, default false). When on, a web
* user who linked their own platform account (Settings → 我的音乐账号) reads
* search results, playlists, daily recommendations and playback URLs from
* THAT account instead of the bot's shared login — so a member stops seeing
* the admin's library. When off (or when the user has no linked account, or
* the source has no per-account login) everything falls back to the shared
* login, which is exactly the pre-existing behaviour.
*/
perUserMusicAccounts: boolean;
/**
* When true, a single-song immediate !play (chat) / play-song (web) inserts
* after the current track and jumps to it instead of clearing the queue, so
@@ -191,6 +201,7 @@ export function getDefaultConfig(): BotConfig {
idleTimeoutMinutes: 0,
localAudioEnabled: true,
savedQueuesEnabled: false,
perUserMusicAccounts: false,
playKeepsQueue: false,
publicUrl: "",
trustProxy: false,
@@ -394,6 +405,9 @@ export function loadConfig(path: string): BotConfig {
// them (`"yes"`, `1`, `null` → false; only a literal `true` enables).
const savedQueuesEnabled = partial.savedQueuesEnabled === true;
const playKeepsQueue = partial.playKeepsQueue === true;
// Same strict-coercion rule: a legacy config without the key, or a
// hand-edited `"yes"`/`1`, must never turn account isolation on by accident.
const perUserMusicAccounts = partial.perUserMusicAccounts === true;
// Voice ducking is opt-in and the retained-volume percentage is consumed
// directly by the audio path. Only a plain-object block with correctly
@@ -456,6 +470,7 @@ export function loadConfig(path: string): BotConfig {
audioQuality,
enabledProviders,
savedQueuesEnabled,
perUserMusicAccounts,
playKeepsQueue,
voiceDucking,
defaultPlatform: defaultPlatformPref,