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:
jiayuLian committed 2026-11-08 18:10:08 +08:00
1 parent 1ef80be883
commit d3eb6f2107
6 files changed
+544 -3

No files matched your search

+63 -2
View File
@@ -102,6 +102,25 @@ export function grantedAccountFor(
return deps.database.getGrantedMusicAccount(user.id!, platform);
}
/**
* Whether the member explicitly asked to play through the shared login.
*
* This exists for one concrete case: their own account has no membership, the
* admin's does, and they would rather hear the paid tracks than their own
* recommendations. Choosing it overrides BOTH their own link and any admin
* assignment — it is an explicit instruction, not a fallback.
*/
export function preferSharedFor(
req: Request | undefined,
platform: string,
deps: MusicScopeDeps,
): boolean {
if (!perUserMusicEnabled(deps)) return false;
const user = scopedUser(req, deps);
if (!user || !deps.database) return false;
return deps.database.getUserMusicPreference(user.id!, platform)?.preferShared === true;
}
/** Where the cookie driving this request came from. */
export interface ResolvedMusicCookie {
source: "personal" | "granted";
@@ -122,6 +141,10 @@ export function resolveMusicCookie(
deps: MusicScopeDeps,
options: { alwaysPersonal?: boolean } = {},
): ResolvedMusicCookie | null {
// An explicit "use the shared account" outranks everything, including the
// `alwaysPersonal` escape hatch: someone who chose membership over their own
// recommendations wants the shared login for FM too.
if (preferSharedFor(req, platform, deps)) return null;
const own = personalCookieFor(req, platform, deps, options.alwaysPersonal);
if (own) return { source: "personal", cookie: own };
const granted = grantedAccountFor(req, platform, deps);
@@ -136,6 +159,39 @@ export function resolveMusicCookie(
return null;
}
/**
* Give `view` a single-shot fallback to the shared login when its own playback
* attempt comes up empty.
*
* The case this exists for: the member's own account is real but has no
* membership, so `/song/url/v1` answers `url: null` (or 400 for the paid track)
* while the admin's account plays it fine. Without this the member has to go
* into settings and switch manually before every such track.
*
* Only `getSongUrl` is wrapped — search, playlists and FM stay on the member's
* own account, which is the whole point of per-user logins. The wrapper keeps
* the shared instance untouched, so nothing leaks between callers.
*/
function withSharedPlaybackFallback(view: MusicProvider, shared: MusicProvider): MusicProvider {
// Guard for partial implementations (and for test doubles): without both
// sides resolvable there is nothing to fall back to.
if (typeof view.getSongUrl !== "function" || typeof shared.getSongUrl !== "function") {
return view;
}
const ownResolve = view.getSongUrl.bind(view);
view.getSongUrl = async (songId: string, quality?: string) => {
try {
const result = await ownResolve(songId, quality);
if (result?.url) return result;
} catch {
// A failure on the personal side (no membership, -460, expired cookie)
// is not a reason for this track to go grey — try the shared login.
}
return shared.getSongUrl(songId, quality);
};
return view;
}
/**
* Wrap `shared` in a per-account view when the request carries a usable login;
* otherwise return `shared` untouched.
@@ -149,14 +205,19 @@ export function resolveProviderForRequest(
platform: string,
shared: MusicProvider,
deps: MusicScopeDeps,
options: { alwaysPersonal?: boolean } = {},
options: { alwaysPersonal?: boolean; fallbackToShared?: boolean } = {},
): MusicProvider {
const resolved = resolveMusicCookie(req, platform, deps, options);
if (!resolved) return shared;
if (typeof shared.withCookie !== "function") return shared;
let view: MusicProvider;
try {
return shared.withCookie(resolved.cookie);
view = shared.withCookie(resolved.cookie);
} catch {
return shared;
}
// On by default: the common complaint is "my account has no membership, the
// admin's does". Set false to keep playback strictly on the member's account.
if (options.fallbackToShared === false) return view;
return withSharedPlaybackFallback(view, shared);
}