support recursive played filter

This commit is contained in:
Luke Pulverenti 2014-01-15 17:19:37 -05:00
parent 98b6046f04
commit d0a5197c0a
5 changed files with 22 additions and 40 deletions

View File

@ -137,24 +137,14 @@ namespace MediaBrowser.Api.UserLibrary
{
var libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList();
items = items.Where(i => GetLibraryItems(i, libraryItems).All(l =>
{
var userdata = UserDataRepository.GetUserData(user.Id, l.GetUserDataKey());
return userdata != null && userdata.Played;
}));
items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => l.IsPlayed(user)));
}
if (filters.Contains(ItemFilter.IsUnplayed))
{
var libraryItems = user.RootFolder.GetRecursiveChildren(user).ToList();
items = items.Where(i => GetLibraryItems(i, libraryItems).All(l =>
{
var userdata = UserDataRepository.GetUserData(user.Id, l.GetUserDataKey());
return userdata == null || !userdata.Played;
}));
items = items.Where(i => GetLibraryItems(i, libraryItems).All(l => !l.IsPlayed(user)));
}
return items;

View File

@ -386,6 +386,9 @@ namespace MediaBrowser.Api.UserLibrary
/// <returns>IEnumerable{BaseItem}.</returns>
internal static IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user, IUserDataManager repository)
{
// Avoid implicitly captured closure
var currentUser = user;
switch (filter)
{
case ItemFilter.IsFavoriteOrLikes:
@ -440,20 +443,10 @@ namespace MediaBrowser.Api.UserLibrary
});
case ItemFilter.IsPlayed:
return items.Where(item =>
{
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
return userdata != null && userdata.Played;
});
return items.Where(item => item.IsPlayed(currentUser));
case ItemFilter.IsUnplayed:
return items.Where(item =>
{
var userdata = repository.GetUserData(user.Id, item.GetUserDataKey());
return userdata == null || !userdata.Played;
});
return items.Where(item => !item.IsPlayed(currentUser));
case ItemFilter.IsFolder:
return items.Where(item => item.IsFolder);

View File

@ -167,6 +167,7 @@ namespace MediaBrowser.Controller.Entities
public static ILocalizationManager LocalizationManager { get; set; }
public static IItemRepository ItemRepository { get; set; }
public static IFileSystem FileSystem { get; set; }
public static IUserDataManager UserDataManager { get; set; }
/// <summary>
/// Returns a <see cref="System.String" /> that represents this instance.
@ -1705,5 +1706,12 @@ namespace MediaBrowser.Controller.Entities
// Directory watchers should repeat this, but do a quick refresh first
return RefreshMetadata(CancellationToken.None, forceSave: true, allowSlowProviders: false);
}
public virtual bool IsPlayed(User user)
{
var userdata = UserDataManager.GetUserData(user.Id, GetUserDataKey());
return userdata != null && userdata.Played;
}
}
}

View File

@ -1084,5 +1084,10 @@ namespace MediaBrowser.Controller.Entities
}
});
}
public override bool IsPlayed(User user)
{
return GetRecursiveChildren(user).Where(i => !i.IsFolder).All(i => i.IsPlayed(user));
}
}
}

View File

@ -32,14 +32,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>DateTime.</returns>
private int GetValue(BaseItem x)
{
var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey());
if (userdata == null)
{
return 0;
}
return userdata.Played ? 1 : 0;
return x.IsPlayed(User) ? 1 : 0;
}
/// <summary>
@ -90,14 +83,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
/// <returns>DateTime.</returns>
private int GetValue(BaseItem x)
{
var userdata = UserDataRepository.GetUserData(User.Id, x.GetUserDataKey());
if (userdata == null)
{
return 1;
}
return userdata.Played ? 0 : 1;
return x.IsPlayed(User) ? 0 : 1;
}
/// <summary>