mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
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:
1 parent
17008ae52e
commit
61c24cc219
20 files changed
+2568
-114
No files matched your search
+49
-2
@@ -4,8 +4,10 @@ import { YouTubeProvider } from "../../music/youtube.js";
|
||||
import type { CookieStore } from "../../music/auth.js";
|
||||
import type { Logger } from "../../logger.js";
|
||||
import type { BotConfig, JellyfinConfig } from "../../data/config.js";
|
||||
import type { BotDatabase } from "../../data/database.js";
|
||||
import { requirePermission } from "../middleware/requirePermission.js";
|
||||
import { requireNotGuest } from "../middleware/requireNotGuest.js";
|
||||
import { resolveMusicCookie } from "./provider-scope.js";
|
||||
|
||||
export function createAuthRouter(
|
||||
neteaseProvider: MusicProvider,
|
||||
@@ -16,7 +18,10 @@ export function createAuthRouter(
|
||||
kugouProvider?: MusicProvider,
|
||||
spotifyProvider?: MusicProvider,
|
||||
jellyfinProvider?: MusicProvider,
|
||||
config?: BotConfig
|
||||
config?: BotConfig,
|
||||
// Per-user music accounts (#164 follow-up). Omitted by unit-test routers →
|
||||
// every caller keeps seeing the bot's shared login status.
|
||||
database?: BotDatabase,
|
||||
): Router {
|
||||
const router = Router();
|
||||
// YouTube is auth-less; we only use this instance so /auth/status can
|
||||
@@ -36,9 +41,51 @@ export function createAuthRouter(
|
||||
try {
|
||||
const platform = req.query.platform as string;
|
||||
const provider = getProvider(platform);
|
||||
// Per-user accounts (#164 follow-up). Two different questions, two
|
||||
// different answers:
|
||||
//
|
||||
// /api/auth/status → "is the BOT's shared login alive?" This is the
|
||||
// admin console (Settings → 音乐平台), so an
|
||||
// admin always gets the shared status even if
|
||||
// they linked or were assigned a personal one.
|
||||
// /api/me/music/status → "which account does THIS user actually play
|
||||
// through?" (personal-music.ts) — that one does
|
||||
// report the personal/granted view.
|
||||
//
|
||||
// For a non-admin the shared nickname/avatar must never be returned: that
|
||||
// is exactly the leak this feature exists to close, so they get
|
||||
// loggedIn:false instead until they have an account of their own.
|
||||
const user = (req as { user?: { id?: string; role?: string } }).user;
|
||||
// A platform without per-account login support (spotify / jellyfin /
|
||||
// youtube) keeps reporting the shared status for everyone: the toggle only
|
||||
// governs sources that can actually build a per-user view.
|
||||
const perUser =
|
||||
config?.perUserMusicAccounts === true &&
|
||||
user?.role !== "admin" &&
|
||||
typeof provider.withCookie === "function";
|
||||
|
||||
if (perUser) {
|
||||
const own = resolveMusicCookie(req, provider.platform, { database, config });
|
||||
if (own && typeof provider.withCookie === "function") {
|
||||
const view = provider.withCookie(own.cookie);
|
||||
const status = await view.getAuthStatus();
|
||||
res.json({
|
||||
platform: provider.platform,
|
||||
personal: true,
|
||||
source: own.source,
|
||||
// Admin-facing name for an assigned account; absent for self-linked.
|
||||
label: own.label,
|
||||
...status,
|
||||
});
|
||||
return;
|
||||
}
|
||||
res.json({ platform: provider.platform, loggedIn: false, personal: true, source: "none" });
|
||||
return;
|
||||
}
|
||||
|
||||
const status = await provider.getAuthStatus();
|
||||
logger.debug({ platform, status }, "Auth status check");
|
||||
res.json({ platform: provider.platform, ...status });
|
||||
res.json({ platform: provider.platform, source: "shared", ...status });
|
||||
} catch (err) {
|
||||
logger.error({ err }, "Auth status check failed");
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
|
||||
Reference in new issue
Block a user