Folder.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  1. using MediaBrowser.Common.Progress;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Providers;
  4. using MediaBrowser.Model.Dto;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.Querying;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using MediaBrowser.Common.IO;
  14. using MediaBrowser.Controller.Channels;
  15. using MediaBrowser.Controller.Entities.Audio;
  16. using MediaBrowser.Controller.Entities.TV;
  17. using MediaBrowser.Controller.IO;
  18. using MediaBrowser.Model.Channels;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Serialization;
  21. namespace MediaBrowser.Controller.Entities
  22. {
  23. /// <summary>
  24. /// Class Folder
  25. /// </summary>
  26. public class Folder : BaseItem
  27. {
  28. public static IUserManager UserManager { get; set; }
  29. public static IUserViewManager UserViewManager { get; set; }
  30. /// <summary>
  31. /// Gets or sets a value indicating whether this instance is root.
  32. /// </summary>
  33. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  34. public bool IsRoot { get; set; }
  35. public virtual List<LinkedChild> LinkedChildren { get; set; }
  36. [IgnoreDataMember]
  37. public DateTime? DateLastMediaAdded { get; set; }
  38. public Folder()
  39. {
  40. LinkedChildren = new List<LinkedChild>();
  41. }
  42. [IgnoreDataMember]
  43. public override bool SupportsThemeMedia
  44. {
  45. get { return true; }
  46. }
  47. [IgnoreDataMember]
  48. public virtual bool IsPreSorted
  49. {
  50. get { return false; }
  51. }
  52. [IgnoreDataMember]
  53. public virtual bool IsPhysicalRoot
  54. {
  55. get { return false; }
  56. }
  57. [IgnoreDataMember]
  58. public override bool SupportsPlayedStatus
  59. {
  60. get
  61. {
  62. return true;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance is folder.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance is folder; otherwise, <c>false</c>.</value>
  69. [IgnoreDataMember]
  70. public override bool IsFolder
  71. {
  72. get
  73. {
  74. return true;
  75. }
  76. }
  77. [IgnoreDataMember]
  78. public override bool IsDisplayedAsFolder
  79. {
  80. get
  81. {
  82. return true;
  83. }
  84. }
  85. [IgnoreDataMember]
  86. public virtual bool SupportsCumulativeRunTimeTicks
  87. {
  88. get
  89. {
  90. return false;
  91. }
  92. }
  93. [IgnoreDataMember]
  94. public virtual bool SupportsDateLastMediaAdded
  95. {
  96. get
  97. {
  98. return false;
  99. }
  100. }
  101. public override bool CanDelete()
  102. {
  103. if (IsRoot)
  104. {
  105. return false;
  106. }
  107. return base.CanDelete();
  108. }
  109. public override bool RequiresRefresh()
  110. {
  111. var baseResult = base.RequiresRefresh();
  112. if (SupportsCumulativeRunTimeTicks && !RunTimeTicks.HasValue)
  113. {
  114. baseResult = true;
  115. }
  116. return baseResult;
  117. }
  118. [IgnoreDataMember]
  119. public override string FileNameWithoutExtension
  120. {
  121. get
  122. {
  123. if (LocationType == LocationType.FileSystem)
  124. {
  125. return System.IO.Path.GetFileName(Path);
  126. }
  127. return null;
  128. }
  129. }
  130. protected override bool IsAllowTagFilterEnforced()
  131. {
  132. if (this is ICollectionFolder)
  133. {
  134. return false;
  135. }
  136. if (this is UserView)
  137. {
  138. return false;
  139. }
  140. return true;
  141. }
  142. [IgnoreDataMember]
  143. protected virtual bool SupportsShortcutChildren
  144. {
  145. get { return false; }
  146. }
  147. /// <summary>
  148. /// Adds the child.
  149. /// </summary>
  150. /// <param name="item">The item.</param>
  151. /// <param name="cancellationToken">The cancellation token.</param>
  152. /// <returns>Task.</returns>
  153. /// <exception cref="System.InvalidOperationException">Unable to add + item.Name</exception>
  154. public async Task AddChild(BaseItem item, CancellationToken cancellationToken)
  155. {
  156. item.SetParent(this);
  157. if (item.Id == Guid.Empty)
  158. {
  159. item.Id = LibraryManager.GetNewItemId(item.Path, item.GetType());
  160. }
  161. if (ActualChildren.Any(i => i.Id == item.Id))
  162. {
  163. throw new ArgumentException(string.Format("A child with the Id {0} already exists.", item.Id));
  164. }
  165. if (item.DateCreated == DateTime.MinValue)
  166. {
  167. item.DateCreated = DateTime.UtcNow;
  168. }
  169. if (item.DateModified == DateTime.MinValue)
  170. {
  171. item.DateModified = DateTime.UtcNow;
  172. }
  173. await LibraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
  174. }
  175. /// <summary>
  176. /// Removes the child.
  177. /// </summary>
  178. /// <param name="item">The item.</param>
  179. public void RemoveChild(BaseItem item)
  180. {
  181. item.SetParent(null);
  182. }
  183. /// <summary>
  184. /// Returns the valid set of index by options for this folder type.
  185. /// Override or extend to modify.
  186. /// </summary>
  187. /// <returns>Dictionary{System.StringFunc{UserIEnumerable{BaseItem}}}.</returns>
  188. protected virtual IEnumerable<string> GetIndexByOptions()
  189. {
  190. return new List<string> {
  191. {"None"},
  192. {"Performer"},
  193. {"Genre"},
  194. {"Director"},
  195. {"Year"},
  196. {"Studio"}
  197. };
  198. }
  199. /// <summary>
  200. /// Get the list of indexy by choices for this folder (localized).
  201. /// </summary>
  202. /// <value>The index by option strings.</value>
  203. [IgnoreDataMember]
  204. public IEnumerable<string> IndexByOptionStrings
  205. {
  206. get { return GetIndexByOptions(); }
  207. }
  208. /// <summary>
  209. /// Gets the actual children.
  210. /// </summary>
  211. /// <value>The actual children.</value>
  212. [IgnoreDataMember]
  213. protected virtual IEnumerable<BaseItem> ActualChildren
  214. {
  215. get
  216. {
  217. return LoadChildren();
  218. }
  219. }
  220. /// <summary>
  221. /// thread-safe access to the actual children of this folder - without regard to user
  222. /// </summary>
  223. /// <value>The children.</value>
  224. [IgnoreDataMember]
  225. public IEnumerable<BaseItem> Children
  226. {
  227. get { return ActualChildren.ToList(); }
  228. }
  229. /// <summary>
  230. /// thread-safe access to all recursive children of this folder - without regard to user
  231. /// </summary>
  232. /// <value>The recursive children.</value>
  233. [IgnoreDataMember]
  234. public IEnumerable<BaseItem> RecursiveChildren
  235. {
  236. get { return GetRecursiveChildren(); }
  237. }
  238. public override bool IsVisible(User user)
  239. {
  240. if (this is ICollectionFolder && !(this is BasePluginFolder))
  241. {
  242. if (user.Policy.BlockedMediaFolders != null)
  243. {
  244. if (user.Policy.BlockedMediaFolders.Contains(Id.ToString("N"), StringComparer.OrdinalIgnoreCase) ||
  245. // Backwards compatibility
  246. user.Policy.BlockedMediaFolders.Contains(Name, StringComparer.OrdinalIgnoreCase))
  247. {
  248. return false;
  249. }
  250. }
  251. else
  252. {
  253. if (!user.Policy.EnableAllFolders && !user.Policy.EnabledFolders.Contains(Id.ToString("N"), StringComparer.OrdinalIgnoreCase))
  254. {
  255. return false;
  256. }
  257. }
  258. }
  259. return base.IsVisible(user);
  260. }
  261. /// <summary>
  262. /// Loads our children. Validation will occur externally.
  263. /// We want this sychronous.
  264. /// </summary>
  265. protected virtual IEnumerable<BaseItem> LoadChildren()
  266. {
  267. //Logger.Debug("Loading children from {0} {1} {2}", GetType().Name, Id, Path);
  268. //just load our children from the repo - the library will be validated and maintained in other processes
  269. return GetCachedChildren();
  270. }
  271. public Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken)
  272. {
  273. return ValidateChildren(progress, cancellationToken, new MetadataRefreshOptions(new DirectoryService(Logger, FileSystem)));
  274. }
  275. /// <summary>
  276. /// Validates that the children of the folder still exist
  277. /// </summary>
  278. /// <param name="progress">The progress.</param>
  279. /// <param name="cancellationToken">The cancellation token.</param>
  280. /// <param name="metadataRefreshOptions">The metadata refresh options.</param>
  281. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  282. /// <returns>Task.</returns>
  283. public Task ValidateChildren(IProgress<double> progress, CancellationToken cancellationToken, MetadataRefreshOptions metadataRefreshOptions, bool recursive = true)
  284. {
  285. return ValidateChildrenInternal(progress, cancellationToken, recursive, true, metadataRefreshOptions, metadataRefreshOptions.DirectoryService);
  286. }
  287. private Dictionary<Guid, BaseItem> GetActualChildrenDictionary()
  288. {
  289. var dictionary = new Dictionary<Guid, BaseItem>();
  290. foreach (var child in ActualChildren)
  291. {
  292. var id = child.Id;
  293. if (dictionary.ContainsKey(id))
  294. {
  295. Logger.Error("Found folder containing items with duplicate id. Path: {0}, Child Name: {1}",
  296. Path ?? Name,
  297. child.Path ?? child.Name);
  298. }
  299. else
  300. {
  301. dictionary[id] = child;
  302. }
  303. }
  304. return dictionary;
  305. }
  306. private bool IsValidFromResolver(BaseItem current, BaseItem newItem)
  307. {
  308. return current.IsValidFromResolver(newItem);
  309. }
  310. /// <summary>
  311. /// Validates the children internal.
  312. /// </summary>
  313. /// <param name="progress">The progress.</param>
  314. /// <param name="cancellationToken">The cancellation token.</param>
  315. /// <param name="recursive">if set to <c>true</c> [recursive].</param>
  316. /// <param name="refreshChildMetadata">if set to <c>true</c> [refresh child metadata].</param>
  317. /// <param name="refreshOptions">The refresh options.</param>
  318. /// <param name="directoryService">The directory service.</param>
  319. /// <returns>Task.</returns>
  320. protected async virtual Task ValidateChildrenInternal(IProgress<double> progress, CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, MetadataRefreshOptions refreshOptions, IDirectoryService directoryService)
  321. {
  322. var locationType = LocationType;
  323. cancellationToken.ThrowIfCancellationRequested();
  324. var validChildren = new List<BaseItem>();
  325. var allLibraryPaths = LibraryManager
  326. .GetVirtualFolders()
  327. .SelectMany(i => i.Locations)
  328. .ToList();
  329. if (locationType != LocationType.Remote && locationType != LocationType.Virtual)
  330. {
  331. IEnumerable<BaseItem> nonCachedChildren;
  332. try
  333. {
  334. nonCachedChildren = GetNonCachedChildren(directoryService);
  335. }
  336. catch (IOException ex)
  337. {
  338. nonCachedChildren = new BaseItem[] { };
  339. Logger.ErrorException("Error getting file system entries for {0}", ex, Path);
  340. }
  341. if (nonCachedChildren == null) return; //nothing to validate
  342. progress.Report(5);
  343. //build a dictionary of the current children we have now by Id so we can compare quickly and easily
  344. var currentChildren = GetActualChildrenDictionary();
  345. //create a list for our validated children
  346. var newItems = new List<BaseItem>();
  347. cancellationToken.ThrowIfCancellationRequested();
  348. foreach (var child in nonCachedChildren)
  349. {
  350. BaseItem currentChild;
  351. if (currentChildren.TryGetValue(child.Id, out currentChild) && IsValidFromResolver(currentChild, child))
  352. {
  353. validChildren.Add(currentChild);
  354. continue;
  355. }
  356. // Brand new item - needs to be added
  357. child.SetParent(this);
  358. newItems.Add(child);
  359. validChildren.Add(child);
  360. }
  361. // If any items were added or removed....
  362. if (newItems.Count > 0 || currentChildren.Count != validChildren.Count)
  363. {
  364. // That's all the new and changed ones - now see if there are any that are missing
  365. var itemsRemoved = currentChildren.Values.Except(validChildren).ToList();
  366. var actualRemovals = new List<BaseItem>();
  367. foreach (var item in itemsRemoved)
  368. {
  369. var itemLocationType = item.LocationType;
  370. if (itemLocationType == LocationType.Virtual ||
  371. itemLocationType == LocationType.Remote)
  372. {
  373. }
  374. else if (!string.IsNullOrEmpty(item.Path) && IsPathOffline(item.Path, allLibraryPaths))
  375. {
  376. }
  377. else
  378. {
  379. actualRemovals.Add(item);
  380. }
  381. }
  382. if (actualRemovals.Count > 0)
  383. {
  384. foreach (var item in actualRemovals)
  385. {
  386. Logger.Debug("Removed item: " + item.Path);
  387. item.SetParent(null);
  388. await LibraryManager.DeleteItem(item, new DeleteOptions { DeleteFileLocation = false }).ConfigureAwait(false);
  389. LibraryManager.ReportItemRemoved(item);
  390. }
  391. }
  392. await LibraryManager.CreateItems(newItems, cancellationToken).ConfigureAwait(false);
  393. }
  394. }
  395. progress.Report(10);
  396. cancellationToken.ThrowIfCancellationRequested();
  397. if (recursive)
  398. {
  399. await ValidateSubFolders(ActualChildren.OfType<Folder>().ToList(), directoryService, progress, cancellationToken).ConfigureAwait(false);
  400. }
  401. progress.Report(20);
  402. if (refreshChildMetadata)
  403. {
  404. var container = this as IMetadataContainer;
  405. var innerProgress = new ActionableProgress<double>();
  406. innerProgress.RegisterAction(p => progress.Report(.80 * p + 20));
  407. if (container != null)
  408. {
  409. await container.RefreshAllMetadata(refreshOptions, innerProgress, cancellationToken).ConfigureAwait(false);
  410. }
  411. else
  412. {
  413. await RefreshMetadataRecursive(refreshOptions, recursive, innerProgress, cancellationToken);
  414. }
  415. }
  416. progress.Report(100);
  417. }
  418. private async Task RefreshMetadataRecursive(MetadataRefreshOptions refreshOptions, bool recursive, IProgress<double> progress, CancellationToken cancellationToken)
  419. {
  420. var children = ActualChildren.ToList();
  421. var percentages = new Dictionary<Guid, double>(children.Count);
  422. var numComplete = 0;
  423. var count = children.Count;
  424. foreach (var child in children)
  425. {
  426. cancellationToken.ThrowIfCancellationRequested();
  427. if (child.IsFolder)
  428. {
  429. var innerProgress = new ActionableProgress<double>();
  430. // Avoid implicitly captured closure
  431. var currentChild = child;
  432. innerProgress.RegisterAction(p =>
  433. {
  434. lock (percentages)
  435. {
  436. percentages[currentChild.Id] = p / 100;
  437. var innerPercent = percentages.Values.Sum();
  438. innerPercent /= count;
  439. innerPercent *= 100;
  440. progress.Report(innerPercent);
  441. }
  442. });
  443. await RefreshChildMetadata(child, refreshOptions, recursive, innerProgress, cancellationToken)
  444. .ConfigureAwait(false);
  445. }
  446. else
  447. {
  448. await RefreshChildMetadata(child, refreshOptions, false, new Progress<double>(), cancellationToken)
  449. .ConfigureAwait(false);
  450. }
  451. numComplete++;
  452. double percent = numComplete;
  453. percent /= count;
  454. percent *= 100;
  455. progress.Report(percent);
  456. }
  457. progress.Report(100);
  458. }
  459. private async Task RefreshChildMetadata(BaseItem child, MetadataRefreshOptions refreshOptions, bool recursive, IProgress<double> progress, CancellationToken cancellationToken)
  460. {
  461. var container = child as IMetadataContainer;
  462. if (container != null)
  463. {
  464. await container.RefreshAllMetadata(refreshOptions, progress, cancellationToken).ConfigureAwait(false);
  465. }
  466. else
  467. {
  468. await child.RefreshMetadata(refreshOptions, cancellationToken).ConfigureAwait(false);
  469. if (recursive)
  470. {
  471. var folder = child as Folder;
  472. if (folder != null)
  473. {
  474. await folder.RefreshMetadataRecursive(refreshOptions, true, progress, cancellationToken);
  475. }
  476. }
  477. }
  478. progress.Report(100);
  479. }
  480. /// <summary>
  481. /// Refreshes the children.
  482. /// </summary>
  483. /// <param name="children">The children.</param>
  484. /// <param name="directoryService">The directory service.</param>
  485. /// <param name="progress">The progress.</param>
  486. /// <param name="cancellationToken">The cancellation token.</param>
  487. /// <returns>Task.</returns>
  488. private async Task ValidateSubFolders(IList<Folder> children, IDirectoryService directoryService, IProgress<double> progress, CancellationToken cancellationToken)
  489. {
  490. var list = children;
  491. var childCount = list.Count;
  492. var percentages = new Dictionary<Guid, double>(list.Count);
  493. foreach (var item in list)
  494. {
  495. cancellationToken.ThrowIfCancellationRequested();
  496. var child = item;
  497. var innerProgress = new ActionableProgress<double>();
  498. innerProgress.RegisterAction(p =>
  499. {
  500. lock (percentages)
  501. {
  502. percentages[child.Id] = p / 100;
  503. var percent = percentages.Values.Sum();
  504. percent /= childCount;
  505. progress.Report(10 * percent + 10);
  506. }
  507. });
  508. await child.ValidateChildrenInternal(innerProgress, cancellationToken, true, false, null, directoryService)
  509. .ConfigureAwait(false);
  510. }
  511. }
  512. /// <summary>
  513. /// Determines whether the specified path is offline.
  514. /// </summary>
  515. /// <param name="path">The path.</param>
  516. /// <returns><c>true</c> if the specified path is offline; otherwise, <c>false</c>.</returns>
  517. public static bool IsPathOffline(string path)
  518. {
  519. return IsPathOffline(path, LibraryManager.GetVirtualFolders().SelectMany(i => i.Locations).ToList());
  520. }
  521. public static bool IsPathOffline(string path, List<string> allLibraryPaths)
  522. {
  523. if (FileSystem.FileExists(path))
  524. {
  525. return false;
  526. }
  527. var originalPath = path;
  528. // Depending on whether the path is local or unc, it may return either null or '\' at the top
  529. while (!string.IsNullOrEmpty(path) && path.Length > 1)
  530. {
  531. if (FileSystem.DirectoryExists(path))
  532. {
  533. return false;
  534. }
  535. if (allLibraryPaths.Contains(path, StringComparer.OrdinalIgnoreCase))
  536. {
  537. return true;
  538. }
  539. path = System.IO.Path.GetDirectoryName(path);
  540. }
  541. return allLibraryPaths.Any(i => ContainsPath(i, originalPath));
  542. }
  543. private static bool ContainsPath(string parent, string path)
  544. {
  545. return FileSystem.AreEqual(parent, path) || FileSystem.ContainsSubPath(parent, path);
  546. }
  547. /// <summary>
  548. /// Get the children of this folder from the actual file system
  549. /// </summary>
  550. /// <returns>IEnumerable{BaseItem}.</returns>
  551. protected virtual IEnumerable<BaseItem> GetNonCachedChildren(IDirectoryService directoryService)
  552. {
  553. var collectionType = LibraryManager.GetContentType(this);
  554. var libraryOptions = LibraryManager.GetLibraryOptions(this);
  555. return LibraryManager.ResolvePaths(GetFileSystemChildren(directoryService), directoryService, this, libraryOptions, collectionType);
  556. }
  557. /// <summary>
  558. /// Get our children from the repo - stubbed for now
  559. /// </summary>
  560. /// <returns>IEnumerable{BaseItem}.</returns>
  561. protected IEnumerable<BaseItem> GetCachedChildren()
  562. {
  563. return ItemRepository.GetItemList(new InternalItemsQuery
  564. {
  565. ParentId = Id,
  566. GroupByPresentationUniqueKey = false
  567. });
  568. }
  569. public virtual int GetChildCount(User user)
  570. {
  571. if (LinkedChildren.Count > 0)
  572. {
  573. if (!(this is ICollectionFolder))
  574. {
  575. return GetChildren(user, true).Count();
  576. }
  577. }
  578. var result = GetItems(new InternalItemsQuery(user)
  579. {
  580. Recursive = false,
  581. Limit = 0,
  582. ParentId = Id
  583. }).Result;
  584. return result.TotalRecordCount;
  585. }
  586. public virtual int GetRecursiveChildCount(User user)
  587. {
  588. return GetItems(new InternalItemsQuery(user)
  589. {
  590. Recursive = true,
  591. IsFolder = false,
  592. IsVirtualItem = false,
  593. EnableTotalRecordCount = true,
  594. Limit = 0
  595. }).Result.TotalRecordCount;
  596. }
  597. public QueryResult<BaseItem> QueryRecursive(InternalItemsQuery query)
  598. {
  599. var user = query.User;
  600. if (!query.ForceDirect && RequiresPostFiltering(query))
  601. {
  602. IEnumerable<BaseItem> items;
  603. Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager);
  604. if (query.User == null)
  605. {
  606. items = GetRecursiveChildren(filter);
  607. }
  608. else
  609. {
  610. items = GetRecursiveChildren(user, query);
  611. }
  612. return PostFilterAndSort(items, query, true, true);
  613. }
  614. if (!(this is UserRootFolder) && !(this is AggregateFolder))
  615. {
  616. query.ParentId = query.ParentId ?? Id;
  617. }
  618. return LibraryManager.GetItemsResult(query);
  619. }
  620. private bool RequiresPostFiltering(InternalItemsQuery query)
  621. {
  622. if (LinkedChildren.Count > 0)
  623. {
  624. if (!(this is ICollectionFolder))
  625. {
  626. Logger.Debug("Query requires post-filtering due to LinkedChildren. Type: " + GetType().Name);
  627. return true;
  628. }
  629. }
  630. if (query.SortBy != null && query.SortBy.Length > 0)
  631. {
  632. if (query.SortBy.Contains(ItemSortBy.AiredEpisodeOrder, StringComparer.OrdinalIgnoreCase))
  633. {
  634. Logger.Debug("Query requires post-filtering due to ItemSortBy.AiredEpisodeOrder");
  635. return true;
  636. }
  637. if (query.SortBy.Contains(ItemSortBy.GameSystem, StringComparer.OrdinalIgnoreCase))
  638. {
  639. Logger.Debug("Query requires post-filtering due to ItemSortBy.GameSystem");
  640. return true;
  641. }
  642. if (query.SortBy.Contains(ItemSortBy.Metascore, StringComparer.OrdinalIgnoreCase))
  643. {
  644. Logger.Debug("Query requires post-filtering due to ItemSortBy.Metascore");
  645. return true;
  646. }
  647. if (query.SortBy.Contains(ItemSortBy.Players, StringComparer.OrdinalIgnoreCase))
  648. {
  649. Logger.Debug("Query requires post-filtering due to ItemSortBy.Players");
  650. return true;
  651. }
  652. if (query.SortBy.Contains(ItemSortBy.VideoBitRate, StringComparer.OrdinalIgnoreCase))
  653. {
  654. Logger.Debug("Query requires post-filtering due to ItemSortBy.VideoBitRate");
  655. return true;
  656. }
  657. }
  658. if (query.ItemIds.Length > 0)
  659. {
  660. Logger.Debug("Query requires post-filtering due to ItemIds");
  661. return true;
  662. }
  663. if (query.IsInBoxSet.HasValue)
  664. {
  665. Logger.Debug("Query requires post-filtering due to IsInBoxSet");
  666. return true;
  667. }
  668. // Filter by Video3DFormat
  669. if (query.Is3D.HasValue)
  670. {
  671. Logger.Debug("Query requires post-filtering due to Is3D");
  672. return true;
  673. }
  674. if (query.HasOfficialRating.HasValue)
  675. {
  676. Logger.Debug("Query requires post-filtering due to HasOfficialRating");
  677. return true;
  678. }
  679. if (query.IsPlaceHolder.HasValue)
  680. {
  681. Logger.Debug("Query requires post-filtering due to IsPlaceHolder");
  682. return true;
  683. }
  684. if (query.HasSpecialFeature.HasValue)
  685. {
  686. Logger.Debug("Query requires post-filtering due to HasSpecialFeature");
  687. return true;
  688. }
  689. if (query.HasSubtitles.HasValue)
  690. {
  691. Logger.Debug("Query requires post-filtering due to HasSubtitles");
  692. return true;
  693. }
  694. if (query.HasTrailer.HasValue)
  695. {
  696. Logger.Debug("Query requires post-filtering due to HasTrailer");
  697. return true;
  698. }
  699. // Filter by VideoType
  700. if (query.VideoTypes.Length > 0)
  701. {
  702. Logger.Debug("Query requires post-filtering due to VideoTypes");
  703. return true;
  704. }
  705. // Apply person filter
  706. if (query.ItemIdsFromPersonFilters != null)
  707. {
  708. Logger.Debug("Query requires post-filtering due to ItemIdsFromPersonFilters");
  709. return true;
  710. }
  711. if (query.MinPlayers.HasValue)
  712. {
  713. Logger.Debug("Query requires post-filtering due to MinPlayers");
  714. return true;
  715. }
  716. if (query.MaxPlayers.HasValue)
  717. {
  718. Logger.Debug("Query requires post-filtering due to MaxPlayers");
  719. return true;
  720. }
  721. if (UserViewBuilder.CollapseBoxSetItems(query, this, query.User, ConfigurationManager))
  722. {
  723. Logger.Debug("Query requires post-filtering due to CollapseBoxSetItems");
  724. return true;
  725. }
  726. if (!string.IsNullOrWhiteSpace(query.AdjacentTo))
  727. {
  728. Logger.Debug("Query requires post-filtering due to AdjacentTo");
  729. return true;
  730. }
  731. if (query.AirDays.Length > 0)
  732. {
  733. Logger.Debug("Query requires post-filtering due to AirDays");
  734. return true;
  735. }
  736. if (query.SeriesStatuses.Length > 0)
  737. {
  738. Logger.Debug("Query requires post-filtering due to SeriesStatuses");
  739. return true;
  740. }
  741. if (query.AiredDuringSeason.HasValue)
  742. {
  743. Logger.Debug("Query requires post-filtering due to AiredDuringSeason");
  744. return true;
  745. }
  746. if (!string.IsNullOrWhiteSpace(query.AlbumArtistStartsWithOrGreater))
  747. {
  748. Logger.Debug("Query requires post-filtering due to AlbumArtistStartsWithOrGreater");
  749. return true;
  750. }
  751. if (query.IsPlayed.HasValue)
  752. {
  753. if (query.IncludeItemTypes.Length == 1 && query.IncludeItemTypes.Contains(typeof(Series).Name))
  754. {
  755. Logger.Debug("Query requires post-filtering due to IsPlayed");
  756. return true;
  757. }
  758. }
  759. return false;
  760. }
  761. public Task<QueryResult<BaseItem>> GetItems(InternalItemsQuery query)
  762. {
  763. if (query.ItemIds.Length > 0)
  764. {
  765. var result = LibraryManager.GetItemsResult(query);
  766. if (query.SortBy.Length == 0)
  767. {
  768. var ids = query.ItemIds.ToList();
  769. // Try to preserve order
  770. result.Items = result.Items.OrderBy(i => ids.IndexOf(i.Id.ToString("N"))).ToArray();
  771. }
  772. return Task.FromResult(result);
  773. }
  774. return GetItemsInternal(query);
  775. }
  776. protected virtual async Task<QueryResult<BaseItem>> GetItemsInternal(InternalItemsQuery query)
  777. {
  778. if (SourceType == SourceType.Channel)
  779. {
  780. try
  781. {
  782. // Don't blow up here because it could cause parent screens with other content to fail
  783. return await ChannelManager.GetChannelItemsInternal(new ChannelItemQuery
  784. {
  785. ChannelId = ChannelId,
  786. FolderId = Id.ToString("N"),
  787. Limit = query.Limit,
  788. StartIndex = query.StartIndex,
  789. UserId = query.User.Id.ToString("N"),
  790. SortBy = query.SortBy,
  791. SortOrder = query.SortOrder
  792. }, new Progress<double>(), CancellationToken.None);
  793. }
  794. catch
  795. {
  796. // Already logged at lower levels
  797. return new QueryResult<BaseItem>();
  798. }
  799. }
  800. if (query.Recursive)
  801. {
  802. return QueryRecursive(query);
  803. }
  804. var user = query.User;
  805. Func<BaseItem, bool> filter = i => UserViewBuilder.Filter(i, user, query, UserDataManager, LibraryManager);
  806. IEnumerable<BaseItem> items;
  807. if (query.User == null)
  808. {
  809. items = query.Recursive
  810. ? GetRecursiveChildren(filter)
  811. : Children.Where(filter);
  812. }
  813. else
  814. {
  815. items = query.Recursive
  816. ? GetRecursiveChildren(user, query)
  817. : GetChildren(user, true).Where(filter);
  818. }
  819. return PostFilterAndSort(items, query, true, true);
  820. }
  821. protected QueryResult<BaseItem> PostFilterAndSort(IEnumerable<BaseItem> items, InternalItemsQuery query, bool collapseBoxSetItems, bool enableSorting)
  822. {
  823. return UserViewBuilder.PostFilterAndSort(items, this, null, query, LibraryManager, ConfigurationManager, collapseBoxSetItems, enableSorting);
  824. }
  825. public virtual IEnumerable<BaseItem> GetChildren(User user, bool includeLinkedChildren)
  826. {
  827. if (user == null)
  828. {
  829. throw new ArgumentNullException();
  830. }
  831. //the true root should return our users root folder children
  832. if (IsPhysicalRoot) return user.RootFolder.GetChildren(user, includeLinkedChildren);
  833. var result = new Dictionary<Guid, BaseItem>();
  834. AddChildren(user, includeLinkedChildren, result, false, null);
  835. return result.Values;
  836. }
  837. protected virtual IEnumerable<BaseItem> GetEligibleChildrenForRecursiveChildren(User user)
  838. {
  839. return Children;
  840. }
  841. /// <summary>
  842. /// Adds the children to list.
  843. /// </summary>
  844. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  845. private void AddChildren(User user, bool includeLinkedChildren, Dictionary<Guid, BaseItem> result, bool recursive, InternalItemsQuery query)
  846. {
  847. foreach (var child in GetEligibleChildrenForRecursiveChildren(user))
  848. {
  849. if (child.IsVisible(user))
  850. {
  851. if (query == null || UserViewBuilder.FilterItem(child, query))
  852. {
  853. result[child.Id] = child;
  854. }
  855. if (recursive && child.IsFolder)
  856. {
  857. var folder = (Folder)child;
  858. folder.AddChildren(user, includeLinkedChildren, result, true, query);
  859. }
  860. }
  861. }
  862. if (includeLinkedChildren)
  863. {
  864. foreach (var child in GetLinkedChildren(user))
  865. {
  866. if (child.IsVisible(user))
  867. {
  868. if (query == null || UserViewBuilder.FilterItem(child, query))
  869. {
  870. result[child.Id] = child;
  871. }
  872. }
  873. }
  874. }
  875. }
  876. /// <summary>
  877. /// Gets allowed recursive children of an item
  878. /// </summary>
  879. /// <param name="user">The user.</param>
  880. /// <param name="includeLinkedChildren">if set to <c>true</c> [include linked children].</param>
  881. /// <returns>IEnumerable{BaseItem}.</returns>
  882. /// <exception cref="System.ArgumentNullException"></exception>
  883. public IEnumerable<BaseItem> GetRecursiveChildren(User user, bool includeLinkedChildren = true)
  884. {
  885. return GetRecursiveChildren(user, null);
  886. }
  887. public virtual IEnumerable<BaseItem> GetRecursiveChildren(User user, InternalItemsQuery query)
  888. {
  889. if (user == null)
  890. {
  891. throw new ArgumentNullException("user");
  892. }
  893. var result = new Dictionary<Guid, BaseItem>();
  894. AddChildren(user, true, result, true, query);
  895. return result.Values;
  896. }
  897. /// <summary>
  898. /// Gets the recursive children.
  899. /// </summary>
  900. /// <returns>IList{BaseItem}.</returns>
  901. public IList<BaseItem> GetRecursiveChildren()
  902. {
  903. return GetRecursiveChildren(true);
  904. }
  905. public IList<BaseItem> GetRecursiveChildren(bool includeLinkedChildren)
  906. {
  907. return GetRecursiveChildren(i => true, includeLinkedChildren);
  908. }
  909. public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter)
  910. {
  911. return GetRecursiveChildren(filter, true);
  912. }
  913. public IList<BaseItem> GetRecursiveChildren(Func<BaseItem, bool> filter, bool includeLinkedChildren)
  914. {
  915. var result = new Dictionary<Guid, BaseItem>();
  916. AddChildrenToList(result, includeLinkedChildren, true, filter);
  917. return result.Values.ToList();
  918. }
  919. /// <summary>
  920. /// Adds the children to list.
  921. /// </summary>
  922. private void AddChildrenToList(Dictionary<Guid, BaseItem> result, bool includeLinkedChildren, bool recursive, Func<BaseItem, bool> filter)
  923. {
  924. foreach (var child in Children)
  925. {
  926. if (filter == null || filter(child))
  927. {
  928. result[child.Id] = child;
  929. }
  930. if (recursive && child.IsFolder)
  931. {
  932. var folder = (Folder)child;
  933. // We can only support includeLinkedChildren for the first folder, or we might end up stuck in a loop of linked items
  934. folder.AddChildrenToList(result, false, true, filter);
  935. }
  936. }
  937. if (includeLinkedChildren)
  938. {
  939. foreach (var child in GetLinkedChildren())
  940. {
  941. if (filter == null || filter(child))
  942. {
  943. result[child.Id] = child;
  944. }
  945. }
  946. }
  947. }
  948. /// <summary>
  949. /// Gets the linked children.
  950. /// </summary>
  951. /// <returns>IEnumerable{BaseItem}.</returns>
  952. public IEnumerable<BaseItem> GetLinkedChildren()
  953. {
  954. return LinkedChildren
  955. .Select(GetLinkedChild)
  956. .Where(i => i != null);
  957. }
  958. protected virtual bool FilterLinkedChildrenPerUser
  959. {
  960. get
  961. {
  962. return false;
  963. }
  964. }
  965. public IEnumerable<BaseItem> GetLinkedChildren(User user)
  966. {
  967. if (!FilterLinkedChildrenPerUser || user == null)
  968. {
  969. return GetLinkedChildren();
  970. }
  971. var locations = user.RootFolder
  972. .Children
  973. .OfType<CollectionFolder>()
  974. .Where(i => i.IsVisible(user))
  975. .SelectMany(i => i.PhysicalLocations)
  976. .ToList();
  977. return LinkedChildren
  978. .Select(i =>
  979. {
  980. var child = GetLinkedChild(i);
  981. if (child != null)
  982. {
  983. var childLocationType = child.LocationType;
  984. if (childLocationType == LocationType.Remote || childLocationType == LocationType.Virtual)
  985. {
  986. if (!child.IsVisibleStandalone(user))
  987. {
  988. return null;
  989. }
  990. }
  991. else if (childLocationType == LocationType.FileSystem && !locations.Any(l => FileSystem.ContainsSubPath(l, child.Path)))
  992. {
  993. return null;
  994. }
  995. }
  996. return child;
  997. })
  998. .Where(i => i != null);
  999. }
  1000. /// <summary>
  1001. /// Gets the linked children.
  1002. /// </summary>
  1003. /// <returns>IEnumerable{BaseItem}.</returns>
  1004. public IEnumerable<Tuple<LinkedChild, BaseItem>> GetLinkedChildrenInfos()
  1005. {
  1006. return LinkedChildren
  1007. .Select(i => new Tuple<LinkedChild, BaseItem>(i, GetLinkedChild(i)))
  1008. .Where(i => i.Item2 != null);
  1009. }
  1010. [IgnoreDataMember]
  1011. protected override bool SupportsOwnedItems
  1012. {
  1013. get
  1014. {
  1015. return base.SupportsOwnedItems || SupportsShortcutChildren;
  1016. }
  1017. }
  1018. protected override async Task<bool> RefreshedOwnedItems(MetadataRefreshOptions options, List<FileSystemMetadata> fileSystemChildren, CancellationToken cancellationToken)
  1019. {
  1020. var changesFound = false;
  1021. if (LocationType == LocationType.FileSystem)
  1022. {
  1023. if (RefreshLinkedChildren(fileSystemChildren))
  1024. {
  1025. changesFound = true;
  1026. }
  1027. }
  1028. var baseHasChanges = await base.RefreshedOwnedItems(options, fileSystemChildren, cancellationToken).ConfigureAwait(false);
  1029. return baseHasChanges || changesFound;
  1030. }
  1031. /// <summary>
  1032. /// Refreshes the linked children.
  1033. /// </summary>
  1034. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  1035. protected virtual bool RefreshLinkedChildren(IEnumerable<FileSystemMetadata> fileSystemChildren)
  1036. {
  1037. var currentManualLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Manual).ToList();
  1038. var currentShortcutLinks = LinkedChildren.Where(i => i.Type == LinkedChildType.Shortcut).ToList();
  1039. List<LinkedChild> newShortcutLinks;
  1040. if (SupportsShortcutChildren)
  1041. {
  1042. newShortcutLinks = fileSystemChildren
  1043. .Where(i => !i.IsDirectory && FileSystem.IsShortcut(i.FullName))
  1044. .Select(i =>
  1045. {
  1046. try
  1047. {
  1048. Logger.Debug("Found shortcut at {0}", i.FullName);
  1049. var resolvedPath = FileSystem.ResolveShortcut(i.FullName);
  1050. if (!string.IsNullOrEmpty(resolvedPath))
  1051. {
  1052. return new LinkedChild
  1053. {
  1054. Path = resolvedPath,
  1055. Type = LinkedChildType.Shortcut
  1056. };
  1057. }
  1058. Logger.Error("Error resolving shortcut {0}", i.FullName);
  1059. return null;
  1060. }
  1061. catch (IOException ex)
  1062. {
  1063. Logger.ErrorException("Error resolving shortcut {0}", ex, i.FullName);
  1064. return null;
  1065. }
  1066. })
  1067. .Where(i => i != null)
  1068. .ToList();
  1069. }
  1070. else { newShortcutLinks = new List<LinkedChild>(); }
  1071. if (!newShortcutLinks.SequenceEqual(currentShortcutLinks, new LinkedChildComparer()))
  1072. {
  1073. Logger.Info("Shortcut links have changed for {0}", Path);
  1074. newShortcutLinks.AddRange(currentManualLinks);
  1075. LinkedChildren = newShortcutLinks;
  1076. return true;
  1077. }
  1078. foreach (var child in LinkedChildren)
  1079. {
  1080. // Reset the cached value
  1081. child.ItemId = null;
  1082. }
  1083. return false;
  1084. }
  1085. /// <summary>
  1086. /// Folders need to validate and refresh
  1087. /// </summary>
  1088. /// <returns>Task.</returns>
  1089. public override async Task ChangedExternally()
  1090. {
  1091. var progress = new Progress<double>();
  1092. await ValidateChildren(progress, CancellationToken.None).ConfigureAwait(false);
  1093. await base.ChangedExternally().ConfigureAwait(false);
  1094. }
  1095. /// <summary>
  1096. /// Marks the played.
  1097. /// </summary>
  1098. /// <param name="user">The user.</param>
  1099. /// <param name="datePlayed">The date played.</param>
  1100. /// <param name="resetPosition">if set to <c>true</c> [reset position].</param>
  1101. /// <returns>Task.</returns>
  1102. public override async Task MarkPlayed(User user,
  1103. DateTime? datePlayed,
  1104. bool resetPosition)
  1105. {
  1106. var query = new InternalItemsQuery
  1107. {
  1108. User = user,
  1109. Recursive = true,
  1110. IsFolder = false,
  1111. EnableTotalRecordCount = false
  1112. };
  1113. if (!user.Configuration.DisplayMissingEpisodes || !user.Configuration.DisplayUnairedEpisodes)
  1114. {
  1115. query.IsVirtualItem = false;
  1116. }
  1117. var itemsResult = await GetItems(query).ConfigureAwait(false);
  1118. // Sweep through recursively and update status
  1119. var tasks = itemsResult.Items.Select(c => c.MarkPlayed(user, datePlayed, resetPosition));
  1120. await Task.WhenAll(tasks).ConfigureAwait(false);
  1121. }
  1122. /// <summary>
  1123. /// Marks the unplayed.
  1124. /// </summary>
  1125. /// <param name="user">The user.</param>
  1126. /// <returns>Task.</returns>
  1127. public override async Task MarkUnplayed(User user)
  1128. {
  1129. var itemsResult = await GetItems(new InternalItemsQuery
  1130. {
  1131. User = user,
  1132. Recursive = true,
  1133. IsFolder = false,
  1134. EnableTotalRecordCount = false
  1135. }).ConfigureAwait(false);
  1136. // Sweep through recursively and update status
  1137. var tasks = itemsResult.Items.Select(c => c.MarkUnplayed(user));
  1138. await Task.WhenAll(tasks).ConfigureAwait(false);
  1139. }
  1140. public override bool IsPlayed(User user)
  1141. {
  1142. var itemsResult = GetItems(new InternalItemsQuery(user)
  1143. {
  1144. Recursive = true,
  1145. IsFolder = false,
  1146. IsVirtualItem = false,
  1147. EnableTotalRecordCount = false
  1148. }).Result;
  1149. return itemsResult.Items
  1150. .All(i => i.IsPlayed(user));
  1151. }
  1152. public override bool IsUnplayed(User user)
  1153. {
  1154. return !IsPlayed(user);
  1155. }
  1156. [IgnoreDataMember]
  1157. public virtual bool SupportsUserDataFromChildren
  1158. {
  1159. get
  1160. {
  1161. // These are just far too slow.
  1162. if (this is ICollectionFolder)
  1163. {
  1164. return false;
  1165. }
  1166. if (this is UserView)
  1167. {
  1168. return false;
  1169. }
  1170. if (this is UserRootFolder)
  1171. {
  1172. return false;
  1173. }
  1174. if (this is Channel)
  1175. {
  1176. return false;
  1177. }
  1178. if (SourceType != SourceType.Library)
  1179. {
  1180. return false;
  1181. }
  1182. var iItemByName = this as IItemByName;
  1183. if (iItemByName != null)
  1184. {
  1185. var hasDualAccess = this as IHasDualAccess;
  1186. if (hasDualAccess == null || hasDualAccess.IsAccessedByName)
  1187. {
  1188. return false;
  1189. }
  1190. }
  1191. return true;
  1192. }
  1193. }
  1194. public override async Task FillUserDataDtoValues(UserItemDataDto dto, UserItemData userData, BaseItemDto itemDto, User user, List<ItemFields> itemFields)
  1195. {
  1196. if (!SupportsUserDataFromChildren)
  1197. {
  1198. return;
  1199. }
  1200. if (itemDto != null)
  1201. {
  1202. if (itemFields.Contains(ItemFields.RecursiveItemCount))
  1203. {
  1204. itemDto.RecursiveItemCount = GetRecursiveChildCount(user);
  1205. }
  1206. }
  1207. if (SupportsPlayedStatus)
  1208. {
  1209. var unplayedQueryResult = await GetItems(new InternalItemsQuery(user)
  1210. {
  1211. Recursive = true,
  1212. IsFolder = false,
  1213. IsVirtualItem = false,
  1214. EnableTotalRecordCount = true,
  1215. Limit = 0,
  1216. IsPlayed = false
  1217. }).ConfigureAwait(false);
  1218. double unplayedCount = unplayedQueryResult.TotalRecordCount;
  1219. dto.UnplayedItemCount = unplayedQueryResult.TotalRecordCount;
  1220. if (itemDto != null && itemDto.RecursiveItemCount.HasValue)
  1221. {
  1222. if (itemDto.RecursiveItemCount.Value > 0)
  1223. {
  1224. var unplayedPercentage = (unplayedCount/itemDto.RecursiveItemCount.Value)*100;
  1225. dto.PlayedPercentage = 100 - unplayedPercentage;
  1226. dto.Played = dto.PlayedPercentage.Value >= 100;
  1227. }
  1228. }
  1229. else
  1230. {
  1231. dto.Played = (dto.UnplayedItemCount ?? 0) == 0;
  1232. }
  1233. }
  1234. }
  1235. }
  1236. }