fix(plex): use promoted home hubs

close #1004
This commit is contained in:
edde746
2026-05-11 13:18:43 +02:00
parent fd1eaf9d96
commit fcd8198fcb
5 changed files with 165 additions and 103 deletions
@@ -6,9 +6,12 @@ import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:plezy/connection/connection.dart';
import 'package:plezy/database/app_database.dart';
import 'package:plezy/models/plex/plex_config.dart';
import 'package:plezy/services/data_aggregation_service.dart';
import 'package:plezy/services/jellyfin_client.dart';
import 'package:plezy/services/multi_server_manager.dart';
import 'package:plezy/services/plex_api_cache.dart';
import 'package:plezy/services/plex_client.dart';
JellyfinConnection _conn() => JellyfinConnection(
id: 'srv-1/user-1',
@@ -36,6 +39,7 @@ void main() {
setUp(() {
db = AppDatabase.forTesting(NativeDatabase.memory());
PlexApiCache.initialize(db);
manager = MultiServerManager();
service = DataAggregationService(manager);
});
@@ -159,5 +163,63 @@ void main() {
['movies', 'shows'],
);
});
test('Plex home layout keeps promoted hubs instead of splitting by preview libraries', () async {
final captured = <Uri>[];
final client = PlexClient.forTesting(
config: PlexConfig(
baseUrl: 'https://plex.example.com',
token: 'token',
clientIdentifier: 'client-id',
product: 'Plezy',
version: 'test',
),
serverId: 'plex-1',
serverName: 'Plex',
promotedHubKey: '/hubs/promoted',
httpClient: MockClient((req) async {
captured.add(req.url);
if (req.url.path == '/hubs/promoted') {
return _json({
'MediaContainer': {
'Hub': [
{
'key': '/hubs/home/recentlyAdded?type=2',
'title': 'Recently Added TV',
'type': 'mixed',
'hubIdentifier': 'home.television.recent',
'size': 7,
'more': true,
'Metadata': [
for (var i = 1; i <= 7; i++)
{
'ratingKey': 'show-$i',
'type': 'show',
'title': 'Show $i',
'librarySectionID': i,
'librarySectionTitle': 'Library $i',
},
],
},
],
},
});
}
return http.Response('unexpected request', 500);
}),
);
addTearDown(client.close);
manager.debugRegisterClientForTesting(client);
final hubs = await service.getHubsFromAllServers(useGlobalHubs: true, includePlaybackHubs: false);
expect(hubs, hasLength(1));
expect(hubs.single.title, 'Recently Added TV');
expect(hubs.single.identifier, 'home.television.recent');
expect(hubs.single.libraryId, isNull);
expect(hubs.single.items, hasLength(7));
expect(captured.map((uri) => uri.path), ['/hubs/promoted']);
});
});
}
+58
View File
@@ -95,6 +95,38 @@ void main() {
expect(httpClient.requests.map((r) => r.url.origin), everyElement(primary));
});
test('fetchGlobalHubs uses promoted hub endpoint advertised by media providers', () async {
final db = AppDatabase.forTesting(NativeDatabase.memory());
PlexApiCache.initialize(db);
addTearDown(db.close);
final httpClient = _SequenceClient([
(_) async => _jsonResponse(_mediaProvidersPayload()),
(_) async => _jsonResponse(_globalHubsPayload()),
]);
final client = await PlexClient.create(
PlexConfig(
baseUrl: 'http://server:32400',
token: 'token',
clientIdentifier: 'client-id',
product: 'Plezy',
version: 'test',
),
serverId: 'server-id',
serverName: 'Server',
httpClient: httpClient,
seedTranscoderVideoSupport: true,
);
addTearDown(client.close);
final hubs = await client.fetchGlobalHubs(limit: 12);
expect(hubs, hasLength(1));
expect(hubs.single.title, 'Recently Added Movies');
expect(httpClient.requests.map((r) => r.url.path), ['/media/providers', '/hubs/promoted']);
expect(httpClient.requests.last.url.queryParameters['count'], '12');
});
test('fetchLibraryHubs retries transient failures without switching Plex endpoints', () async {
final db = AppDatabase.forTesting(NativeDatabase.memory());
PlexApiCache.initialize(db);
@@ -157,3 +189,29 @@ Map<String, dynamic> _globalHubsPayload() => {
],
},
};
Map<String, dynamic> _mediaProvidersPayload() => {
'MediaContainer': {
'MediaProvider': [
{
'identifier': 'com.plexapp.plugins.library',
'Feature': [
{
'type': 'content',
'Directory': [
{'title': 'Home', 'hubKey': '/hubs'},
{
'id': '1',
'key': '/library/sections/1',
'hubKey': '/hubs/sections/1',
'type': 'movie',
'title': 'Movies',
},
],
},
{'type': 'promoted', 'key': '/hubs/promoted'},
],
},
],
},
};