Playlist.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Querying;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using MediaBrowser.Model.Serialization;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Controller.Providers;
  11. namespace MediaBrowser.Controller.Playlists
  12. {
  13. public class Playlist : Folder, IHasShares
  14. {
  15. public string OwnerUserId { get; set; }
  16. public List<Share> Shares { get; set; }
  17. public Playlist()
  18. {
  19. Shares = new List<Share>();
  20. }
  21. [IgnoreDataMember]
  22. protected override bool FilterLinkedChildrenPerUser
  23. {
  24. get
  25. {
  26. return true;
  27. }
  28. }
  29. [IgnoreDataMember]
  30. public override bool SupportsPlayedStatus
  31. {
  32. get
  33. {
  34. return string.Equals(MediaType, "Video", StringComparison.OrdinalIgnoreCase);
  35. }
  36. }
  37. [IgnoreDataMember]
  38. public override bool AlwaysScanInternalMetadataPath
  39. {
  40. get
  41. {
  42. return true;
  43. }
  44. }
  45. [IgnoreDataMember]
  46. public override bool SupportsCumulativeRunTimeTicks
  47. {
  48. get
  49. {
  50. return true;
  51. }
  52. }
  53. public override double? GetDefaultPrimaryImageAspectRatio()
  54. {
  55. return 1;
  56. }
  57. public override bool IsAuthorizedToDelete(User user)
  58. {
  59. return true;
  60. }
  61. public override bool IsSaveLocalMetadataEnabled()
  62. {
  63. return true;
  64. }
  65. protected override IEnumerable<BaseItem> LoadChildren()
  66. {
  67. // Save a trip to the database
  68. return new List<BaseItem>();
  69. }
  70. public override IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  71. {
  72. return GetPlayableItems(user).Result;
  73. }
  74. protected override IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  75. {
  76. return new List<BaseItem>();
  77. }
  78. public override IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  79. {
  80. var items = GetPlayableItems(user).Result;
  81. if (query != null)
  82. {
  83. items = items.Where(i => UserViewBuilder.FilterItem(i, query));
  84. }
  85. return items;
  86. }
  87. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetManageableItems()
  88. {
  89. return GetLinkedChildrenInfos();
  90. }
  91. private Task<IEnumerable<BaseItem>> GetPlayableItems(User user)
  92. {
  93. return GetPlaylistItems(MediaType, base.GetChildren(user, true), user);
  94. }
  95. public static async Task<IEnumerable<BaseItem>> GetPlaylistItems(string playlistMediaType, IEnumerable<BaseItem> inputItems, User user)
  96. {
  97. if (user != null)
  98. {
  99. inputItems = inputItems.Where(i => i.IsVisible(user));
  100. }
  101. var list = new List<BaseItem>();
  102. foreach (var item in inputItems)
  103. {
  104. var playlistItems = await GetPlaylistItems(item, user, playlistMediaType).ConfigureAwait(false);
  105. list.AddRange(playlistItems);
  106. }
  107. return list;
  108. }
  109. private static async Task<IEnumerable<BaseItem>> GetPlaylistItems(BaseItem item, User user, string mediaType)
  110. {
  111. var musicGenre = item as MusicGenre;
  112. if (musicGenre != null)
  113. {
  114. var items = LibraryManager.GetItemList(new InternalItemsQuery(user)
  115. {
  116. Recursive = true,
  117. IncludeItemTypes = new[] { typeof(Audio).Name },
  118. Genres = new[] { musicGenre.Name }
  119. });
  120. return LibraryManager.Sort(items, user, new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }, SortOrder.Ascending);
  121. }
  122. var musicArtist = item as MusicArtist;
  123. if (musicArtist != null)
  124. {
  125. Func<BaseItem, bool> filter = i =>
  126. {
  127. var audio = i as Audio;
  128. return audio != null && audio.HasAnyArtist(musicArtist.Name);
  129. };
  130. var items = user == null
  131. ? LibraryManager.RootFolder.GetRecursiveChildren(filter)
  132. : user.RootFolder.GetRecursiveChildren(user, new InternalItemsQuery(user)
  133. {
  134. IncludeItemTypes = new[] { typeof(Audio).Name },
  135. ArtistIds = new[] { musicArtist.Id.ToString("N") }
  136. });
  137. return LibraryManager.Sort(items, user, new[] { ItemSortBy.AlbumArtist, ItemSortBy.Album, ItemSortBy.SortName }, SortOrder.Ascending);
  138. }
  139. var folder = item as Folder;
  140. if (folder != null)
  141. {
  142. var query = new InternalItemsQuery(user)
  143. {
  144. Recursive = true,
  145. IsFolder = false,
  146. SortBy = new[] { ItemSortBy.SortName },
  147. MediaTypes = new[] { mediaType },
  148. EnableTotalRecordCount = false
  149. };
  150. var itemsResult = await folder.GetItems(query).ConfigureAwait(false);
  151. var items = itemsResult.Items;
  152. return items;
  153. }
  154. return new[] { item };
  155. }
  156. [IgnoreDataMember]
  157. public override bool IsPreSorted
  158. {
  159. get
  160. {
  161. return true;
  162. }
  163. }
  164. public string PlaylistMediaType { get; set; }
  165. [IgnoreDataMember]
  166. public override string MediaType
  167. {
  168. get
  169. {
  170. return PlaylistMediaType;
  171. }
  172. }
  173. public void SetMediaType(string value)
  174. {
  175. PlaylistMediaType = value;
  176. }
  177. public override bool IsVisible(User user)
  178. {
  179. var userId = user.Id.ToString("N");
  180. return Shares.Any(i => string.Equals(userId, i.UserId, StringComparison.OrdinalIgnoreCase)) ||
  181. string.Equals(OwnerUserId, userId, StringComparison.OrdinalIgnoreCase);
  182. }
  183. public override bool IsVisibleStandalone(User user)
  184. {
  185. return IsVisible(user);
  186. }
  187. }
  188. }