Files
plezy/lib/profiles/active_plex_token.dart
edde746 cf0dd0607e fix(profiles): keep playback preferences bound to their authenticated account
Picker ordering could select another account for playback preferences, and delayed operations could publish data after the active account changed.

Resolve playback authority independently of the account picker and fence repository, controller, and inline settings operations to their original authenticated account.
2026-09-06 20:21:11 +02:00

53 lines
2.1 KiB
Dart

import '../connection/connection_registry.dart';
import 'active_plex_identity.dart';
import 'active_profile_provider.dart';
import 'profile.dart';
import 'profile_connection_registry.dart';
/// The active profile's Plex identity together with the effective token
/// resolved by [resolveActivePlexToken].
class ActivePlexToken {
const ActivePlexToken({required this.identity, required this.token});
final ActivePlexIdentity identity;
/// Effective Plex token for [identity] under the policy documented on
/// [resolveActivePlexToken].
final String token;
}
/// Resolve the active profile's effective Plex credential for cloud callers.
///
/// One policy: the profile's per-user token (the `ProfileConnection` row
/// bound to the identity's account, minted via `/home/users/{uuid}/switch`)
/// wins when present; otherwise the account-owner token.
///
/// [allowAccountTokenForHomeUser] controls the owner-token fallback for Plex
/// Home profiles. Cloud Discover and Seerr pass `true` — any credential on
/// the account is acceptable there. Account preferences resolve separately:
/// their Home principal requires its exact parent's switched token, without
/// owner or other-account fallback. Local profiles retain owner-token fallback.
Future<ActivePlexToken?> resolveActivePlexToken({
required ActiveProfileProvider activeProfile,
required ConnectionRegistry connections,
required ProfileConnectionRegistry profileConnections,
required bool allowAccountTokenForHomeUser,
}) async {
final identity = await resolveActivePlexIdentity(
activeProfile: activeProfile,
connections: connections,
profileConnections: profileConnections,
);
if (identity == null) return null;
final profile = activeProfile.active;
if (profile != null) {
final profileConnection = await profileConnections.get(profile.id, identity.account.id);
if (profileConnection?.hasToken ?? false) {
return ActivePlexToken(identity: identity, token: profileConnection!.userToken!);
}
}
if (!allowAccountTokenForHomeUser && profile?.kind == ProfileKind.plexHome) return null;
return ActivePlexToken(identity: identity, token: identity.account.accountToken);
}