From ed018a8bea89f4a19dbfca2e21eb7207df2db332 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Fri, 17 Aug 2012 13:37:26 -0400 Subject: Moved some logic into domain entities, which is possible now that we're embracing DTO's --- MediaBrowser.Model/Entities/BaseItem.cs | 9 +++ MediaBrowser.Model/Entities/Folder.cs | 121 ++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) (limited to 'MediaBrowser.Model/Entities') diff --git a/MediaBrowser.Model/Entities/BaseItem.cs b/MediaBrowser.Model/Entities/BaseItem.cs index d2745e460..c9d5b936b 100644 --- a/MediaBrowser.Model/Entities/BaseItem.cs +++ b/MediaBrowser.Model/Entities/BaseItem.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Runtime.Serialization; +using MediaBrowser.Model.Users; namespace MediaBrowser.Model.Entities { @@ -101,5 +102,13 @@ namespace MediaBrowser.Model.Entities { SetProviderId(provider.ToString(), value); } + + /// + /// Determines if a given user has access to this item + /// + internal bool IsParentalAllowed(User user) + { + return true; + } } } diff --git a/MediaBrowser.Model/Entities/Folder.cs b/MediaBrowser.Model/Entities/Folder.cs index 293d7a990..98f1d8229 100644 --- a/MediaBrowser.Model/Entities/Folder.cs +++ b/MediaBrowser.Model/Entities/Folder.cs @@ -1,5 +1,8 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Runtime.Serialization; +using MediaBrowser.Model.Users; namespace MediaBrowser.Model.Entities { @@ -18,6 +21,124 @@ namespace MediaBrowser.Model.Entities [IgnoreDataMember] public BaseItem[] Children { get; set; } + /// + /// Gets allowed children of an item + /// + public IEnumerable GetParentalAllowedChildren(User user) + { + return Children.Where(c => c.IsParentalAllowed(user)); + } + + /// + /// Gets allowed recursive children of an item + /// + public IEnumerable GetParentalAllowedRecursiveChildren(User user) + { + foreach (var item in GetParentalAllowedChildren(user)) + { + yield return item; + + var subFolder = item as Folder; + + if (subFolder != null) + { + foreach (var subitem in subFolder.GetParentalAllowedRecursiveChildren(user)) + { + yield return subitem; + } + } + } + } + + /// + /// Finds all recursive items within a top-level parent that contain the given genre and are allowed for the current user + /// + public IEnumerable GetItemsWithGenre(string genre, User user) + { + return GetParentalAllowedRecursiveChildren(user).Where(f => f.Genres != null && f.Genres.Any(s => s.Equals(genre, StringComparison.OrdinalIgnoreCase))); + } + + /// + /// Finds all recursive items within a top-level parent that contain the given year and are allowed for the current user + /// + public IEnumerable GetItemsWithYear(int year, User user) + { + return GetParentalAllowedRecursiveChildren(user).Where(f => f.ProductionYear.HasValue && f.ProductionYear == year); + } + + /// + /// Finds all recursive items within a top-level parent that contain the given studio and are allowed for the current user + /// + public IEnumerable GetItemsWithStudio(string studio, User user) + { + return GetParentalAllowedRecursiveChildren(user).Where(f => f.Studios != null && f.Studios.Any(s => s.Equals(studio, StringComparison.OrdinalIgnoreCase))); + } + + /// + /// Finds all recursive items within a top-level parent that contain the given person and are allowed for the current user + /// + /// Specify this to limit results to a specific PersonType + public IEnumerable GetItemsWithPerson(string person, PersonType? personType, User user) + { + return GetParentalAllowedRecursiveChildren(user).Where(c => + { + if (c.People != null) + { + if (personType.HasValue) + { + return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase) && p.PersonType == personType.Value); + } + else + { + return c.People.Any(p => p.Name.Equals(person, StringComparison.OrdinalIgnoreCase)); + } + } + + return false; + }); + } + + /// + /// Gets all recently added items (recursive) within a folder, based on configuration and parental settings + /// + public IEnumerable GetRecentlyAddedItems(User user) + { + DateTime now = DateTime.Now; + + return GetParentalAllowedRecursiveChildren(user).Where(i => !(i is Folder) && (now - i.DateCreated).TotalDays < user.RecentItemDays); + } + + /// + /// Gets all recently added unplayed items (recursive) within a folder, based on configuration and parental settings + /// + public IEnumerable GetRecentlyAddedUnplayedItems(User user) + { + return GetRecentlyAddedItems(user).Where(i => + { + var userdata = user.GetItemData(i.Id); + + return userdata == null || userdata.PlayCount == 0; + }); + } + + /// + /// Gets all in-progress items (recursive) within a folder + /// + public IEnumerable GetInProgressItems(User user) + { + return GetParentalAllowedRecursiveChildren(user).Where(i => + { + if (i is Folder) + { + return false; + } + + var userdata = user.GetItemData(i.Id); + + return userdata != null && userdata.PlaybackPosition.Ticks > 0; + }); + } + /// /// Finds an item by ID, recursively /// -- cgit v1.2.3