ChannelManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. using System.Globalization;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Controller.Channels;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Dto;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Providers;
  10. using MediaBrowser.Model.Channels;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Entities;
  13. using MediaBrowser.Model.Logging;
  14. using MediaBrowser.Model.Querying;
  15. using MediaBrowser.Model.Serialization;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. namespace MediaBrowser.Server.Implementations.Channels
  23. {
  24. public class ChannelManager : IChannelManager
  25. {
  26. private IChannel[] _channels;
  27. private IChannelFactory[] _factories;
  28. private List<Channel> _channelEntities = new List<Channel>();
  29. private readonly IUserManager _userManager;
  30. private readonly IUserDataManager _userDataManager;
  31. private readonly IDtoService _dtoService;
  32. private readonly ILibraryManager _libraryManager;
  33. private readonly ILogger _logger;
  34. private readonly IServerConfigurationManager _config;
  35. private readonly IFileSystem _fileSystem;
  36. private readonly IJsonSerializer _jsonSerializer;
  37. public ChannelManager(IUserManager userManager, IDtoService dtoService, ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem, IUserDataManager userDataManager, IJsonSerializer jsonSerializer)
  38. {
  39. _userManager = userManager;
  40. _dtoService = dtoService;
  41. _libraryManager = libraryManager;
  42. _logger = logger;
  43. _config = config;
  44. _fileSystem = fileSystem;
  45. _userDataManager = userDataManager;
  46. _jsonSerializer = jsonSerializer;
  47. }
  48. public void AddParts(IEnumerable<IChannel> channels, IEnumerable<IChannelFactory> factories)
  49. {
  50. _channels = channels.ToArray();
  51. _factories = factories.ToArray();
  52. }
  53. private IEnumerable<IChannel> GetAllChannels()
  54. {
  55. return _factories
  56. .SelectMany(i =>
  57. {
  58. try
  59. {
  60. return i.GetChannels().ToList();
  61. }
  62. catch (Exception ex)
  63. {
  64. _logger.ErrorException("Error getting channel list", ex);
  65. return new List<IChannel>();
  66. }
  67. })
  68. .Concat(_channels)
  69. .OrderBy(i => i.Name);
  70. }
  71. public Task<QueryResult<BaseItemDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
  72. {
  73. var user = string.IsNullOrWhiteSpace(query.UserId)
  74. ? null
  75. : _userManager.GetUserById(new Guid(query.UserId));
  76. var channels = _channelEntities.OrderBy(i => i.SortName).ToList();
  77. if (user != null)
  78. {
  79. channels = channels.Where(i => GetChannelProvider(i).IsEnabledFor(user) && i.IsVisible(user))
  80. .ToList();
  81. }
  82. var all = channels;
  83. var totalCount = all.Count;
  84. if (query.StartIndex.HasValue)
  85. {
  86. all = all.Skip(query.StartIndex.Value).ToList();
  87. }
  88. if (query.Limit.HasValue)
  89. {
  90. all = all.Take(query.Limit.Value).ToList();
  91. }
  92. // Get everything
  93. var fields = Enum.GetNames(typeof(ItemFields))
  94. .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
  95. .ToList();
  96. var returnItems = all.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
  97. .ToArray();
  98. var result = new QueryResult<BaseItemDto>
  99. {
  100. Items = returnItems,
  101. TotalRecordCount = totalCount
  102. };
  103. return Task.FromResult(result);
  104. }
  105. public async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
  106. {
  107. var allChannelsList = GetAllChannels().ToList();
  108. var list = new List<Channel>();
  109. var numComplete = 0;
  110. foreach (var channelInfo in allChannelsList)
  111. {
  112. cancellationToken.ThrowIfCancellationRequested();
  113. try
  114. {
  115. var item = await GetChannel(channelInfo, cancellationToken).ConfigureAwait(false);
  116. list.Add(item);
  117. _libraryManager.RegisterItem(item);
  118. }
  119. catch (OperationCanceledException)
  120. {
  121. throw;
  122. }
  123. catch (Exception ex)
  124. {
  125. _logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Name);
  126. }
  127. numComplete++;
  128. double percent = numComplete;
  129. percent /= allChannelsList.Count;
  130. progress.Report(100 * percent);
  131. }
  132. _channelEntities = list.ToList();
  133. progress.Report(100);
  134. }
  135. public Task<IEnumerable<ChannelMediaInfo>> GetChannelItemMediaSources(string id, CancellationToken cancellationToken)
  136. {
  137. var item = (IChannelMediaItem)_libraryManager.GetItemById(id);
  138. var channelGuid = new Guid(item.ChannelId);
  139. var channel = _channelEntities.First(i => i.Id == channelGuid);
  140. var channelPlugin = GetChannelProvider(channel);
  141. var requiresCallback = channelPlugin as IRequiresMediaInfoCallback;
  142. if (requiresCallback != null)
  143. {
  144. return requiresCallback.GetChannelItemMediaInfo(item.ExternalId, cancellationToken);
  145. }
  146. return Task.FromResult<IEnumerable<ChannelMediaInfo>>(item.ChannelMediaSources);
  147. }
  148. private async Task<Channel> GetChannel(IChannel channelInfo, CancellationToken cancellationToken)
  149. {
  150. var path = Path.Combine(_config.ApplicationPaths.ItemsByNamePath, "channels", _fileSystem.GetValidFilename(channelInfo.Name));
  151. var fileInfo = new DirectoryInfo(path);
  152. var isNew = false;
  153. if (!fileInfo.Exists)
  154. {
  155. _logger.Debug("Creating directory {0}", path);
  156. Directory.CreateDirectory(path);
  157. fileInfo = new DirectoryInfo(path);
  158. if (!fileInfo.Exists)
  159. {
  160. throw new IOException("Path not created: " + path);
  161. }
  162. isNew = true;
  163. }
  164. var id = GetInternalChannelId(channelInfo.Name);
  165. var item = _libraryManager.GetItemById(id) as Channel;
  166. if (item == null)
  167. {
  168. item = new Channel
  169. {
  170. Name = channelInfo.Name,
  171. Id = id,
  172. DateCreated = _fileSystem.GetCreationTimeUtc(fileInfo),
  173. DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo),
  174. Path = path
  175. };
  176. isNew = true;
  177. }
  178. item.HomePageUrl = channelInfo.HomePageUrl;
  179. item.OriginalChannelName = channelInfo.Name;
  180. if (string.IsNullOrEmpty(item.Name))
  181. {
  182. item.Name = channelInfo.Name;
  183. }
  184. await item.RefreshMetadata(new MetadataRefreshOptions
  185. {
  186. ForceSave = isNew
  187. }, cancellationToken);
  188. return item;
  189. }
  190. public Channel GetChannel(string id)
  191. {
  192. return (Channel)_libraryManager.GetItemById(new Guid(id));
  193. }
  194. public ChannelFeatures GetChannelFeatures(string id)
  195. {
  196. var channel = GetChannel(id);
  197. var channelProvider = GetChannelProvider(channel);
  198. return GetChannelFeaturesDto(channelProvider.GetChannelFeatures());
  199. }
  200. public ChannelFeatures GetChannelFeaturesDto(InternalChannelFeatures features)
  201. {
  202. return new ChannelFeatures
  203. {
  204. CanFilter = !features.MaxPageSize.HasValue,
  205. CanSearch = features.CanSearch,
  206. ContentTypes = features.ContentTypes,
  207. DefaultSortFields = features.DefaultSortFields,
  208. MaxPageSize = features.MaxPageSize,
  209. MediaTypes = features.MediaTypes,
  210. SupportsSortOrderToggle = features.SupportsSortOrderToggle
  211. };
  212. }
  213. private Guid GetInternalChannelId(string name)
  214. {
  215. if (string.IsNullOrWhiteSpace(name))
  216. {
  217. throw new ArgumentNullException("name");
  218. }
  219. return ("Channel " + name).GetMBId(typeof(Channel));
  220. }
  221. public async Task<QueryResult<BaseItemDto>> GetChannelItems(ChannelItemQuery query, CancellationToken cancellationToken)
  222. {
  223. var queryChannelId = query.ChannelId;
  224. // Get the internal channel entity
  225. var channel = _channelEntities.First(i => i.Id == new Guid(queryChannelId));
  226. // Find the corresponding channel provider plugin
  227. var channelProvider = GetChannelProvider(channel);
  228. var channelInfo = channelProvider.GetChannelFeatures();
  229. int? providerStartIndex = null;
  230. int? providerLimit = null;
  231. if (channelInfo.MaxPageSize.HasValue)
  232. {
  233. providerStartIndex = query.StartIndex;
  234. if (query.Limit.HasValue && query.Limit.Value > channelInfo.MaxPageSize.Value)
  235. {
  236. throw new ArgumentException(string.Format("{0} channel only supports a maximum of {1} records at a time.", channel.Name, channelInfo.MaxPageSize.Value));
  237. }
  238. providerLimit = query.Limit;
  239. }
  240. var user = string.IsNullOrWhiteSpace(query.UserId)
  241. ? null
  242. : _userManager.GetUserById(new Guid(query.UserId));
  243. ChannelItemSortField? sortField = null;
  244. ChannelItemSortField parsedField;
  245. if (query.SortBy.Length == 1 &&
  246. Enum.TryParse(query.SortBy[0], true, out parsedField))
  247. {
  248. sortField = parsedField;
  249. }
  250. var sortDescending = query.SortOrder.HasValue && query.SortOrder.Value == SortOrder.Descending;
  251. var itemsResult = await GetChannelItems(channelProvider,
  252. user,
  253. query.FolderId,
  254. providerStartIndex,
  255. providerLimit,
  256. sortField,
  257. sortDescending,
  258. cancellationToken)
  259. .ConfigureAwait(false);
  260. var providerTotalRecordCount = providerLimit.HasValue ? itemsResult.TotalRecordCount : null;
  261. var tasks = itemsResult.Items.Select(i => GetChannelItemEntity(i, channelProvider, channel, cancellationToken));
  262. var internalItems = await Task.WhenAll(tasks).ConfigureAwait(false);
  263. if (user != null)
  264. {
  265. internalItems = internalItems.Where(i => i.IsVisible(user)).ToArray();
  266. if (providerTotalRecordCount.HasValue)
  267. {
  268. providerTotalRecordCount = providerTotalRecordCount.Value;
  269. }
  270. }
  271. return await GetReturnItems(internalItems, providerTotalRecordCount, user, query, cancellationToken).ConfigureAwait(false);
  272. }
  273. private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
  274. private async Task<ChannelItemResult> GetChannelItems(IChannel channel,
  275. User user,
  276. string folderId,
  277. int? startIndex,
  278. int? limit,
  279. ChannelItemSortField? sortField,
  280. bool sortDescending,
  281. CancellationToken cancellationToken)
  282. {
  283. var cachePath = GetChannelDataCachePath(channel, user, folderId, sortField, sortDescending);
  284. try
  285. {
  286. if (!startIndex.HasValue && !limit.HasValue)
  287. {
  288. var channelItemResult = _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
  289. if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(channelItemResult.CacheLength) > DateTime.UtcNow)
  290. {
  291. return channelItemResult;
  292. }
  293. }
  294. }
  295. catch (FileNotFoundException)
  296. {
  297. }
  298. catch (DirectoryNotFoundException)
  299. {
  300. }
  301. await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  302. try
  303. {
  304. try
  305. {
  306. if (!startIndex.HasValue && !limit.HasValue)
  307. {
  308. var channelItemResult = _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
  309. if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(channelItemResult.CacheLength) > DateTime.UtcNow)
  310. {
  311. return channelItemResult;
  312. }
  313. }
  314. }
  315. catch (FileNotFoundException)
  316. {
  317. }
  318. catch (DirectoryNotFoundException)
  319. {
  320. }
  321. var query = new InternalChannelItemQuery
  322. {
  323. User = user,
  324. StartIndex = startIndex,
  325. Limit = limit,
  326. SortBy = sortField,
  327. SortDescending = sortDescending
  328. };
  329. if (!string.IsNullOrWhiteSpace(folderId))
  330. {
  331. var categoryItem = (IChannelItem)_libraryManager.GetItemById(new Guid(folderId));
  332. query.FolderId = categoryItem.ExternalId;
  333. }
  334. var result = await channel.GetChannelItems(query, cancellationToken).ConfigureAwait(false);
  335. if (!startIndex.HasValue && !limit.HasValue)
  336. {
  337. CacheResponse(result, cachePath);
  338. }
  339. return result;
  340. }
  341. finally
  342. {
  343. _resourcePool.Release();
  344. }
  345. }
  346. private void CacheResponse(ChannelItemResult result, string path)
  347. {
  348. try
  349. {
  350. Directory.CreateDirectory(Path.GetDirectoryName(path));
  351. _jsonSerializer.SerializeToFile(result, path);
  352. }
  353. catch (Exception ex)
  354. {
  355. _logger.ErrorException("Error writing to channel cache file: {0}", ex, path);
  356. }
  357. }
  358. private string GetChannelDataCachePath(IChannel channel,
  359. User user,
  360. string folderId,
  361. ChannelItemSortField? sortField,
  362. bool sortDescending)
  363. {
  364. var channelId = GetInternalChannelId(channel.Name).ToString("N");
  365. var folderKey = string.IsNullOrWhiteSpace(folderId) ? "root" : folderId.GetMD5().ToString("N");
  366. var version = string.IsNullOrWhiteSpace(channel.DataVersion) ? "0" : channel.DataVersion;
  367. var filename = user.Id.ToString("N");
  368. var hashfilename = false;
  369. if (sortField.HasValue)
  370. {
  371. filename += "-sortField-" + sortField.Value;
  372. hashfilename = true;
  373. }
  374. if (sortDescending)
  375. {
  376. filename += "-sortDescending";
  377. hashfilename = true;
  378. }
  379. if (hashfilename)
  380. {
  381. filename = filename.GetMD5().ToString("N");
  382. }
  383. return Path.Combine(_config.ApplicationPaths.CachePath, "channels", channelId, version, folderKey, filename + ".json");
  384. }
  385. private async Task<QueryResult<BaseItemDto>> GetReturnItems(IEnumerable<BaseItem> items, int? totalCountFromProvider, User user, ChannelItemQuery query, CancellationToken cancellationToken)
  386. {
  387. items = ApplyFilters(items, query.Filters, user);
  388. var sortBy = query.SortBy.Length == 0 ? new[] { ItemSortBy.SortName } : query.SortBy;
  389. items = _libraryManager.Sort(items, user, sortBy, query.SortOrder ?? SortOrder.Ascending);
  390. var all = items.ToList();
  391. var totalCount = totalCountFromProvider ?? all.Count;
  392. if (!totalCountFromProvider.HasValue)
  393. {
  394. if (query.StartIndex.HasValue)
  395. {
  396. all = all.Skip(query.StartIndex.Value).ToList();
  397. }
  398. if (query.Limit.HasValue)
  399. {
  400. all = all.Take(query.Limit.Value).ToList();
  401. }
  402. }
  403. await RefreshIfNeeded(all, cancellationToken).ConfigureAwait(false);
  404. // Get everything
  405. var fields = Enum.GetNames(typeof(ItemFields))
  406. .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
  407. .ToList();
  408. var returnItemArray = all.Select(i => _dtoService.GetBaseItemDto(i, fields, user))
  409. .ToArray();
  410. return new QueryResult<BaseItemDto>
  411. {
  412. Items = returnItemArray,
  413. TotalRecordCount = totalCount
  414. };
  415. }
  416. private string GetIdToHash(string externalId, IChannel channelProvider)
  417. {
  418. // Increment this as needed to force new downloads
  419. // Incorporate Name because it's being used to convert channel entity to provider
  420. return externalId + (channelProvider.DataVersion ?? string.Empty) + (channelProvider.Name ?? string.Empty) + "13";
  421. }
  422. private async Task<BaseItem> GetChannelItemEntity(ChannelItemInfo info, IChannel channelProvider, Channel internalChannel, CancellationToken cancellationToken)
  423. {
  424. BaseItem item;
  425. Guid id;
  426. var isNew = false;
  427. var idToHash = GetIdToHash(info.Id, channelProvider);
  428. if (info.Type == ChannelItemType.Folder)
  429. {
  430. id = idToHash.GetMBId(typeof(ChannelFolderItem));
  431. item = _libraryManager.GetItemById(id) as ChannelFolderItem;
  432. if (item == null)
  433. {
  434. isNew = true;
  435. item = new ChannelFolderItem();
  436. }
  437. }
  438. else if (info.MediaType == ChannelMediaType.Audio)
  439. {
  440. id = idToHash.GetMBId(typeof(ChannelAudioItem));
  441. item = _libraryManager.GetItemById(id) as ChannelAudioItem;
  442. if (item == null)
  443. {
  444. isNew = true;
  445. item = new ChannelAudioItem();
  446. }
  447. }
  448. else
  449. {
  450. id = idToHash.GetMBId(typeof(ChannelVideoItem));
  451. item = _libraryManager.GetItemById(id) as ChannelVideoItem;
  452. if (item == null)
  453. {
  454. isNew = true;
  455. item = new ChannelVideoItem();
  456. }
  457. }
  458. item.Id = id;
  459. item.RunTimeTicks = info.RunTimeTicks;
  460. if (isNew)
  461. {
  462. item.Name = info.Name;
  463. item.Genres = info.Genres;
  464. item.Studios = info.Studios;
  465. item.CommunityRating = info.CommunityRating;
  466. item.OfficialRating = info.OfficialRating;
  467. item.Overview = info.Overview;
  468. item.People = info.People;
  469. item.PremiereDate = info.PremiereDate;
  470. item.ProductionYear = info.ProductionYear;
  471. item.ProviderIds = info.ProviderIds;
  472. if (info.DateCreated.HasValue)
  473. {
  474. item.DateCreated = info.DateCreated.Value;
  475. }
  476. }
  477. var channelItem = (IChannelItem)item;
  478. channelItem.OriginalImageUrl = info.ImageUrl;
  479. channelItem.ExternalId = info.Id;
  480. channelItem.ChannelId = internalChannel.Id.ToString("N");
  481. channelItem.ChannelItemType = info.Type;
  482. if (isNew)
  483. {
  484. channelItem.Tags = info.Tags;
  485. }
  486. var channelMediaItem = item as IChannelMediaItem;
  487. if (channelMediaItem != null)
  488. {
  489. channelMediaItem.IsInfiniteStream = info.IsInfiniteStream;
  490. channelMediaItem.ContentType = info.ContentType;
  491. channelMediaItem.ChannelMediaSources = info.MediaSources;
  492. var mediaSource = info.MediaSources.FirstOrDefault();
  493. item.Path = mediaSource == null ? null : mediaSource.Path;
  494. }
  495. if (isNew)
  496. {
  497. await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
  498. _libraryManager.RegisterItem(item);
  499. }
  500. return item;
  501. }
  502. private async Task RefreshIfNeeded(IEnumerable<BaseItem> programs, CancellationToken cancellationToken)
  503. {
  504. foreach (var program in programs)
  505. {
  506. await RefreshIfNeeded(program, cancellationToken).ConfigureAwait(false);
  507. }
  508. }
  509. private async Task RefreshIfNeeded(BaseItem program, CancellationToken cancellationToken)
  510. {
  511. //if (_refreshedPrograms.ContainsKey(program.Id))
  512. {
  513. //return;
  514. }
  515. await program.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  516. //_refreshedPrograms.TryAdd(program.Id, true);
  517. }
  518. internal IChannel GetChannelProvider(Channel channel)
  519. {
  520. return GetAllChannels().First(i => string.Equals(i.Name, channel.OriginalChannelName, StringComparison.OrdinalIgnoreCase));
  521. }
  522. private IEnumerable<BaseItem> ApplyFilters(IEnumerable<BaseItem> items, IEnumerable<ItemFilter> filters, User user)
  523. {
  524. foreach (var filter in filters.OrderByDescending(f => (int)f))
  525. {
  526. items = ApplyFilter(items, filter, user);
  527. }
  528. return items;
  529. }
  530. private IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user)
  531. {
  532. // Avoid implicitly captured closure
  533. var currentUser = user;
  534. switch (filter)
  535. {
  536. case ItemFilter.IsFavoriteOrLikes:
  537. return items.Where(item =>
  538. {
  539. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  540. if (userdata == null)
  541. {
  542. return false;
  543. }
  544. var likes = userdata.Likes ?? false;
  545. var favorite = userdata.IsFavorite;
  546. return likes || favorite;
  547. });
  548. case ItemFilter.Likes:
  549. return items.Where(item =>
  550. {
  551. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  552. return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
  553. });
  554. case ItemFilter.Dislikes:
  555. return items.Where(item =>
  556. {
  557. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  558. return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
  559. });
  560. case ItemFilter.IsFavorite:
  561. return items.Where(item =>
  562. {
  563. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  564. return userdata != null && userdata.IsFavorite;
  565. });
  566. case ItemFilter.IsResumable:
  567. return items.Where(item =>
  568. {
  569. var userdata = _userDataManager.GetUserData(user.Id, item.GetUserDataKey());
  570. return userdata != null && userdata.PlaybackPositionTicks > 0;
  571. });
  572. case ItemFilter.IsPlayed:
  573. return items.Where(item => item.IsPlayed(currentUser));
  574. case ItemFilter.IsUnplayed:
  575. return items.Where(item => item.IsUnplayed(currentUser));
  576. case ItemFilter.IsFolder:
  577. return items.Where(item => item.IsFolder);
  578. case ItemFilter.IsNotFolder:
  579. return items.Where(item => !item.IsFolder);
  580. }
  581. return items;
  582. }
  583. }
  584. }