UserViewBuilder.cs 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. using MediaBrowser.Controller.Channels;
  2. using MediaBrowser.Controller.Entities.Audio;
  3. using MediaBrowser.Controller.Entities.Movies;
  4. using MediaBrowser.Controller.Entities.TV;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.LiveTv;
  7. using MediaBrowser.Controller.Playlists;
  8. using MediaBrowser.Controller.TV;
  9. using MediaBrowser.Model.Channels;
  10. using MediaBrowser.Model.Entities;
  11. using MediaBrowser.Model.LiveTv;
  12. using MediaBrowser.Model.Logging;
  13. using MediaBrowser.Model.Querying;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using MediaBrowser.Controller.Configuration;
  20. using MediaBrowser.Model.Extensions;
  21. namespace MediaBrowser.Controller.Entities
  22. {
  23. public class UserViewBuilder
  24. {
  25. private readonly IChannelManager _channelManager;
  26. private readonly ILiveTvManager _liveTvManager;
  27. private readonly IUserViewManager _userViewManager;
  28. private readonly ILibraryManager _libraryManager;
  29. private readonly ILogger _logger;
  30. private readonly IUserDataManager _userDataManager;
  31. private readonly ITVSeriesManager _tvSeriesManager;
  32. private readonly IServerConfigurationManager _config;
  33. private readonly IPlaylistManager _playlistManager;
  34. public UserViewBuilder(IUserViewManager userViewManager, ILiveTvManager liveTvManager, IChannelManager channelManager, ILibraryManager libraryManager, ILogger logger, IUserDataManager userDataManager, ITVSeriesManager tvSeriesManager, IServerConfigurationManager config, IPlaylistManager playlistManager)
  35. {
  36. _userViewManager = userViewManager;
  37. _liveTvManager = liveTvManager;
  38. _channelManager = channelManager;
  39. _libraryManager = libraryManager;
  40. _logger = logger;
  41. _userDataManager = userDataManager;
  42. _tvSeriesManager = tvSeriesManager;
  43. _config = config;
  44. _playlistManager = playlistManager;
  45. }
  46. public async Task<QueryResult<BaseItem>> GetUserItems(Folder queryParent, Folder displayParent, string viewType, InternalItemsQuery query)
  47. {
  48. var user = query.User;
  49. //if (query.IncludeItemTypes != null &&
  50. // query.IncludeItemTypes.Length == 1 &&
  51. // string.Equals(query.IncludeItemTypes[0], "Playlist", StringComparison.OrdinalIgnoreCase))
  52. //{
  53. // if (!string.Equals(viewType, CollectionType.Playlists, StringComparison.OrdinalIgnoreCase))
  54. // {
  55. // return await FindPlaylists(queryParent, user, query).ConfigureAwait(false);
  56. // }
  57. //}
  58. switch (viewType)
  59. {
  60. case CollectionType.Channels:
  61. {
  62. var result = await _channelManager.GetChannelsInternal(new ChannelQuery
  63. {
  64. UserId = user == null ? null : user.Id.ToString("N"),
  65. Limit = query.Limit,
  66. StartIndex = query.StartIndex
  67. }, CancellationToken.None).ConfigureAwait(false);
  68. return GetResult(result);
  69. }
  70. case SpecialFolder.LiveTvChannels:
  71. {
  72. var result = await _liveTvManager.GetInternalChannels(new LiveTvChannelQuery
  73. {
  74. UserId = query.User.Id.ToString("N"),
  75. Limit = query.Limit,
  76. StartIndex = query.StartIndex
  77. }, CancellationToken.None).ConfigureAwait(false);
  78. return GetResult(result);
  79. }
  80. case SpecialFolder.LiveTvNowPlaying:
  81. {
  82. var result = await _liveTvManager.GetRecommendedProgramsInternal(new RecommendedProgramQuery
  83. {
  84. UserId = query.User.Id.ToString("N"),
  85. Limit = query.Limit,
  86. IsAiring = true
  87. }, new Dto.DtoOptions(), CancellationToken.None).ConfigureAwait(false);
  88. return GetResult(result);
  89. }
  90. case SpecialFolder.LiveTvRecordingGroups:
  91. {
  92. var result = await _liveTvManager.GetInternalRecordings(new RecordingQuery
  93. {
  94. UserId = query.User.Id.ToString("N"),
  95. Status = RecordingStatus.Completed,
  96. Limit = query.Limit,
  97. StartIndex = query.StartIndex
  98. }, CancellationToken.None).ConfigureAwait(false);
  99. return GetResult(result);
  100. }
  101. case CollectionType.LiveTv:
  102. {
  103. return await GetLiveTvView(queryParent, user, query).ConfigureAwait(false);
  104. }
  105. case CollectionType.Photos:
  106. case CollectionType.Books:
  107. case CollectionType.HomeVideos:
  108. case CollectionType.Games:
  109. case CollectionType.MusicVideos:
  110. {
  111. if (query.Recursive)
  112. {
  113. query.Recursive = true;
  114. query.ParentId = queryParent.Id;
  115. query.SetUser(user);
  116. return _libraryManager.GetItemsResult(query);
  117. }
  118. return GetResult(queryParent.GetChildren(user, true), queryParent, query);
  119. }
  120. case CollectionType.Folders:
  121. return GetResult(user.RootFolder.GetChildren(user, true), queryParent, query);
  122. case CollectionType.Playlists:
  123. return await GetPlaylistsView(queryParent, user, query).ConfigureAwait(false);
  124. case CollectionType.BoxSets:
  125. return GetBoxsetView(queryParent, user, query);
  126. case CollectionType.TvShows:
  127. return await GetTvView(queryParent, user, query).ConfigureAwait(false);
  128. case CollectionType.Movies:
  129. return await GetMovieFolders(queryParent, user, query).ConfigureAwait(false);
  130. case SpecialFolder.TvShowSeries:
  131. return GetTvSeries(queryParent, user, query);
  132. case SpecialFolder.TvGenres:
  133. return await GetTvGenres(queryParent, user, query).ConfigureAwait(false);
  134. case SpecialFolder.TvGenre:
  135. return GetTvGenreItems(queryParent, displayParent, user, query);
  136. case SpecialFolder.TvResume:
  137. return GetTvResume(queryParent, user, query);
  138. case SpecialFolder.TvNextUp:
  139. return GetTvNextUp(queryParent, query);
  140. case SpecialFolder.TvLatest:
  141. return GetTvLatest(queryParent, user, query);
  142. case SpecialFolder.MovieFavorites:
  143. return GetFavoriteMovies(queryParent, user, query);
  144. case SpecialFolder.MovieLatest:
  145. return GetMovieLatest(queryParent, user, query);
  146. case SpecialFolder.MovieGenres:
  147. return await GetMovieGenres(queryParent, user, query).ConfigureAwait(false);
  148. case SpecialFolder.MovieGenre:
  149. return await GetMovieGenreItems(queryParent, displayParent, user, query).ConfigureAwait(false);
  150. case SpecialFolder.MovieResume:
  151. return GetMovieResume(queryParent, user, query);
  152. case SpecialFolder.MovieMovies:
  153. return GetMovieMovies(queryParent, user, query);
  154. case SpecialFolder.MovieCollections:
  155. return GetMovieCollections(queryParent, user, query);
  156. case SpecialFolder.TvFavoriteEpisodes:
  157. return GetFavoriteEpisodes(queryParent, user, query);
  158. case SpecialFolder.TvFavoriteSeries:
  159. return GetFavoriteSeries(queryParent, user, query);
  160. case CollectionType.Music:
  161. return await GetMusicFolders(queryParent, user, query).ConfigureAwait(false);
  162. case SpecialFolder.MusicGenres:
  163. return GetMusicGenres(queryParent, user, query);
  164. case SpecialFolder.MusicLatest:
  165. return GetMusicLatest(queryParent, user, query);
  166. case SpecialFolder.MusicPlaylists:
  167. return GetMusicPlaylists(queryParent, user, query);
  168. case SpecialFolder.MusicAlbums:
  169. return GetMusicAlbums(queryParent, user, query);
  170. case SpecialFolder.MusicAlbumArtists:
  171. return GetMusicAlbumArtists(queryParent, user, query);
  172. case SpecialFolder.MusicArtists:
  173. return GetMusicArtists(queryParent, user, query);
  174. case SpecialFolder.MusicSongs:
  175. return GetMusicSongs(queryParent, user, query);
  176. case SpecialFolder.MusicFavorites:
  177. return await GetMusicFavorites(queryParent, user, query).ConfigureAwait(false);
  178. case SpecialFolder.MusicFavoriteAlbums:
  179. return GetFavoriteAlbums(queryParent, user, query);
  180. case SpecialFolder.MusicFavoriteArtists:
  181. return GetFavoriteArtists(queryParent, user, query);
  182. case SpecialFolder.MusicFavoriteSongs:
  183. return GetFavoriteSongs(queryParent, user, query);
  184. default:
  185. {
  186. if (queryParent is UserView)
  187. {
  188. return GetResult(GetMediaFolders(user).SelectMany(i => i.GetChildren(user, true)), queryParent, query);
  189. }
  190. else
  191. {
  192. return GetResult(queryParent.GetChildren(user, true), queryParent, query);
  193. }
  194. }
  195. }
  196. }
  197. private async Task<QueryResult<BaseItem>> GetMusicFolders(Folder parent, User user, InternalItemsQuery query)
  198. {
  199. if (query.Recursive)
  200. {
  201. query.Recursive = true;
  202. query.SetUser(user);
  203. if (query.IncludeItemTypes.Length == 0)
  204. {
  205. query.IncludeItemTypes = new[] { typeof(MusicArtist).Name, typeof(MusicAlbum).Name, typeof(Audio.Audio).Name, typeof(MusicVideo).Name };
  206. }
  207. return parent.QueryRecursive(query);
  208. }
  209. var list = new List<BaseItem>();
  210. list.Add(await GetUserView(SpecialFolder.MusicLatest, "0", parent).ConfigureAwait(false));
  211. list.Add(await GetUserView(SpecialFolder.MusicPlaylists, "1", parent).ConfigureAwait(false));
  212. list.Add(await GetUserView(SpecialFolder.MusicAlbums, "2", parent).ConfigureAwait(false));
  213. list.Add(await GetUserView(SpecialFolder.MusicAlbumArtists, "3", parent).ConfigureAwait(false));
  214. list.Add(await GetUserView(SpecialFolder.MusicArtists, "4", parent).ConfigureAwait(false));
  215. list.Add(await GetUserView(SpecialFolder.MusicSongs, "5", parent).ConfigureAwait(false));
  216. list.Add(await GetUserView(SpecialFolder.MusicGenres, "6", parent).ConfigureAwait(false));
  217. list.Add(await GetUserView(SpecialFolder.MusicFavorites, "7", parent).ConfigureAwait(false));
  218. return GetResult(list, parent, query);
  219. }
  220. private async Task<QueryResult<BaseItem>> GetMusicFavorites(Folder parent, User user, InternalItemsQuery query)
  221. {
  222. var list = new List<BaseItem>();
  223. list.Add(await GetUserView(SpecialFolder.MusicFavoriteAlbums, "0", parent).ConfigureAwait(false));
  224. list.Add(await GetUserView(SpecialFolder.MusicFavoriteArtists, "1", parent).ConfigureAwait(false));
  225. list.Add(await GetUserView(SpecialFolder.MusicFavoriteSongs, "2", parent).ConfigureAwait(false));
  226. return GetResult(list, parent, query);
  227. }
  228. private QueryResult<BaseItem> GetMusicGenres(Folder parent, User user, InternalItemsQuery query)
  229. {
  230. var result = _libraryManager.GetMusicGenres(new InternalItemsQuery(user)
  231. {
  232. AncestorIds = new[] { parent.Id.ToString("N") },
  233. StartIndex = query.StartIndex,
  234. Limit = query.Limit
  235. });
  236. return new QueryResult<BaseItem>
  237. {
  238. TotalRecordCount = result.TotalRecordCount,
  239. Items = result.Items.Select(i => i.Item1).ToArray()
  240. };
  241. }
  242. private QueryResult<BaseItem> GetMusicAlbumArtists(Folder parent, User user, InternalItemsQuery query)
  243. {
  244. var artists = _libraryManager.GetAlbumArtists(new InternalItemsQuery(user)
  245. {
  246. AncestorIds = new[] { parent.Id.ToString("N") },
  247. StartIndex = query.StartIndex,
  248. Limit = query.Limit
  249. });
  250. return new QueryResult<BaseItem>
  251. {
  252. TotalRecordCount = artists.TotalRecordCount,
  253. Items = artists.Items.Select(i => i.Item1).ToArray()
  254. };
  255. }
  256. private QueryResult<BaseItem> GetMusicArtists(Folder parent, User user, InternalItemsQuery query)
  257. {
  258. var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
  259. {
  260. AncestorIds = new[] { parent.Id.ToString("N") },
  261. StartIndex = query.StartIndex,
  262. Limit = query.Limit
  263. });
  264. return new QueryResult<BaseItem>
  265. {
  266. TotalRecordCount = artists.TotalRecordCount,
  267. Items = artists.Items.Select(i => i.Item1).ToArray()
  268. };
  269. }
  270. private QueryResult<BaseItem> GetFavoriteArtists(Folder parent, User user, InternalItemsQuery query)
  271. {
  272. var artists = _libraryManager.GetArtists(new InternalItemsQuery(user)
  273. {
  274. AncestorIds = new[] { parent.Id.ToString("N") },
  275. StartIndex = query.StartIndex,
  276. Limit = query.Limit,
  277. IsFavorite = true
  278. });
  279. return new QueryResult<BaseItem>
  280. {
  281. TotalRecordCount = artists.TotalRecordCount,
  282. Items = artists.Items.Select(i => i.Item1).ToArray()
  283. };
  284. }
  285. private QueryResult<BaseItem> GetMusicPlaylists(Folder parent, User user, InternalItemsQuery query)
  286. {
  287. query.ParentId = null;
  288. query.IncludeItemTypes = new[] { typeof(Playlist).Name };
  289. query.SetUser(user);
  290. query.Recursive = true;
  291. return _libraryManager.GetItemsResult(query);
  292. }
  293. private QueryResult<BaseItem> GetMusicAlbums(Folder parent, User user, InternalItemsQuery query)
  294. {
  295. query.Recursive = true;
  296. query.ParentId = parent.Id;
  297. query.SetUser(user);
  298. query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
  299. return _libraryManager.GetItemsResult(query);
  300. }
  301. private QueryResult<BaseItem> GetMusicSongs(Folder parent, User user, InternalItemsQuery query)
  302. {
  303. query.Recursive = true;
  304. query.ParentId = parent.Id;
  305. query.SetUser(user);
  306. query.IncludeItemTypes = new[] { typeof(Audio.Audio).Name };
  307. return _libraryManager.GetItemsResult(query);
  308. }
  309. private QueryResult<BaseItem> GetMusicLatest(Folder parent, User user, InternalItemsQuery query)
  310. {
  311. var items = _userViewManager.GetLatestItems(new LatestItemsQuery
  312. {
  313. UserId = user.Id.ToString("N"),
  314. Limit = GetSpecialItemsLimit(),
  315. IncludeItemTypes = new[] { typeof(Audio.Audio).Name },
  316. ParentId = parent == null ? null : parent.Id.ToString("N"),
  317. GroupItems = true
  318. }).Select(i => i.Item1 ?? i.Item2.FirstOrDefault()).Where(i => i != null);
  319. query.SortBy = new string[] { };
  320. return PostFilterAndSort(items, parent, null, query, false, true);
  321. }
  322. private QueryResult<BaseItem> GetFavoriteSongs(Folder parent, User user, InternalItemsQuery query)
  323. {
  324. query.Recursive = true;
  325. query.ParentId = parent.Id;
  326. query.SetUser(user);
  327. query.IsFavorite = true;
  328. query.IncludeItemTypes = new[] { typeof(Audio.Audio).Name };
  329. return _libraryManager.GetItemsResult(query);
  330. }
  331. private QueryResult<BaseItem> GetFavoriteAlbums(Folder parent, User user, InternalItemsQuery query)
  332. {
  333. query.Recursive = true;
  334. query.ParentId = parent.Id;
  335. query.SetUser(user);
  336. query.IsFavorite = true;
  337. query.IncludeItemTypes = new[] { typeof(MusicAlbum).Name };
  338. return _libraryManager.GetItemsResult(query);
  339. }
  340. private async Task<QueryResult<BaseItem>> FindPlaylists(Folder parent, User user, InternalItemsQuery query)
  341. {
  342. var list = _playlistManager.GetPlaylists(user.Id.ToString("N"));
  343. return GetResult(list, parent, query);
  344. }
  345. private int GetSpecialItemsLimit()
  346. {
  347. return 50;
  348. }
  349. private async Task<QueryResult<BaseItem>> GetMovieFolders(Folder parent, User user, InternalItemsQuery query)
  350. {
  351. if (query.Recursive)
  352. {
  353. query.Recursive = true;
  354. query.SetUser(user);
  355. if (query.IncludeItemTypes.Length == 0)
  356. {
  357. query.IncludeItemTypes = new[] { typeof(Movie).Name, typeof(BoxSet).Name };
  358. }
  359. return parent.QueryRecursive(query);
  360. }
  361. var list = new List<BaseItem>();
  362. list.Add(await GetUserView(SpecialFolder.MovieResume, "0", parent).ConfigureAwait(false));
  363. list.Add(await GetUserView(SpecialFolder.MovieLatest, "1", parent).ConfigureAwait(false));
  364. list.Add(await GetUserView(SpecialFolder.MovieMovies, "2", parent).ConfigureAwait(false));
  365. list.Add(await GetUserView(SpecialFolder.MovieCollections, "3", parent).ConfigureAwait(false));
  366. list.Add(await GetUserView(SpecialFolder.MovieFavorites, "4", parent).ConfigureAwait(false));
  367. list.Add(await GetUserView(SpecialFolder.MovieGenres, "5", parent).ConfigureAwait(false));
  368. return GetResult(list, parent, query);
  369. }
  370. private QueryResult<BaseItem> GetFavoriteMovies(Folder parent, User user, InternalItemsQuery query)
  371. {
  372. query.Recursive = true;
  373. query.ParentId = parent.Id;
  374. query.SetUser(user);
  375. query.IsFavorite = true;
  376. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  377. return _libraryManager.GetItemsResult(query);
  378. }
  379. private QueryResult<BaseItem> GetFavoriteSeries(Folder parent, User user, InternalItemsQuery query)
  380. {
  381. query.Recursive = true;
  382. query.ParentId = parent.Id;
  383. query.SetUser(user);
  384. query.IsFavorite = true;
  385. query.IncludeItemTypes = new[] { typeof(Series).Name };
  386. return _libraryManager.GetItemsResult(query);
  387. }
  388. private QueryResult<BaseItem> GetFavoriteEpisodes(Folder parent, User user, InternalItemsQuery query)
  389. {
  390. query.Recursive = true;
  391. query.ParentId = parent.Id;
  392. query.SetUser(user);
  393. query.IsFavorite = true;
  394. query.IncludeItemTypes = new[] { typeof(Episode).Name };
  395. return _libraryManager.GetItemsResult(query);
  396. }
  397. private QueryResult<BaseItem> GetMovieMovies(Folder parent, User user, InternalItemsQuery query)
  398. {
  399. query.Recursive = true;
  400. query.ParentId = parent.Id;
  401. query.SetUser(user);
  402. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  403. return _libraryManager.GetItemsResult(query);
  404. }
  405. private QueryResult<BaseItem> GetMovieCollections(Folder parent, User user, InternalItemsQuery query)
  406. {
  407. return GetBoxsetView(parent, user, query);
  408. }
  409. private QueryResult<BaseItem> GetMovieLatest(Folder parent, User user, InternalItemsQuery query)
  410. {
  411. query.SortBy = new[] { ItemSortBy.DateCreated, ItemSortBy.SortName };
  412. query.SortOrder = SortOrder.Descending;
  413. query.Recursive = true;
  414. query.ParentId = parent.Id;
  415. query.SetUser(user);
  416. query.Limit = GetSpecialItemsLimit();
  417. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  418. return ConvertToResult(_libraryManager.GetItemList(query));
  419. }
  420. private QueryResult<BaseItem> GetMovieResume(Folder parent, User user, InternalItemsQuery query)
  421. {
  422. query.SortBy = new[] { ItemSortBy.DatePlayed, ItemSortBy.SortName };
  423. query.SortOrder = SortOrder.Descending;
  424. query.IsResumable = true;
  425. query.Recursive = true;
  426. query.ParentId = parent.Id;
  427. query.SetUser(user);
  428. query.Limit = GetSpecialItemsLimit();
  429. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  430. return ConvertToResult(_libraryManager.GetItemList(query));
  431. }
  432. private QueryResult<BaseItem> ConvertToResult(IEnumerable<BaseItem> items)
  433. {
  434. var arr = items.ToArray();
  435. return new QueryResult<BaseItem>
  436. {
  437. Items = arr,
  438. TotalRecordCount = arr.Length
  439. };
  440. }
  441. private async Task<QueryResult<BaseItem>> GetMovieGenres(Folder parent, User user, InternalItemsQuery query)
  442. {
  443. var tasks = parent.QueryRecursive(new InternalItemsQuery(user)
  444. {
  445. IncludeItemTypes = new[] { typeof(Movie).Name },
  446. Recursive = true,
  447. EnableTotalRecordCount = false
  448. }).Items
  449. .SelectMany(i => i.Genres)
  450. .DistinctNames()
  451. .Select(i =>
  452. {
  453. try
  454. {
  455. return _libraryManager.GetGenre(i);
  456. }
  457. catch
  458. {
  459. // Full exception logged at lower levels
  460. _logger.Error("Error getting genre");
  461. return null;
  462. }
  463. })
  464. .Where(i => i != null)
  465. .Select(i => GetUserView(i.Name, SpecialFolder.MovieGenre, i.SortName, parent));
  466. var genres = await Task.WhenAll(tasks).ConfigureAwait(false);
  467. return GetResult(genres, parent, query);
  468. }
  469. private async Task<QueryResult<BaseItem>> GetMovieGenreItems(Folder queryParent, Folder displayParent, User user, InternalItemsQuery query)
  470. {
  471. query.Recursive = true;
  472. query.ParentId = queryParent.Id;
  473. query.Genres = new[] { displayParent.Name };
  474. query.SetUser(user);
  475. query.IncludeItemTypes = new[] { typeof(Movie).Name };
  476. return _libraryManager.GetItemsResult(query);
  477. }
  478. private async Task<QueryResult<BaseItem>> GetPlaylistsView(Folder parent, User user, InternalItemsQuery query)
  479. {
  480. return GetResult(_playlistManager.GetPlaylists(user.Id.ToString("N")), parent, query);
  481. }
  482. private QueryResult<BaseItem> GetBoxsetView(Folder parent, User user, InternalItemsQuery query)
  483. {
  484. query.ParentId = null;
  485. query.IncludeItemTypes = new[] { typeof(BoxSet).Name };
  486. query.SetUser(user);
  487. query.Recursive = true;
  488. return _libraryManager.GetItemsResult(query);
  489. }
  490. private async Task<QueryResult<BaseItem>> GetTvView(Folder parent, User user, InternalItemsQuery query)
  491. {
  492. if (query.Recursive)
  493. {
  494. query.Recursive = true;
  495. query.SetUser(user);
  496. if (query.IncludeItemTypes.Length == 0)
  497. {
  498. query.IncludeItemTypes = new[] { typeof(Series).Name, typeof(Season).Name, typeof(Episode).Name };
  499. }
  500. return parent.QueryRecursive(query);
  501. }
  502. var list = new List<BaseItem>();
  503. list.Add(await GetUserView(SpecialFolder.TvResume, "0", parent).ConfigureAwait(false));
  504. list.Add(await GetUserView(SpecialFolder.TvNextUp, "1", parent).ConfigureAwait(false));
  505. list.Add(await GetUserView(SpecialFolder.TvLatest, "2", parent).ConfigureAwait(false));
  506. list.Add(await GetUserView(SpecialFolder.TvShowSeries, "3", parent).ConfigureAwait(false));
  507. list.Add(await GetUserView(SpecialFolder.TvFavoriteSeries, "4", parent).ConfigureAwait(false));
  508. list.Add(await GetUserView(SpecialFolder.TvFavoriteEpisodes, "5", parent).ConfigureAwait(false));
  509. list.Add(await GetUserView(SpecialFolder.TvGenres, "6", parent).ConfigureAwait(false));
  510. return GetResult(list, parent, query);
  511. }
  512. private QueryResult<BaseItem> GetTvLatest(Folder parent, User user, InternalItemsQuery query)
  513. {
  514. query.SortBy = new[] { ItemSortBy.DateCreated, ItemSortBy.SortName };
  515. query.SortOrder = SortOrder.Descending;
  516. query.Recursive = true;
  517. query.ParentId = parent.Id;
  518. query.SetUser(user);
  519. query.Limit = GetSpecialItemsLimit();
  520. query.IncludeItemTypes = new[] { typeof(Episode).Name };
  521. query.ExcludeLocationTypes = new[] { LocationType.Virtual };
  522. return ConvertToResult(_libraryManager.GetItemList(query));
  523. }
  524. private QueryResult<BaseItem> GetTvNextUp(Folder parent, InternalItemsQuery query)
  525. {
  526. var parentFolders = GetMediaFolders(parent, query.User, new[] { CollectionType.TvShows, string.Empty });
  527. var result = _tvSeriesManager.GetNextUp(new NextUpQuery
  528. {
  529. Limit = query.Limit,
  530. StartIndex = query.StartIndex,
  531. UserId = query.User.Id.ToString("N")
  532. }, parentFolders);
  533. return result;
  534. }
  535. private QueryResult<BaseItem> GetTvResume(Folder parent, User user, InternalItemsQuery query)
  536. {
  537. query.SortBy = new[] { ItemSortBy.DatePlayed, ItemSortBy.SortName };
  538. query.SortOrder = SortOrder.Descending;
  539. query.IsResumable = true;
  540. query.Recursive = true;
  541. query.ParentId = parent.Id;
  542. query.SetUser(user);
  543. query.Limit = GetSpecialItemsLimit();
  544. query.IncludeItemTypes = new[] { typeof(Episode).Name };
  545. return ConvertToResult(_libraryManager.GetItemList(query));
  546. }
  547. private QueryResult<BaseItem> GetTvSeries(Folder parent, User user, InternalItemsQuery query)
  548. {
  549. query.Recursive = true;
  550. query.ParentId = parent.Id;
  551. query.SetUser(user);
  552. query.IncludeItemTypes = new[] { typeof(Series).Name };
  553. return _libraryManager.GetItemsResult(query);
  554. }
  555. private async Task<QueryResult<BaseItem>> GetTvGenres(Folder parent, User user, InternalItemsQuery query)
  556. {
  557. var tasks = parent.QueryRecursive(new InternalItemsQuery(user)
  558. {
  559. IncludeItemTypes = new[] { typeof(Series).Name },
  560. Recursive = true,
  561. EnableTotalRecordCount = false
  562. }).Items
  563. .SelectMany(i => i.Genres)
  564. .DistinctNames()
  565. .Select(i =>
  566. {
  567. try
  568. {
  569. return _libraryManager.GetGenre(i);
  570. }
  571. catch
  572. {
  573. // Full exception logged at lower levels
  574. _logger.Error("Error getting genre");
  575. return null;
  576. }
  577. })
  578. .Where(i => i != null)
  579. .Select(i => GetUserView(i.Name, SpecialFolder.TvGenre, i.SortName, parent));
  580. var genres = await Task.WhenAll(tasks).ConfigureAwait(false);
  581. return GetResult(genres, parent, query);
  582. }
  583. private QueryResult<BaseItem> GetTvGenreItems(Folder queryParent, Folder displayParent, User user, InternalItemsQuery query)
  584. {
  585. query.Recursive = true;
  586. query.ParentId = queryParent.Id;
  587. query.Genres = new[] { displayParent.Name };
  588. query.SetUser(user);
  589. query.IncludeItemTypes = new[] { typeof(Series).Name };
  590. return _libraryManager.GetItemsResult(query);
  591. }
  592. private QueryResult<BaseItem> GetResult<T>(QueryResult<T> result)
  593. where T : BaseItem
  594. {
  595. return new QueryResult<BaseItem>
  596. {
  597. Items = result.Items,
  598. TotalRecordCount = result.TotalRecordCount
  599. };
  600. }
  601. private QueryResult<BaseItem> GetResult<T>(IEnumerable<T> items,
  602. BaseItem queryParent,
  603. InternalItemsQuery query)
  604. where T : BaseItem
  605. {
  606. items = items.Where(i => Filter(i, query.User, query, _userDataManager, _libraryManager));
  607. return PostFilterAndSort(items, queryParent, null, query, _libraryManager, _config, true, true);
  608. }
  609. public static bool FilterItem(BaseItem item, InternalItemsQuery query)
  610. {
  611. return Filter(item, query.User, query, BaseItem.UserDataManager, BaseItem.LibraryManager);
  612. }
  613. private QueryResult<BaseItem> PostFilterAndSort(IEnumerable<BaseItem> items,
  614. BaseItem queryParent,
  615. int? totalRecordLimit,
  616. InternalItemsQuery query,
  617. bool collapseBoxSetItems,
  618. bool enableSorting)
  619. {
  620. return PostFilterAndSort(items, queryParent, totalRecordLimit, query, _libraryManager, _config, collapseBoxSetItems, enableSorting);
  621. }
  622. public static QueryResult<BaseItem> PostFilterAndSort(IEnumerable<BaseItem> items,
  623. BaseItem queryParent,
  624. int? totalRecordLimit,
  625. InternalItemsQuery query,
  626. ILibraryManager libraryManager,
  627. IServerConfigurationManager configurationManager,
  628. bool collapseBoxSetItems,
  629. bool enableSorting)
  630. {
  631. var user = query.User;
  632. items = FilterVirtualEpisodes(items,
  633. query.IsMissing,
  634. query.IsVirtualUnaired,
  635. query.IsUnaired);
  636. if (collapseBoxSetItems)
  637. {
  638. items = CollapseBoxSetItemsIfNeeded(items, query, queryParent, user, configurationManager);
  639. }
  640. // This must be the last filter
  641. if (!string.IsNullOrEmpty(query.AdjacentTo))
  642. {
  643. items = FilterForAdjacency(items, query.AdjacentTo);
  644. }
  645. return SortAndPage(items, totalRecordLimit, query, libraryManager, enableSorting);
  646. }
  647. public static IEnumerable<BaseItem> CollapseBoxSetItemsIfNeeded(IEnumerable<BaseItem> items,
  648. InternalItemsQuery query,
  649. BaseItem queryParent,
  650. User user,
  651. IServerConfigurationManager configurationManager)
  652. {
  653. if (items == null)
  654. {
  655. throw new ArgumentNullException("items");
  656. }
  657. if (CollapseBoxSetItems(query, queryParent, user, configurationManager))
  658. {
  659. items = BaseItem.CollectionManager.CollapseItemsWithinBoxSets(items, user);
  660. }
  661. items = ApplyPostCollectionCollapseFilters(query, items, user);
  662. return items;
  663. }
  664. private static IEnumerable<BaseItem> ApplyPostCollectionCollapseFilters(InternalItemsQuery request,
  665. IEnumerable<BaseItem> items,
  666. User user)
  667. {
  668. if (!string.IsNullOrEmpty(request.NameStartsWithOrGreater))
  669. {
  670. items = items.Where(i => string.Compare(request.NameStartsWithOrGreater, i.SortName, StringComparison.CurrentCultureIgnoreCase) < 1);
  671. }
  672. if (!string.IsNullOrEmpty(request.NameStartsWith))
  673. {
  674. items = items.Where(i => string.Compare(request.NameStartsWith, i.SortName.Substring(0, 1), StringComparison.CurrentCultureIgnoreCase) == 0);
  675. }
  676. if (!string.IsNullOrEmpty(request.NameLessThan))
  677. {
  678. items = items.Where(i => string.Compare(request.NameLessThan, i.SortName, StringComparison.CurrentCultureIgnoreCase) == 1);
  679. }
  680. return items;
  681. }
  682. public static bool CollapseBoxSetItems(InternalItemsQuery query,
  683. BaseItem queryParent,
  684. User user,
  685. IServerConfigurationManager configurationManager)
  686. {
  687. // Could end up stuck in a loop like this
  688. if (queryParent is BoxSet)
  689. {
  690. return false;
  691. }
  692. var param = query.CollapseBoxSetItems;
  693. if (!param.HasValue)
  694. {
  695. if (user != null && !configurationManager.Configuration.EnableGroupingIntoCollections)
  696. {
  697. return false;
  698. }
  699. if (query.IncludeItemTypes.Length == 0 || query.IncludeItemTypes.Contains("Movie", StringComparer.OrdinalIgnoreCase))
  700. {
  701. param = true;
  702. }
  703. }
  704. return param.HasValue && param.Value && AllowBoxSetCollapsing(query);
  705. }
  706. private static bool AllowBoxSetCollapsing(InternalItemsQuery request)
  707. {
  708. if (request.IsFavorite.HasValue)
  709. {
  710. return false;
  711. }
  712. if (request.IsFavoriteOrLiked.HasValue)
  713. {
  714. return false;
  715. }
  716. if (request.IsLiked.HasValue)
  717. {
  718. return false;
  719. }
  720. if (request.IsPlayed.HasValue)
  721. {
  722. return false;
  723. }
  724. if (request.IsResumable.HasValue)
  725. {
  726. return false;
  727. }
  728. if (request.IsFolder.HasValue)
  729. {
  730. return false;
  731. }
  732. if (request.Genres.Length > 0)
  733. {
  734. return false;
  735. }
  736. if (request.HasImdbId.HasValue)
  737. {
  738. return false;
  739. }
  740. if (request.HasOfficialRating.HasValue)
  741. {
  742. return false;
  743. }
  744. if (request.HasOverview.HasValue)
  745. {
  746. return false;
  747. }
  748. if (request.HasParentalRating.HasValue)
  749. {
  750. return false;
  751. }
  752. if (request.HasSpecialFeature.HasValue)
  753. {
  754. return false;
  755. }
  756. if (request.HasSubtitles.HasValue)
  757. {
  758. return false;
  759. }
  760. if (request.HasThemeSong.HasValue)
  761. {
  762. return false;
  763. }
  764. if (request.HasThemeVideo.HasValue)
  765. {
  766. return false;
  767. }
  768. if (request.HasTmdbId.HasValue)
  769. {
  770. return false;
  771. }
  772. if (request.HasTrailer.HasValue)
  773. {
  774. return false;
  775. }
  776. if (request.ImageTypes.Length > 0)
  777. {
  778. return false;
  779. }
  780. if (request.Is3D.HasValue)
  781. {
  782. return false;
  783. }
  784. if (request.IsHD.HasValue)
  785. {
  786. return false;
  787. }
  788. if (request.IsInBoxSet.HasValue)
  789. {
  790. return false;
  791. }
  792. if (request.IsLocked.HasValue)
  793. {
  794. return false;
  795. }
  796. if (request.IsPlaceHolder.HasValue)
  797. {
  798. return false;
  799. }
  800. if (request.IsPlayed.HasValue)
  801. {
  802. return false;
  803. }
  804. if (!string.IsNullOrWhiteSpace(request.Person))
  805. {
  806. return false;
  807. }
  808. if (request.PersonIds.Length > 0)
  809. {
  810. return false;
  811. }
  812. if (request.ItemIds.Length > 0)
  813. {
  814. return false;
  815. }
  816. if (request.StudioIds.Length > 0)
  817. {
  818. return false;
  819. }
  820. if (request.GenreIds.Length > 0)
  821. {
  822. return false;
  823. }
  824. if (request.VideoTypes.Length > 0)
  825. {
  826. return false;
  827. }
  828. if (request.Years.Length > 0)
  829. {
  830. return false;
  831. }
  832. if (request.Tags.Length > 0)
  833. {
  834. return false;
  835. }
  836. if (request.OfficialRatings.Length > 0)
  837. {
  838. return false;
  839. }
  840. if (request.MinPlayers.HasValue)
  841. {
  842. return false;
  843. }
  844. if (request.MaxPlayers.HasValue)
  845. {
  846. return false;
  847. }
  848. if (request.MinCommunityRating.HasValue)
  849. {
  850. return false;
  851. }
  852. if (request.MinCriticRating.HasValue)
  853. {
  854. return false;
  855. }
  856. if (request.MinIndexNumber.HasValue)
  857. {
  858. return false;
  859. }
  860. return true;
  861. }
  862. private static IEnumerable<BaseItem> FilterVirtualEpisodes(
  863. IEnumerable<BaseItem> items,
  864. bool? isMissing,
  865. bool? isVirtualUnaired,
  866. bool? isUnaired)
  867. {
  868. if (isMissing.HasValue)
  869. {
  870. var val = isMissing.Value;
  871. items = items.Where(i =>
  872. {
  873. var e = i as Episode;
  874. if (e != null)
  875. {
  876. return e.IsMissingEpisode == val;
  877. }
  878. return true;
  879. });
  880. }
  881. if (isUnaired.HasValue)
  882. {
  883. var val = isUnaired.Value;
  884. items = items.Where(i =>
  885. {
  886. var e = i as Episode;
  887. if (e != null)
  888. {
  889. return e.IsUnaired == val;
  890. }
  891. return true;
  892. });
  893. }
  894. if (isVirtualUnaired.HasValue)
  895. {
  896. var val = isVirtualUnaired.Value;
  897. items = items.Where(i =>
  898. {
  899. var e = i as Episode;
  900. if (e != null)
  901. {
  902. return e.IsVirtualUnaired == val;
  903. }
  904. return true;
  905. });
  906. }
  907. return items;
  908. }
  909. public static QueryResult<BaseItem> SortAndPage(IEnumerable<BaseItem> items,
  910. int? totalRecordLimit,
  911. InternalItemsQuery query,
  912. ILibraryManager libraryManager, bool enableSorting)
  913. {
  914. var user = query.User;
  915. items = items.DistinctBy(i => i.GetPresentationUniqueKey(), StringComparer.OrdinalIgnoreCase);
  916. if (query.SortBy.Length > 0)
  917. {
  918. items = libraryManager.Sort(items, user, query.SortBy, query.SortOrder);
  919. }
  920. var itemsArray = totalRecordLimit.HasValue ? items.Take(totalRecordLimit.Value).ToArray() : items.ToArray();
  921. var totalCount = itemsArray.Length;
  922. if (query.Limit.HasValue)
  923. {
  924. itemsArray = itemsArray.Skip(query.StartIndex ?? 0).Take(query.Limit.Value).ToArray();
  925. }
  926. else if (query.StartIndex.HasValue)
  927. {
  928. itemsArray = itemsArray.Skip(query.StartIndex.Value).ToArray();
  929. }
  930. return new QueryResult<BaseItem>
  931. {
  932. TotalRecordCount = totalCount,
  933. Items = itemsArray
  934. };
  935. }
  936. public static bool Filter(BaseItem item, User user, InternalItemsQuery query, IUserDataManager userDataManager, ILibraryManager libraryManager)
  937. {
  938. if (query.ItemIdsFromPersonFilters == null)
  939. {
  940. if (query.PersonIds.Length > 0)
  941. {
  942. var names = query.PersonIds
  943. .Select(libraryManager.GetItemById)
  944. .Select(i => i == null ? null : i.Name)
  945. .Where(i => !string.IsNullOrWhiteSpace(i))
  946. .ToList();
  947. var itemIdList = new List<Guid>();
  948. foreach (var name in names)
  949. {
  950. itemIdList.AddRange(libraryManager.GetItemIds(new InternalItemsQuery
  951. {
  952. Person = name
  953. }));
  954. }
  955. query.ItemIdsFromPersonFilters = itemIdList;
  956. }
  957. // Apply person filter
  958. else if (!string.IsNullOrWhiteSpace(query.Person))
  959. {
  960. var itemIdList = new List<Guid>();
  961. itemIdList.AddRange(libraryManager.GetItemIds(new InternalItemsQuery
  962. {
  963. Person = query.Person,
  964. PersonTypes = query.PersonTypes
  965. }));
  966. query.ItemIdsFromPersonFilters = itemIdList;
  967. }
  968. }
  969. if (query.MediaTypes.Length > 0 && !query.MediaTypes.Contains(item.MediaType ?? string.Empty, StringComparer.OrdinalIgnoreCase))
  970. {
  971. return false;
  972. }
  973. if (query.IncludeItemTypes.Length > 0 && !query.IncludeItemTypes.Contains(item.GetClientTypeName(), StringComparer.OrdinalIgnoreCase))
  974. {
  975. return false;
  976. }
  977. if (query.ExcludeItemTypes.Length > 0 && query.ExcludeItemTypes.Contains(item.GetClientTypeName(), StringComparer.OrdinalIgnoreCase))
  978. {
  979. return false;
  980. }
  981. if (query.ExcludeLocationTypes.Length > 0 && query.ExcludeLocationTypes.Contains(item.LocationType))
  982. {
  983. return false;
  984. }
  985. if (query.IsFolder.HasValue && query.IsFolder.Value != item.IsFolder)
  986. {
  987. return false;
  988. }
  989. UserItemData userData = null;
  990. if (query.IsLiked.HasValue)
  991. {
  992. userData = userData ?? userDataManager.GetUserData(user, item);
  993. if (!userData.Likes.HasValue || userData.Likes != query.IsLiked.Value)
  994. {
  995. return false;
  996. }
  997. }
  998. if (query.IsFavoriteOrLiked.HasValue)
  999. {
  1000. userData = userData ?? userDataManager.GetUserData(user, item);
  1001. var isFavoriteOrLiked = userData.IsFavorite || (userData.Likes ?? false);
  1002. if (isFavoriteOrLiked != query.IsFavoriteOrLiked.Value)
  1003. {
  1004. return false;
  1005. }
  1006. }
  1007. if (query.IsFavorite.HasValue)
  1008. {
  1009. userData = userData ?? userDataManager.GetUserData(user, item);
  1010. if (userData.IsFavorite != query.IsFavorite.Value)
  1011. {
  1012. return false;
  1013. }
  1014. }
  1015. if (query.IsResumable.HasValue)
  1016. {
  1017. userData = userData ?? userDataManager.GetUserData(user, item);
  1018. var isResumable = userData.PlaybackPositionTicks > 0;
  1019. if (isResumable != query.IsResumable.Value)
  1020. {
  1021. return false;
  1022. }
  1023. }
  1024. if (query.IsPlayed.HasValue)
  1025. {
  1026. if (item.IsPlayed(user) != query.IsPlayed.Value)
  1027. {
  1028. return false;
  1029. }
  1030. }
  1031. if (query.IsInBoxSet.HasValue)
  1032. {
  1033. var val = query.IsInBoxSet.Value;
  1034. if (item.GetParents().OfType<BoxSet>().Any() != val)
  1035. {
  1036. return false;
  1037. }
  1038. }
  1039. // Filter by Video3DFormat
  1040. if (query.Is3D.HasValue)
  1041. {
  1042. var val = query.Is3D.Value;
  1043. var video = item as Video;
  1044. if (video == null || val != video.Video3DFormat.HasValue)
  1045. {
  1046. return false;
  1047. }
  1048. }
  1049. if (query.IsHD.HasValue)
  1050. {
  1051. var val = query.IsHD.Value;
  1052. var video = item as Video;
  1053. if (video == null || !video.IsHD.HasValue || val != video.IsHD)
  1054. {
  1055. return false;
  1056. }
  1057. }
  1058. if (query.IsLocked.HasValue)
  1059. {
  1060. var val = query.IsLocked.Value;
  1061. if (item.IsLocked != val)
  1062. {
  1063. return false;
  1064. }
  1065. }
  1066. if (query.HasOverview.HasValue)
  1067. {
  1068. var filterValue = query.HasOverview.Value;
  1069. var hasValue = !string.IsNullOrEmpty(item.Overview);
  1070. if (hasValue != filterValue)
  1071. {
  1072. return false;
  1073. }
  1074. }
  1075. if (query.HasImdbId.HasValue)
  1076. {
  1077. var filterValue = query.HasImdbId.Value;
  1078. var hasValue = !string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Imdb));
  1079. if (hasValue != filterValue)
  1080. {
  1081. return false;
  1082. }
  1083. }
  1084. if (query.HasTmdbId.HasValue)
  1085. {
  1086. var filterValue = query.HasTmdbId.Value;
  1087. var hasValue = !string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb));
  1088. if (hasValue != filterValue)
  1089. {
  1090. return false;
  1091. }
  1092. }
  1093. if (query.HasTvdbId.HasValue)
  1094. {
  1095. var filterValue = query.HasTvdbId.Value;
  1096. var hasValue = !string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tvdb));
  1097. if (hasValue != filterValue)
  1098. {
  1099. return false;
  1100. }
  1101. }
  1102. if (query.HasOfficialRating.HasValue)
  1103. {
  1104. var filterValue = query.HasOfficialRating.Value;
  1105. var hasValue = !string.IsNullOrEmpty(item.OfficialRating);
  1106. if (hasValue != filterValue)
  1107. {
  1108. return false;
  1109. }
  1110. }
  1111. if (query.IsPlaceHolder.HasValue)
  1112. {
  1113. var filterValue = query.IsPlaceHolder.Value;
  1114. var isPlaceHolder = false;
  1115. var hasPlaceHolder = item as ISupportsPlaceHolders;
  1116. if (hasPlaceHolder != null)
  1117. {
  1118. isPlaceHolder = hasPlaceHolder.IsPlaceHolder;
  1119. }
  1120. if (isPlaceHolder != filterValue)
  1121. {
  1122. return false;
  1123. }
  1124. }
  1125. if (query.HasSpecialFeature.HasValue)
  1126. {
  1127. var filterValue = query.HasSpecialFeature.Value;
  1128. var movie = item as IHasSpecialFeatures;
  1129. if (movie != null)
  1130. {
  1131. var ok = filterValue
  1132. ? movie.SpecialFeatureIds.Count > 0
  1133. : movie.SpecialFeatureIds.Count == 0;
  1134. if (!ok)
  1135. {
  1136. return false;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. return false;
  1142. }
  1143. }
  1144. if (query.HasSubtitles.HasValue)
  1145. {
  1146. var val = query.HasSubtitles.Value;
  1147. var video = item as Video;
  1148. if (video == null || val != video.HasSubtitles)
  1149. {
  1150. return false;
  1151. }
  1152. }
  1153. if (query.HasParentalRating.HasValue)
  1154. {
  1155. var val = query.HasParentalRating.Value;
  1156. var rating = item.CustomRating;
  1157. if (string.IsNullOrEmpty(rating))
  1158. {
  1159. rating = item.OfficialRating;
  1160. }
  1161. if (val)
  1162. {
  1163. if (string.IsNullOrEmpty(rating))
  1164. {
  1165. return false;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. if (!string.IsNullOrEmpty(rating))
  1171. {
  1172. return false;
  1173. }
  1174. }
  1175. }
  1176. if (query.HasTrailer.HasValue)
  1177. {
  1178. var val = query.HasTrailer.Value;
  1179. var trailerCount = 0;
  1180. var hasTrailers = item as IHasTrailers;
  1181. if (hasTrailers != null)
  1182. {
  1183. trailerCount = hasTrailers.GetTrailerIds().Count;
  1184. }
  1185. var ok = val ? trailerCount > 0 : trailerCount == 0;
  1186. if (!ok)
  1187. {
  1188. return false;
  1189. }
  1190. }
  1191. if (query.HasThemeSong.HasValue)
  1192. {
  1193. var filterValue = query.HasThemeSong.Value;
  1194. var themeCount = item.ThemeSongIds.Count;
  1195. var ok = filterValue ? themeCount > 0 : themeCount == 0;
  1196. if (!ok)
  1197. {
  1198. return false;
  1199. }
  1200. }
  1201. if (query.HasThemeVideo.HasValue)
  1202. {
  1203. var filterValue = query.HasThemeVideo.Value;
  1204. var themeCount = item.ThemeVideoIds.Count;
  1205. var ok = filterValue ? themeCount > 0 : themeCount == 0;
  1206. if (!ok)
  1207. {
  1208. return false;
  1209. }
  1210. }
  1211. // Apply genre filter
  1212. if (query.Genres.Length > 0 && !query.Genres.Any(v => item.Genres.Contains(v, StringComparer.OrdinalIgnoreCase)))
  1213. {
  1214. return false;
  1215. }
  1216. // Filter by VideoType
  1217. if (query.VideoTypes.Length > 0)
  1218. {
  1219. var video = item as Video;
  1220. if (video == null || !query.VideoTypes.Contains(video.VideoType))
  1221. {
  1222. return false;
  1223. }
  1224. }
  1225. if (query.ImageTypes.Length > 0 && !query.ImageTypes.Any(item.HasImage))
  1226. {
  1227. return false;
  1228. }
  1229. // Apply studio filter
  1230. if (query.StudioIds.Length > 0 && !query.StudioIds.Any(id =>
  1231. {
  1232. var studioItem = libraryManager.GetItemById(id);
  1233. return studioItem != null && item.Studios.Contains(studioItem.Name, StringComparer.OrdinalIgnoreCase);
  1234. }))
  1235. {
  1236. return false;
  1237. }
  1238. // Apply genre filter
  1239. if (query.GenreIds.Length > 0 && !query.GenreIds.Any(id =>
  1240. {
  1241. var genreItem = libraryManager.GetItemById(id);
  1242. return genreItem != null && item.Genres.Contains(genreItem.Name, StringComparer.OrdinalIgnoreCase);
  1243. }))
  1244. {
  1245. return false;
  1246. }
  1247. // Apply year filter
  1248. if (query.Years.Length > 0)
  1249. {
  1250. if (!(item.ProductionYear.HasValue && query.Years.Contains(item.ProductionYear.Value)))
  1251. {
  1252. return false;
  1253. }
  1254. }
  1255. // Apply official rating filter
  1256. if (query.OfficialRatings.Length > 0 && !query.OfficialRatings.Contains(item.OfficialRating ?? string.Empty))
  1257. {
  1258. return false;
  1259. }
  1260. if (query.ItemIds.Length > 0)
  1261. {
  1262. if (!query.ItemIds.Contains(item.Id.ToString("N"), StringComparer.OrdinalIgnoreCase))
  1263. {
  1264. return false;
  1265. }
  1266. }
  1267. // Apply person filter
  1268. if (query.ItemIdsFromPersonFilters != null)
  1269. {
  1270. if (!query.ItemIdsFromPersonFilters.Contains(item.Id))
  1271. {
  1272. return false;
  1273. }
  1274. }
  1275. // Apply tag filter
  1276. var tags = query.Tags;
  1277. if (tags.Length > 0)
  1278. {
  1279. if (!tags.Any(v => item.Tags.Contains(v, StringComparer.OrdinalIgnoreCase)))
  1280. {
  1281. return false;
  1282. }
  1283. }
  1284. if (query.MinPlayers.HasValue)
  1285. {
  1286. var filterValue = query.MinPlayers.Value;
  1287. var game = item as Game;
  1288. if (game != null)
  1289. {
  1290. var players = game.PlayersSupported ?? 1;
  1291. var ok = players >= filterValue;
  1292. if (!ok)
  1293. {
  1294. return false;
  1295. }
  1296. }
  1297. else
  1298. {
  1299. return false;
  1300. }
  1301. }
  1302. if (query.MaxPlayers.HasValue)
  1303. {
  1304. var filterValue = query.MaxPlayers.Value;
  1305. var game = item as Game;
  1306. if (game != null)
  1307. {
  1308. var players = game.PlayersSupported ?? 1;
  1309. var ok = players <= filterValue;
  1310. if (!ok)
  1311. {
  1312. return false;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. return false;
  1318. }
  1319. }
  1320. if (query.MinCommunityRating.HasValue)
  1321. {
  1322. var val = query.MinCommunityRating.Value;
  1323. if (!(item.CommunityRating.HasValue && item.CommunityRating >= val))
  1324. {
  1325. return false;
  1326. }
  1327. }
  1328. if (query.MinCriticRating.HasValue)
  1329. {
  1330. var val = query.MinCriticRating.Value;
  1331. if (!(item.CriticRating.HasValue && item.CriticRating >= val))
  1332. {
  1333. return false;
  1334. }
  1335. }
  1336. if (query.MinIndexNumber.HasValue)
  1337. {
  1338. var val = query.MinIndexNumber.Value;
  1339. if (!(item.IndexNumber.HasValue && item.IndexNumber.Value >= val))
  1340. {
  1341. return false;
  1342. }
  1343. }
  1344. if (query.MinPremiereDate.HasValue)
  1345. {
  1346. var val = query.MinPremiereDate.Value;
  1347. if (!(item.PremiereDate.HasValue && item.PremiereDate.Value >= val))
  1348. {
  1349. return false;
  1350. }
  1351. }
  1352. if (query.MaxPremiereDate.HasValue)
  1353. {
  1354. var val = query.MaxPremiereDate.Value;
  1355. if (!(item.PremiereDate.HasValue && item.PremiereDate.Value <= val))
  1356. {
  1357. return false;
  1358. }
  1359. }
  1360. if (query.ParentIndexNumber.HasValue)
  1361. {
  1362. var filterValue = query.ParentIndexNumber.Value;
  1363. if (item.ParentIndexNumber.HasValue && item.ParentIndexNumber.Value != filterValue)
  1364. {
  1365. return false;
  1366. }
  1367. }
  1368. if (query.AirDays.Length > 0)
  1369. {
  1370. var ok = new[] { item }.OfType<Series>().Any(p => p.AirDays != null && query.AirDays.Any(d => p.AirDays.Contains(d)));
  1371. if (!ok)
  1372. {
  1373. return false;
  1374. }
  1375. }
  1376. if (query.SeriesStatuses.Length > 0)
  1377. {
  1378. var ok = new[] { item }.OfType<Series>().Any(p => p.Status.HasValue && query.SeriesStatuses.Contains(p.Status.Value));
  1379. if (!ok)
  1380. {
  1381. return false;
  1382. }
  1383. }
  1384. if (query.AiredDuringSeason.HasValue)
  1385. {
  1386. var episode = item as Episode;
  1387. if (episode == null)
  1388. {
  1389. return false;
  1390. }
  1391. if (!Series.FilterEpisodesBySeason(new[] { episode }, query.AiredDuringSeason.Value, true).Any())
  1392. {
  1393. return false;
  1394. }
  1395. }
  1396. if (!string.IsNullOrEmpty(query.AlbumArtistStartsWithOrGreater))
  1397. {
  1398. var ok = new[] { item }.OfType<IHasAlbumArtist>()
  1399. .Any(p => string.Compare(query.AlbumArtistStartsWithOrGreater, p.AlbumArtists.FirstOrDefault(), StringComparison.CurrentCultureIgnoreCase) < 1);
  1400. if (!ok)
  1401. {
  1402. return false;
  1403. }
  1404. }
  1405. // Artists
  1406. if (query.ArtistIds.Length > 0)
  1407. {
  1408. var audio = item as IHasArtist;
  1409. //if (!(audio != null && query.ArtistNames.Any(audio.HasAnyArtist)))
  1410. //{
  1411. // return false;
  1412. //}
  1413. }
  1414. // Albums
  1415. if (query.AlbumNames.Length > 0)
  1416. {
  1417. var audio = item as Audio.Audio;
  1418. if (audio != null)
  1419. {
  1420. if (!query.AlbumNames.Any(a => string.Equals(a, audio.Album, StringComparison.OrdinalIgnoreCase)))
  1421. {
  1422. return false;
  1423. }
  1424. }
  1425. var album = item as MusicAlbum;
  1426. if (album != null)
  1427. {
  1428. if (!query.AlbumNames.Any(a => string.Equals(a, album.Name, StringComparison.OrdinalIgnoreCase)))
  1429. {
  1430. return false;
  1431. }
  1432. }
  1433. var musicVideo = item as MusicVideo;
  1434. if (musicVideo != null)
  1435. {
  1436. if (!query.AlbumNames.Any(a => string.Equals(a, musicVideo.Album, StringComparison.OrdinalIgnoreCase)))
  1437. {
  1438. return false;
  1439. }
  1440. }
  1441. return false;
  1442. }
  1443. return true;
  1444. }
  1445. private IEnumerable<Folder> GetMediaFolders(User user)
  1446. {
  1447. if (user == null)
  1448. {
  1449. return _libraryManager.RootFolder
  1450. .Children
  1451. .OfType<Folder>()
  1452. .Where(UserView.IsEligibleForGrouping);
  1453. }
  1454. return user.RootFolder
  1455. .GetChildren(user, true)
  1456. .OfType<Folder>()
  1457. .Where(i => user.IsFolderGrouped(i.Id) && UserView.IsEligibleForGrouping(i));
  1458. }
  1459. private List<Folder> GetMediaFolders(User user, IEnumerable<string> viewTypes)
  1460. {
  1461. if (user == null)
  1462. {
  1463. return GetMediaFolders(null)
  1464. .Where(i =>
  1465. {
  1466. var folder = i as ICollectionFolder;
  1467. return folder != null && viewTypes.Contains(folder.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  1468. }).ToList();
  1469. }
  1470. return GetMediaFolders(user)
  1471. .Where(i =>
  1472. {
  1473. var folder = i as ICollectionFolder;
  1474. return folder != null && viewTypes.Contains(folder.CollectionType ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  1475. }).ToList();
  1476. }
  1477. private List<Folder> GetMediaFolders(Folder parent, User user, IEnumerable<string> viewTypes)
  1478. {
  1479. if (parent == null || parent is UserView)
  1480. {
  1481. return GetMediaFolders(user, viewTypes);
  1482. }
  1483. return new List<Folder> { parent };
  1484. }
  1485. private IEnumerable<BaseItem> GetRecursiveChildren(Folder parent, User user, IEnumerable<string> viewTypes)
  1486. {
  1487. if (parent == null || parent is UserView)
  1488. {
  1489. if (user == null)
  1490. {
  1491. return GetMediaFolders(null, viewTypes).SelectMany(i => i.GetRecursiveChildren());
  1492. }
  1493. return GetMediaFolders(user, viewTypes).SelectMany(i => i.GetRecursiveChildren(user));
  1494. }
  1495. if (user == null)
  1496. {
  1497. return parent.GetRecursiveChildren();
  1498. }
  1499. return parent.GetRecursiveChildren(user);
  1500. }
  1501. private async Task<QueryResult<BaseItem>> GetLiveTvView(Folder queryParent, User user, InternalItemsQuery query)
  1502. {
  1503. if (query.Recursive)
  1504. {
  1505. return await _liveTvManager.GetInternalRecordings(new RecordingQuery
  1506. {
  1507. IsInProgress = false,
  1508. Status = RecordingStatus.Completed,
  1509. UserId = user.Id.ToString("N")
  1510. }, CancellationToken.None).ConfigureAwait(false);
  1511. }
  1512. var list = new List<BaseItem>();
  1513. //list.Add(await GetUserSubView(SpecialFolder.LiveTvNowPlaying, user, "0", parent).ConfigureAwait(false));
  1514. list.Add(await GetUserView(SpecialFolder.LiveTvChannels, string.Empty, user.RootFolder).ConfigureAwait(false));
  1515. list.Add(await GetUserView(SpecialFolder.LiveTvRecordingGroups, string.Empty, user.RootFolder).ConfigureAwait(false));
  1516. return GetResult(list, queryParent, query);
  1517. }
  1518. private Task<UserView> GetUserView(string name, string type, string sortName, BaseItem parent)
  1519. {
  1520. return _userViewManager.GetUserSubView(name, parent.Id.ToString("N"), type, sortName, CancellationToken.None);
  1521. }
  1522. private Task<UserView> GetUserView(string type, string sortName, BaseItem parent)
  1523. {
  1524. return _userViewManager.GetUserSubView(parent.Id.ToString("N"), type, sortName, CancellationToken.None);
  1525. }
  1526. public static IEnumerable<BaseItem> FilterForAdjacency(IEnumerable<BaseItem> items, string adjacentToId)
  1527. {
  1528. var list = items.ToList();
  1529. var adjacentToIdGuid = new Guid(adjacentToId);
  1530. var adjacentToItem = list.FirstOrDefault(i => i.Id == adjacentToIdGuid);
  1531. var index = list.IndexOf(adjacentToItem);
  1532. var previousId = Guid.Empty;
  1533. var nextId = Guid.Empty;
  1534. if (index > 0)
  1535. {
  1536. previousId = list[index - 1].Id;
  1537. }
  1538. if (index < list.Count - 1)
  1539. {
  1540. nextId = list[index + 1].Id;
  1541. }
  1542. return list.Where(i => i.Id == previousId || i.Id == nextId || i.Id == adjacentToIdGuid);
  1543. }
  1544. }
  1545. }