From e70782a4780305f1a36639d31b947a71f44548e9 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 17 Nov 2015 12:38:31 -0500 Subject: [PATCH] update user view builder --- .../Entities/UserViewBuilder.cs | 206 +++++++++++++++++- 1 file changed, 205 insertions(+), 1 deletion(-) diff --git a/MediaBrowser.Controller/Entities/UserViewBuilder.cs b/MediaBrowser.Controller/Entities/UserViewBuilder.cs index 7c588c910..edeb13398 100644 --- a/MediaBrowser.Controller/Entities/UserViewBuilder.cs +++ b/MediaBrowser.Controller/Entities/UserViewBuilder.cs @@ -125,7 +125,6 @@ namespace MediaBrowser.Controller.Entities case CollectionType.HomeVideos: case CollectionType.Games: case CollectionType.MusicVideos: - case CollectionType.Music: { if (query.Recursive) { @@ -194,6 +193,45 @@ namespace MediaBrowser.Controller.Entities case SpecialFolder.TvFavoriteSeries: return GetFavoriteSeries(queryParent, user, query); + case CollectionType.Music: + return await GetMusicFolders(queryParent, user, query).ConfigureAwait(false); + + case SpecialFolder.MusicGenres: + return await GetMusicGenres(queryParent, user, query).ConfigureAwait(false); + + case SpecialFolder.MusicGenre: + return await GetMusicGenreItems(queryParent, displayParent, user, query).ConfigureAwait(false); + + case SpecialFolder.MusicLatest: + return GetMusicLatest(queryParent, user, query); + + case SpecialFolder.MusicPlaylists: + return await GetMusicPlaylists(queryParent, user, query).ConfigureAwait(false); + + case SpecialFolder.MusicAlbums: + return GetMusicAlbums(queryParent, user, query); + + case SpecialFolder.MusicAlbumArtists: + return GetMusicAlbumArtists(queryParent, user, query); + + case SpecialFolder.MusicArtists: + return GetMusicArtists(queryParent, user, query); + + case SpecialFolder.MusicSongs: + return GetMusicSongs(queryParent, user, query); + + case SpecialFolder.MusicFavorites: + return await GetMusicFavorites(queryParent, user, query).ConfigureAwait(false); + + case SpecialFolder.MusicFavoriteAlbums: + return GetFavoriteAlbums(queryParent, user, query); + + case SpecialFolder.MusicFavoriteArtists: + return GetFavoriteArtists(queryParent, user, query); + + case SpecialFolder.MusicFavoriteSongs: + return GetFavoriteSongs(queryParent, user, query); + default: { if (queryParent is UserView) @@ -208,6 +246,172 @@ namespace MediaBrowser.Controller.Entities } } + private async Task> GetMusicFolders(Folder parent, User user, InternalItemsQuery query) + { + if (query.Recursive) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }, i => FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + + var list = new List(); + + list.Add(await GetUserView(SpecialFolder.MusicLatest, "0", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicPlaylists, "1", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicAlbums, "2", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicAlbumArtists, "3", parent).ConfigureAwait(false)); + //list.Add(await GetUserView(SpecialFolder.MusicArtists, user, "4", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicSongs, "5", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicGenres, "6", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicFavorites, "7", parent).ConfigureAwait(false)); + + return GetResult(list, parent, query); + } + + private async Task> GetMusicFavorites(Folder parent, User user, InternalItemsQuery query) + { + var list = new List(); + + list.Add(await GetUserView(SpecialFolder.MusicFavoriteAlbums, "0", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicFavoriteArtists, "1", parent).ConfigureAwait(false)); + list.Add(await GetUserView(SpecialFolder.MusicFavoriteSongs, "2", parent).ConfigureAwait(false)); + + return GetResult(list, parent, query); + } + + private async Task> GetMusicGenres(Folder parent, User user, InternalItemsQuery query) + { + var tasks = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }) + .Where(i => !i.IsFolder) + .SelectMany(i => i.Genres) + .DistinctNames() + .Select(i => + { + try + { + return _libraryManager.GetMusicGenre(i); + } + catch + { + // Full exception logged at lower levels + _logger.Error("Error getting genre"); + return null; + } + + }) + .Where(i => i != null) + .Select(i => GetUserView(i.Name, SpecialFolder.MusicGenre, i.SortName, parent)); + + var genres = await Task.WhenAll(tasks).ConfigureAwait(false); + + return GetResult(genres, parent, query); + } + + private async Task> GetMusicGenreItems(Folder queryParent, Folder displayParent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(queryParent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }) + .Where(i => !i.IsFolder) + .Where(i => i.Genres.Contains(displayParent.Name, StringComparer.OrdinalIgnoreCase)) + .OfType(); + + var artists = _libraryManager.GetAlbumArtists(items); + + return GetResult(artists, queryParent, query); + } + + private QueryResult GetMusicAlbumArtists(Folder parent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }) + .Where(i => !i.IsFolder) + .OfType(); + + var artists = _libraryManager.GetAlbumArtists(items); + + return GetResult(artists, parent, query); + } + + private QueryResult GetMusicArtists(Folder parent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }) + .Where(i => !i.IsFolder) + .OfType(); + + var artists = _libraryManager.GetArtists(items); + + return GetResult(artists, parent, query); + } + + private QueryResult GetFavoriteArtists(Folder parent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }) + .Where(i => !i.IsFolder) + .OfType(); + + var artists = _libraryManager.GetAlbumArtists(items).Where(i => _userDataManager.GetUserData(user.Id, i.GetUserDataKey()).IsFavorite); + + return GetResult(artists, parent, query); + } + + private Task> GetMusicPlaylists(Folder parent, User user, InternalItemsQuery query) + { + query.IncludeItemTypes = new[] { "Playlist" }; + query.Recursive = true; + + return parent.GetItems(query); + } + + private QueryResult GetMusicAlbums(Folder parent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }, i => (i is MusicAlbum) && FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + + private QueryResult GetMusicSongs(Folder parent, User user, InternalItemsQuery query) + { + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }, i => (i is Audio.Audio) && FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + + private QueryResult GetMusicLatest(Folder parent, User user, InternalItemsQuery query) + { + var items = _userViewManager.GetLatestItems(new LatestItemsQuery + { + UserId = user.Id.ToString("N"), + Limit = GetSpecialItemsLimit(), + IncludeItemTypes = new[] { typeof(Audio.Audio).Name }, + ParentId = (parent == null ? null : parent.Id.ToString("N")), + GroupItems = true + + }).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null); + + query.SortBy = new string[] { }; + + //var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music, CollectionType.MusicVideos }, i => i is MusicVideo || i is Audio.Audio && FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + + private QueryResult GetFavoriteSongs(Folder parent, User user, InternalItemsQuery query) + { + query.IsFavorite = true; + + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music }, i => (i is Audio.Audio) && FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + + private QueryResult GetFavoriteAlbums(Folder parent, User user, InternalItemsQuery query) + { + query.IsFavorite = true; + + var items = GetRecursiveChildren(parent, user, new[] { CollectionType.Music }, i => (i is MusicAlbum) && FilterItem(i, query)); + + return PostFilterAndSort(items, parent, null, query); + } + private async Task> FindPlaylists(Folder parent, User user, InternalItemsQuery query) { var list = _playlistManager.GetPlaylists(user.Id.ToString("N"));