refactor(profiles): centralize Plex Home cache codec

This commit is contained in:
edde746
2026-07-12 08:42:20 +02:00
parent ac9c236b6b
commit 877e8880cf
4 changed files with 61 additions and 13 deletions
+3 -4
View File
@@ -3,6 +3,7 @@ import 'dart:convert';
import '../models/plex/plex_home.dart';
import '../models/plex/plex_home_user.dart';
import '../profiles/profile.dart';
import '../profiles/plex_home_cache_codec.dart';
import '../profiles/profile_registry.dart';
import '../services/plex_auth_service.dart';
import '../services/server_registry.dart';
@@ -225,9 +226,7 @@ class ConnectionBootstrap {
final raw = storage.getPlexHomeUsersCacheJson(connectionId);
if (raw == null || raw.isEmpty) return null;
try {
final decoded = jsonDecode(raw);
if (decoded is! List) return null;
return decoded.whereType<Map<String, dynamic>>().map(PlexHomeUser.fromJson).toList();
return decodePlexHomeUsersCache(raw);
} catch (e, st) {
appLogger.w('Migration: failed to read Plex Home cache for $connectionId', error: e, stackTrace: st);
return null;
@@ -238,7 +237,7 @@ class ConnectionBootstrap {
try {
final users = await _plexHomeUserFetcher(account.accountToken);
if (users.isNotEmpty) {
await storage.savePlexHomeUsersCache(account.id, users.map((u) => u.toJson()).toList());
await storage.savePlexHomeUsersCache(account.id, encodePlexHomeUsersCache(users));
appLogger.i('Migration: fetched ${users.length} Plex Home users for ${account.id}');
}
return users;
+19
View File
@@ -0,0 +1,19 @@
import 'dart:convert';
import '../models/plex/plex_home_user.dart';
List<Map<String, dynamic>> encodePlexHomeUsersCache(List<PlexHomeUser> users) {
return users.map((user) => user.toJson()).toList(growable: false);
}
String encodePlexHomeUsersCacheJson(List<PlexHomeUser> users) {
return jsonEncode(encodePlexHomeUsersCache(users));
}
List<PlexHomeUser> decodePlexHomeUsersCache(String raw) {
final decoded = jsonDecode(raw);
if (decoded is! List) {
throw const FormatException('Plex Home users cache is not a list');
}
return decoded.whereType<Map<String, dynamic>>().map(PlexHomeUser.fromJson).toList(growable: false);
}
+5 -9
View File
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';
import '../connection/connection.dart';
import '../connection/connection_registry.dart';
@@ -9,6 +8,7 @@ import '../services/plex_auth_service.dart';
import '../services/storage_service.dart';
import '../utils/app_logger.dart';
import 'profile_connection_registry.dart';
import 'plex_home_cache_codec.dart';
/// Live source of truth for Plex Home users — Plex owns these, so we never
/// persist them as `Profile` rows. The service fetches `/home/users` per
@@ -188,11 +188,12 @@ class PlexHomeService {
appLogger.d('PlexHomeService: dropping fetch result for removed account ${conn.accountLabel}');
return false;
}
final encoded = users.map((u) => u.toJson()).toList();
final encoded = encodePlexHomeUsersCache(users);
// Unchanged fetches (the hourly ticker, mostly) must not emit: every
// emission fans out through ActiveProfileProvider into a full
// recompute/notify cascade across the app.
if (_byConnection.containsKey(conn.id) && storage.getPlexHomeUsersCacheJson(conn.id) == jsonEncode(encoded)) {
if (_byConnection.containsKey(conn.id) &&
storage.getPlexHomeUsersCacheJson(conn.id) == encodePlexHomeUsersCacheJson(users)) {
appLogger.d('PlexHomeService: home users unchanged for ${conn.accountLabel}');
return true;
}
@@ -213,12 +214,7 @@ class PlexHomeService {
final raw = storage.getPlexHomeUsersCacheJson(connectionId);
if (raw == null) return null;
try {
final decoded = jsonDecode(raw);
if (decoded is! List) {
appLogger.w('PlexHomeService: cache for $connectionId is not a list — ignoring');
return null;
}
return decoded.whereType<Map<String, dynamic>>().map(PlexHomeUser.fromJson).toList();
return decodePlexHomeUsersCache(raw);
} catch (e, st) {
appLogger.w('PlexHomeService: failed to read cache for $connectionId', error: e, stackTrace: st);
return null;