Files
plezy/lib/models/plex/plex_user_profile.dart
T

92 lines
3.2 KiB
Dart
Raw Normal View History

import 'package:json_annotation/json_annotation.dart';
2026-04-26 02:34:09 +02:00
import '../../media/media_server_user_profile.dart';
import '../../utils/json_utils.dart';
2026-04-26 02:34:09 +02:00
part 'plex_user_profile.g.dart';
2025-10-18 00:05:20 +02:00
/// Represents a Plex user's profile preferences
/// Fetched from https://clients.plex.tv/api/v2/user
///
/// Every field parses tolerantly: the account API drifts (~July 2026 the
/// language-list fields switched from arrays to CSV strings, #1488), and a
/// profile blob must never fail to parse — token minting embeds it (see
/// UserSwitchResponse.fromJson).
@JsonSerializable()
2026-04-26 02:34:09 +02:00
class PlexUserProfile implements MediaServerUserProfile {
@JsonKey(fromJson: _boolOrTrue)
2026-04-26 02:34:09 +02:00
@override
2025-10-18 00:05:20 +02:00
final bool autoSelectAudio;
2026-07-12 03:22:18 +02:00
@JsonKey(fromJson: flexibleIntOrZero)
2025-10-18 00:05:20 +02:00
final int defaultAudioAccessibility;
@JsonKey(fromJson: _flexibleLanguage)
2026-04-26 02:34:09 +02:00
@override
2025-10-18 00:05:20 +02:00
final String? defaultAudioLanguage;
@JsonKey(fromJson: flexibleCsvStringList)
2026-04-26 02:34:09 +02:00
@override
2025-10-18 00:05:20 +02:00
final List<String>? defaultAudioLanguages;
@JsonKey(fromJson: _flexibleLanguage)
2026-04-26 02:34:09 +02:00
@override
2025-10-18 00:05:20 +02:00
final String? defaultSubtitleLanguage;
@JsonKey(fromJson: flexibleCsvStringList)
2026-04-26 02:34:09 +02:00
@override
2025-10-18 00:05:20 +02:00
final List<String>? defaultSubtitleLanguages;
2026-07-12 03:22:18 +02:00
@JsonKey(fromJson: flexibleIntOrZero)
2025-10-18 00:05:20 +02:00
final int autoSelectSubtitle;
2026-07-12 03:22:18 +02:00
@JsonKey(fromJson: flexibleIntOrZero)
2025-10-18 00:05:20 +02:00
final int defaultSubtitleAccessibility;
@JsonKey(fromJson: _intOr1)
2025-10-18 00:05:20 +02:00
final int defaultSubtitleForced;
@JsonKey(fromJson: _intOr1)
2025-10-18 00:05:20 +02:00
final int watchedIndicator;
2026-07-12 03:22:18 +02:00
@JsonKey(fromJson: flexibleIntOrZero)
2025-10-18 00:05:20 +02:00
final int mediaReviewsVisibility;
@JsonKey(fromJson: flexibleCsvStringList)
2025-10-18 00:05:20 +02:00
final List<String>? mediaReviewsLanguages;
2026-04-26 02:34:09 +02:00
@override
SubtitlePlaybackMode? get subtitleMode => null;
2025-10-18 00:05:20 +02:00
PlexUserProfile({
required this.autoSelectAudio,
required this.defaultAudioAccessibility,
this.defaultAudioLanguage,
this.defaultAudioLanguages,
this.defaultSubtitleLanguage,
this.defaultSubtitleLanguages,
required this.autoSelectSubtitle,
required this.defaultSubtitleAccessibility,
required this.defaultSubtitleForced,
required this.watchedIndicator,
required this.mediaReviewsVisibility,
this.mediaReviewsLanguages,
});
/// Neutral fallback matching the generated defaults — used when the account
/// API returns a profile blob that cannot be parsed at all (schema drift
/// must never break token minting, see UserSwitchResponse.fromJson).
factory PlexUserProfile.defaults() => PlexUserProfile(
autoSelectAudio: true,
defaultAudioAccessibility: 0,
autoSelectSubtitle: 0,
defaultSubtitleAccessibility: 0,
defaultSubtitleForced: 1,
watchedIndicator: 1,
mediaReviewsVisibility: 0,
);
2025-10-18 00:05:20 +02:00
factory PlexUserProfile.fromJson(Map<String, dynamic> json) {
final envelope = json['profile'];
final profile = envelope is Map<String, dynamic> ? envelope : json;
return _$PlexUserProfileFromJson(profile);
2025-10-18 00:05:20 +02:00
}
Map<String, dynamic> toJson() => {'profile': _$PlexUserProfileToJson(this)};
2025-10-18 00:05:20 +02:00
}
/// Singular language fields tolerate the inverse drift (array/CSV → first entry).
String? _flexibleLanguage(Object? v) => flexibleCsvStringList(v)?.first;
bool _boolOrTrue(Object? v) => flexibleBoolNullable(v) ?? true;
int _intOr1(Object? v) => flexibleInt(v) ?? 1;