jellyfin/Emby.Server.Implementations/TV/TVSeriesManager.cs

225 lines
8.0 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.TV;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using System;
using System.Collections.Generic;
using System.Linq;
2016-07-01 15:51:35 +00:00
using MediaBrowser.Controller.Configuration;
2016-11-03 07:14:14 +00:00
namespace Emby.Server.Implementations.TV
{
public class TVSeriesManager : ITVSeriesManager
{
private readonly IUserManager _userManager;
private readonly IUserDataManager _userDataManager;
private readonly ILibraryManager _libraryManager;
2016-07-01 15:51:35 +00:00
private readonly IServerConfigurationManager _config;
2016-07-01 15:51:35 +00:00
public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager config)
{
_userManager = userManager;
_userDataManager = userDataManager;
_libraryManager = libraryManager;
2016-07-01 15:51:35 +00:00
_config = config;
}
public QueryResult<BaseItem> GetNextUp(NextUpQuery request)
{
2014-09-14 15:10:51 +00:00
var user = _userManager.GetUserById(request.UserId);
if (user == null)
{
throw new ArgumentException("User not found");
}
2016-06-30 19:10:12 +00:00
var parentIdGuid = string.IsNullOrWhiteSpace(request.ParentId) ? (Guid?)null : new Guid(request.ParentId);
2016-05-02 05:32:04 +00:00
string presentationUniqueKey = null;
int? limit = null;
if (!string.IsNullOrWhiteSpace(request.SeriesId))
{
var series = _libraryManager.GetItemById(request.SeriesId);
if (series != null)
{
2016-07-01 15:51:35 +00:00
presentationUniqueKey = GetUniqueSeriesKey(series);
2016-05-02 05:32:04 +00:00
limit = 1;
}
}
2016-07-05 06:09:11 +00:00
if (string.IsNullOrWhiteSpace(presentationUniqueKey) && limit.HasValue)
{
limit = limit.Value + 10;
}
2016-03-20 06:46:51 +00:00
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
2015-10-28 19:40:38 +00:00
{
2015-10-29 13:28:05 +00:00
IncludeItemTypes = new[] { typeof(Series).Name },
2016-05-02 05:32:04 +00:00
SortOrder = SortOrder.Ascending,
PresentationUniqueKey = presentationUniqueKey,
2016-06-30 19:10:12 +00:00
Limit = limit,
2016-06-30 19:13:14 +00:00
ParentId = parentIdGuid,
Recursive = true
2015-10-28 19:40:38 +00:00
2016-06-30 19:10:12 +00:00
}).Cast<Series>();
// Avoid implicitly captured closure
var episodes = GetNextUpEpisodes(request, user, items);
return GetResult(episodes, null, request);
}
2016-11-21 08:54:53 +00:00
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, List<Folder> parentsFolders)
{
2014-09-14 15:10:51 +00:00
var user = _userManager.GetUserById(request.UserId);
if (user == null)
{
throw new ArgumentException("User not found");
}
2016-05-02 05:32:04 +00:00
string presentationUniqueKey = null;
int? limit = null;
if (!string.IsNullOrWhiteSpace(request.SeriesId))
{
var series = _libraryManager.GetItemById(request.SeriesId);
if (series != null)
{
2016-07-01 15:51:35 +00:00
presentationUniqueKey = GetUniqueSeriesKey(series);
2016-05-02 05:32:04 +00:00
limit = 1;
}
}
2016-07-05 06:09:11 +00:00
if (string.IsNullOrWhiteSpace(presentationUniqueKey) && limit.HasValue)
{
limit = limit.Value + 10;
}
2016-03-20 06:46:51 +00:00
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
2015-10-29 13:28:05 +00:00
{
IncludeItemTypes = new[] { typeof(Series).Name },
2016-05-02 05:32:04 +00:00
SortOrder = SortOrder.Ascending,
PresentationUniqueKey = presentationUniqueKey,
Limit = limit
2015-10-29 13:28:05 +00:00
2016-11-21 08:54:53 +00:00
}, parentsFolders.Cast<BaseItem>().ToList()).Cast<Series>();
// Avoid implicitly captured closure
var episodes = GetNextUpEpisodes(request, user, items);
return GetResult(episodes, null, request);
}
public IEnumerable<Episode> GetNextUpEpisodes(NextUpQuery request, User user, IEnumerable<Series> series)
{
// Avoid implicitly captured closure
var currentUser = user;
2016-07-23 20:27:22 +00:00
var allNextUp = series
.Select(i => GetNextUp(i, currentUser))
// Include if an episode was found, and either the series is not unwatched or the specific series was requested
2016-11-21 17:17:26 +00:00
.OrderByDescending(i => i.Item1)
2016-07-23 20:27:22 +00:00
.ToList();
// If viewing all next up for all series, remove first episodes
if (string.IsNullOrWhiteSpace(request.SeriesId))
{
var withoutFirstEpisode = allNextUp
2016-11-21 17:17:26 +00:00
.Where(i => i.Item1 != DateTime.MinValue)
2016-07-23 20:27:22 +00:00
.ToList();
// But if that returns empty, keep those first episodes (avoid completely empty view)
if (withoutFirstEpisode.Count > 0)
{
allNextUp = withoutFirstEpisode;
}
}
return allNextUp
2016-11-21 17:17:26 +00:00
.Select(i => i.Item2())
.Where(i => i != null)
2016-07-05 06:09:11 +00:00
.Take(request.Limit ?? int.MaxValue);
}
2016-07-01 15:51:35 +00:00
private string GetUniqueSeriesKey(BaseItem series)
{
return series.GetPresentationUniqueKey();
2016-07-01 15:51:35 +00:00
}
/// <summary>
/// Gets the next up.
/// </summary>
/// <param name="series">The series.</param>
/// <param name="user">The user.</param>
/// <returns>Task{Episode}.</returns>
2016-11-21 17:17:26 +00:00
private Tuple<DateTime, Func<Episode>> GetNextUp(Series series, User user)
{
2016-06-29 16:31:01 +00:00
var lastWatchedEpisode = _libraryManager.GetItemList(new InternalItemsQuery(user)
{
2016-07-01 15:51:35 +00:00
AncestorWithPresentationUniqueKey = GetUniqueSeriesKey(series),
2016-06-14 19:21:26 +00:00
IncludeItemTypes = new[] { typeof(Episode).Name },
SortBy = new[] { ItemSortBy.SortName },
2016-06-29 16:31:01 +00:00
SortOrder = SortOrder.Descending,
IsPlayed = true,
2016-06-14 19:21:26 +00:00
Limit = 1,
2016-06-15 02:18:19 +00:00
ParentIndexNumberNotEquals = 0
2016-06-04 00:15:14 +00:00
2016-06-29 16:31:01 +00:00
}).FirstOrDefault();
2016-11-21 17:17:26 +00:00
Func<Episode> getEpisode = () =>
2016-06-04 00:15:14 +00:00
{
2016-11-21 17:17:26 +00:00
return _libraryManager.GetItemList(new InternalItemsQuery(user)
{
AncestorWithPresentationUniqueKey = GetUniqueSeriesKey(series),
IncludeItemTypes = new[] { typeof(Episode).Name },
SortBy = new[] { ItemSortBy.SortName },
SortOrder = SortOrder.Ascending,
Limit = 1,
IsPlayed = false,
IsVirtualItem = false,
ParentIndexNumberNotEquals = 0,
MinSortName = lastWatchedEpisode == null ? null : lastWatchedEpisode.SortName
}).Cast<Episode>().FirstOrDefault();
};
2016-06-14 19:21:26 +00:00
2016-11-21 17:17:26 +00:00
if (lastWatchedEpisode != null)
2016-06-14 19:21:26 +00:00
{
var userData = _userDataManager.GetUserData(user, lastWatchedEpisode);
2016-06-04 00:15:14 +00:00
2016-07-01 15:51:35 +00:00
var lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
2016-11-21 17:17:26 +00:00
return new Tuple<DateTime, Func<Episode>>(lastWatchedDate, getEpisode);
2016-06-04 00:15:14 +00:00
}
2015-01-23 06:15:15 +00:00
// Return the first episode
2016-11-21 17:17:26 +00:00
return new Tuple<DateTime, Func<Episode>>(DateTime.MinValue, getEpisode);
}
private QueryResult<BaseItem> GetResult(IEnumerable<BaseItem> items, int? totalRecordLimit, NextUpQuery query)
{
var itemsArray = totalRecordLimit.HasValue ? items.Take(totalRecordLimit.Value).ToArray() : items.ToArray();
var totalCount = itemsArray.Length;
if (query.Limit.HasValue)
{
itemsArray = itemsArray.Skip(query.StartIndex ?? 0).Take(query.Limit.Value).ToArray();
}
else if (query.StartIndex.HasValue)
{
itemsArray = itemsArray.Skip(query.StartIndex.Value).ToArray();
}
return new QueryResult<BaseItem>
{
TotalRecordCount = totalCount,
Items = itemsArray
};
}
}
}