Playlist.cs 6.4 KB

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