@@ -238,12 +238,12 @@ abstract class MediaServerClient {
|
||||
/// series" via the null vs `[]` distinction.
|
||||
Future<List<MediaItem>?> fetchClientSideEpisodeQueue(String seriesId);
|
||||
|
||||
/// Albums credited to the artist [artistId], newest first. Not the same as
|
||||
/// [fetchChildren]: Plex artists *are* folder-parents of their albums
|
||||
/// (`/library/metadata/{id}/children`), but Jellyfin albums link to artists
|
||||
/// only via tags, so it queries
|
||||
/// Albums credited to [artist], newest first. Plex filters album rows in
|
||||
/// the artist's music section so release formats omitted from
|
||||
/// `/library/metadata/{id}/children` remain visible. Jellyfin links albums
|
||||
/// to artists via tags and queries
|
||||
/// `/Items?AlbumArtistIds={id}&IncludeItemTypes=MusicAlbum`.
|
||||
Future<List<MediaItem>> fetchArtistAlbums(String artistId);
|
||||
Future<List<MediaItem>> fetchArtistAlbums(MediaItem artist);
|
||||
|
||||
/// Tracks of album [albumId] in disc/track order. Plex:
|
||||
/// `/library/metadata/{id}/children`; Jellyfin:
|
||||
|
||||
@@ -62,7 +62,7 @@ class _ArtistDetailScreenState extends BaseMediaListDetailScreen<ArtistDetailScr
|
||||
bool get hasItems => items.isNotEmpty;
|
||||
|
||||
@override
|
||||
Future<List<MediaItem>> fetchItems() => mediaClient.fetchArtistAlbums(widget.artist.id);
|
||||
Future<List<MediaItem>> fetchItems() => mediaClient.fetchArtistAlbums(widget.artist);
|
||||
|
||||
@override
|
||||
Future<void> loadItems() async {
|
||||
|
||||
@@ -9,16 +9,16 @@ mixin _JellyfinMusicMethods on MediaServerCacheMixin {
|
||||
FailoverHttpClient get _http;
|
||||
List<MediaItem> _mapItems(Iterable<Map<String, dynamic>> items);
|
||||
|
||||
/// Albums credited to [artistId], newest first. Queries `AlbumArtistIds`
|
||||
/// Albums credited to [artist], newest first. Queries `AlbumArtistIds`
|
||||
/// rather than `ParentId` because Jellyfin links albums to artists via
|
||||
/// tags — an artist's albums are usually not its folder children.
|
||||
@override
|
||||
Future<List<MediaItem>> fetchArtistAlbums(String artistId) async {
|
||||
Future<List<MediaItem>> fetchArtistAlbums(MediaItem artist) async {
|
||||
final response = await _http.get(
|
||||
'/Items',
|
||||
queryParameters: {
|
||||
'userId': connection.userId,
|
||||
'AlbumArtistIds': artistId,
|
||||
'AlbumArtistIds': artist.id,
|
||||
'IncludeItemTypes': 'MusicAlbum',
|
||||
'Recursive': 'true',
|
||||
'SortBy': 'PremiereDate,ProductionYear,SortName',
|
||||
|
||||
@@ -2856,10 +2856,33 @@ class PlexClient
|
||||
@override
|
||||
Future<List<MediaItem>?> fetchClientSideEpisodeQueue(String seriesId) async => null;
|
||||
|
||||
/// Plex artists are folder-parents of their albums, so both music child
|
||||
/// listings are plain `/library/metadata/{id}/children` fetches.
|
||||
/// Plex's artist `/children` response only contains the primary album
|
||||
/// bucket. Filter album rows in the artist's music section to include every
|
||||
/// release format Plex associates with the artist.
|
||||
@override
|
||||
Future<List<MediaItem>> fetchArtistAlbums(String artistId) => fetchChildren(artistId);
|
||||
Future<List<MediaItem>> fetchArtistAlbums(MediaItem artist) async {
|
||||
final embeddedSectionId = artist.libraryId;
|
||||
final sectionId = embeddedSectionId != null && embeddedSectionId.isNotEmpty
|
||||
? embeddedSectionId
|
||||
: (await _getMetadataWithImages(artist.id))?.librarySectionID?.toString();
|
||||
if (sectionId == null || sectionId.isEmpty) {
|
||||
throw StateError('Plex artist ${artist.id} is missing a library section ID');
|
||||
}
|
||||
|
||||
// Preserve the existing artist-list cache identity so offline fallback
|
||||
// and item invalidation continue to cover the complete discography.
|
||||
final cacheKey = '/library/metadata/${artist.id}/children';
|
||||
final metadata = await fetchWithCacheFallback<List<PlexMetadataDto>>(
|
||||
cacheKey: cacheKey,
|
||||
networkCall: () => _getAllPagesResponse(
|
||||
'/library/sections/$sectionId/all',
|
||||
queryParameters: {'type': PlexMetadataType.album, 'artist.id': artist.id, 'sort': 'album.year:desc'},
|
||||
),
|
||||
parseCache: (cachedData) => _parseMetadataListFromCachedResponse(cachedData),
|
||||
parseResponse: (response) => _extractMetadataList(response),
|
||||
);
|
||||
return (metadata ?? const <PlexMetadataDto>[]).map((item) => PlexMappers.mediaItem(item)).toList();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<MediaItem>> fetchAlbumTracks(String albumId) => fetchChildren(albumId);
|
||||
|
||||
@@ -1781,7 +1781,9 @@ void main() {
|
||||
|
||||
await scoped.fetchLibraryContent('lib-1', const LibraryQuery(kind: MediaKind.album, offset: 0, limit: 20));
|
||||
await scoped.fetchLibraryContent('lib-1', const LibraryQuery(kind: MediaKind.track, offset: 0, limit: 20));
|
||||
await scoped.fetchArtistAlbums('artist-1');
|
||||
await scoped.fetchArtistAlbums(
|
||||
testMediaItem(id: 'artist-1', backend: MediaBackend.jellyfin, kind: MediaKind.artist),
|
||||
);
|
||||
await scoped.fetchAlbumTracks('album-1');
|
||||
|
||||
final albumBrowse = captured[0].queryParameters;
|
||||
|
||||
@@ -6,10 +6,12 @@ import 'package:http/http.dart' as http;
|
||||
import 'package:plezy/database/app_database.dart';
|
||||
import 'package:plezy/exceptions/media_server_exceptions.dart';
|
||||
import 'package:plezy/media/ids.dart';
|
||||
import 'package:plezy/media/media_kind.dart';
|
||||
import 'package:plezy/services/plex_api_cache.dart';
|
||||
import 'package:plezy/services/plex_client.dart';
|
||||
|
||||
import '../test_helpers/backend_client_fixtures.dart';
|
||||
import '../test_helpers/media_items.dart';
|
||||
|
||||
void main() {
|
||||
late AppDatabase db;
|
||||
@@ -289,4 +291,94 @@ void main() {
|
||||
'season-3',
|
||||
]);
|
||||
});
|
||||
|
||||
test('artist albums include every Plex release bucket and cache all pages', () async {
|
||||
const cacheKey = '/library/metadata/artist-1/children';
|
||||
final requests = <Uri>[];
|
||||
final client = makeClient((request) async {
|
||||
requests.add(request.url);
|
||||
final start = int.parse(request.url.queryParameters['X-Plex-Container-Start']!);
|
||||
final metadata = start == 0
|
||||
? [
|
||||
{'ratingKey': 'album-lp', 'type': 'album', 'title': 'LP'},
|
||||
{'ratingKey': 'album-ep', 'type': 'album', 'title': 'EP'},
|
||||
]
|
||||
: [
|
||||
{'ratingKey': 'album-single', 'type': 'album', 'title': 'Single'},
|
||||
{'ratingKey': 'album-compilation', 'type': 'album', 'title': 'Compilation'},
|
||||
];
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'MediaContainer': {'librarySectionID': 7, 'size': metadata.length, 'totalSize': 4, 'Metadata': metadata},
|
||||
}),
|
||||
200,
|
||||
headers: const {'content-type': 'application/json'},
|
||||
);
|
||||
});
|
||||
addTearDown(client.close);
|
||||
|
||||
final albums = await client.fetchArtistAlbums(
|
||||
testMediaItem(id: 'artist-1', kind: MediaKind.artist, libraryId: '7'),
|
||||
);
|
||||
final cached = await PlexApiCache.instance.get(ServerId('server-id'), cacheKey);
|
||||
final cachedContainer = cached!['MediaContainer'] as Map<String, dynamic>;
|
||||
final cachedMetadata = cachedContainer['Metadata'] as List<dynamic>;
|
||||
|
||||
expect(albums.map((album) => album.id), ['album-lp', 'album-ep', 'album-single', 'album-compilation']);
|
||||
expect(requests, hasLength(2));
|
||||
expect(requests.every((uri) => uri.path == '/library/sections/7/all'), isTrue);
|
||||
expect(requests.every((uri) => uri.queryParameters['type'] == '9'), isTrue);
|
||||
expect(requests.every((uri) => uri.queryParameters['artist.id'] == 'artist-1'), isTrue);
|
||||
expect(requests.every((uri) => uri.queryParameters['sort'] == 'album.year:desc'), isTrue);
|
||||
expect(requests.map((uri) => uri.queryParameters['X-Plex-Container-Start']), ['0', '2']);
|
||||
expect(cachedMetadata.map((item) => (item as Map<String, dynamic>)['ratingKey']), [
|
||||
'album-lp',
|
||||
'album-ep',
|
||||
'album-single',
|
||||
'album-compilation',
|
||||
]);
|
||||
});
|
||||
|
||||
test('artist albums resolve a missing music section from artist metadata', () async {
|
||||
final requestedPaths = <String>[];
|
||||
final client = makeClient((request) async {
|
||||
requestedPaths.add(request.url.path);
|
||||
if (request.url.path == '/library/metadata/artist-1') {
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'MediaContainer': {
|
||||
'librarySectionID': 7,
|
||||
'Metadata': [
|
||||
{'ratingKey': 'artist-1', 'type': 'artist', 'title': 'Artist'},
|
||||
],
|
||||
},
|
||||
}),
|
||||
200,
|
||||
headers: const {'content-type': 'application/json'},
|
||||
);
|
||||
}
|
||||
if (request.url.path == '/library/sections/7/all') {
|
||||
return http.Response(
|
||||
jsonEncode({
|
||||
'MediaContainer': {
|
||||
'librarySectionID': 7,
|
||||
'size': 1,
|
||||
'Metadata': [
|
||||
{'ratingKey': 'album-1', 'type': 'album', 'title': 'Album'},
|
||||
],
|
||||
},
|
||||
}),
|
||||
200,
|
||||
headers: const {'content-type': 'application/json'},
|
||||
);
|
||||
}
|
||||
return http.Response('not found', 404);
|
||||
});
|
||||
addTearDown(client.close);
|
||||
|
||||
final albums = await client.fetchArtistAlbums(testMediaItem(id: 'artist-1', kind: MediaKind.artist));
|
||||
|
||||
expect(requestedPaths, ['/library/metadata/artist-1', '/library/sections/7/all']);
|
||||
expect(albums.map((album) => album.id), ['album-1']);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -596,7 +596,7 @@ class _RelatedMusicClient implements MediaServerClient {
|
||||
Future<List<MediaItem>> fetchAlbumTracks(String albumId) async => const [];
|
||||
|
||||
@override
|
||||
Future<List<MediaItem>> fetchArtistAlbums(String artistId) async => const [];
|
||||
Future<List<MediaItem>> fetchArtistAlbums(MediaItem artist) async => const [];
|
||||
|
||||
@override
|
||||
void close() {}
|
||||
|
||||
Reference in New Issue
Block a user