2014-09-01 13:10:54 -07:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.TV
|
|
|
|
|
{
|
|
|
|
|
public class TVSeriesManager : ITVSeriesManager
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager)
|
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QueryResult<BaseItem> GetNextUp(NextUpQuery request)
|
|
|
|
|
{
|
2014-09-14 08:10:51 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parentIds = string.IsNullOrEmpty(request.ParentId)
|
|
|
|
|
? new string[] { }
|
|
|
|
|
: new[] { request.ParentId };
|
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
string presentationUniqueKey = null;
|
|
|
|
|
int? limit = null;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SeriesId))
|
|
|
|
|
{
|
|
|
|
|
var series = _libraryManager.GetItemById(request.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (series != null)
|
|
|
|
|
{
|
|
|
|
|
presentationUniqueKey = series.PresentationUniqueKey;
|
|
|
|
|
limit = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 23:46:51 -07:00
|
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2015-10-28 12:40:38 -07:00
|
|
|
|
{
|
2015-10-29 06:28:05 -07:00
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-05-01 22:32:04 -07:00
|
|
|
|
SortOrder = SortOrder.Ascending,
|
|
|
|
|
PresentationUniqueKey = presentationUniqueKey,
|
|
|
|
|
Limit = limit
|
2015-10-28 12:40:38 -07:00
|
|
|
|
|
2015-11-11 07:56:31 -07:00
|
|
|
|
}, parentIds).Cast<Series>();
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var episodes = GetNextUpEpisodes(request, user, items);
|
|
|
|
|
|
|
|
|
|
return GetResult(episodes, null, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, IEnumerable<Folder> parentsFolders)
|
|
|
|
|
{
|
2014-09-14 08:10:51 -07:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
string presentationUniqueKey = null;
|
|
|
|
|
int? limit = null;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SeriesId))
|
|
|
|
|
{
|
|
|
|
|
var series = _libraryManager.GetItemById(request.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (series != null)
|
|
|
|
|
{
|
|
|
|
|
presentationUniqueKey = series.PresentationUniqueKey;
|
|
|
|
|
limit = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 23:46:51 -07:00
|
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2015-10-29 06:28:05 -07:00
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-05-01 22:32:04 -07:00
|
|
|
|
SortOrder = SortOrder.Ascending,
|
|
|
|
|
PresentationUniqueKey = presentationUniqueKey,
|
|
|
|
|
Limit = limit
|
2015-10-29 06:28:05 -07:00
|
|
|
|
|
2015-11-11 07:56:31 -07:00
|
|
|
|
}, parentsFolders.Select(i => i.Id.ToString("N"))).Cast<Series>();
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
|
|
|
|
// 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-05-01 22:32:04 -07:00
|
|
|
|
return series
|
2014-09-01 13:10:54 -07:00
|
|
|
|
.AsParallel()
|
|
|
|
|
.Select(i => GetNextUp(i, currentUser))
|
2015-01-26 09:47:15 -07:00
|
|
|
|
// Include if an episode was found, and either the series is not unwatched or the specific series was requested
|
|
|
|
|
.Where(i => i.Item1 != null && (!i.Item3 || !string.IsNullOrWhiteSpace(request.SeriesId)))
|
2016-05-01 22:32:04 -07:00
|
|
|
|
//.OrderByDescending(i =>
|
|
|
|
|
//{
|
|
|
|
|
// var episode = i.Item1;
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
// var seriesUserData = _userDataManager.GetUserData(user, episode.Series);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
// if (seriesUserData.IsFavorite)
|
|
|
|
|
// {
|
|
|
|
|
// return 2;
|
|
|
|
|
// }
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
// if (seriesUserData.Likes.HasValue)
|
|
|
|
|
// {
|
|
|
|
|
// return seriesUserData.Likes.Value ? 1 : -1;
|
|
|
|
|
// }
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
// return 0;
|
|
|
|
|
//})
|
|
|
|
|
.OrderByDescending(i => i.Item2)
|
2014-09-01 13:10:54 -07:00
|
|
|
|
.ThenByDescending(i => i.Item1.PremiereDate ?? DateTime.MinValue)
|
|
|
|
|
.Select(i => i.Item1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the next up.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="series">The series.</param>
|
|
|
|
|
/// <param name="user">The user.</param>
|
|
|
|
|
/// <returns>Task{Episode}.</returns>
|
2015-01-26 09:47:15 -07:00
|
|
|
|
private Tuple<Episode, DateTime, bool> GetNextUp(Series series, User user)
|
2014-09-01 13:10:54 -07:00
|
|
|
|
{
|
|
|
|
|
// Get them in display order, then reverse
|
|
|
|
|
var allEpisodes = series.GetSeasons(user, true, true)
|
2015-01-24 23:34:50 -07:00
|
|
|
|
.Where(i => !i.IndexNumber.HasValue || i.IndexNumber.Value != 0)
|
2015-02-04 04:56:48 -07:00
|
|
|
|
.SelectMany(i => i.GetEpisodes(user))
|
2014-09-01 13:10:54 -07:00
|
|
|
|
.Reverse()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
Episode lastWatched = null;
|
|
|
|
|
var lastWatchedDate = DateTime.MinValue;
|
|
|
|
|
Episode nextUp = null;
|
|
|
|
|
|
2015-08-09 09:35:27 -07:00
|
|
|
|
var includeMissing = user.Configuration.DisplayMissingEpisodes;
|
|
|
|
|
|
2014-09-01 13:10:54 -07:00
|
|
|
|
// Go back starting with the most recent episodes
|
|
|
|
|
foreach (var episode in allEpisodes)
|
|
|
|
|
{
|
2016-04-30 15:05:13 -07:00
|
|
|
|
var userData = _userDataManager.GetUserData(user, episode);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
|
|
|
|
|
if (userData.Played)
|
|
|
|
|
{
|
|
|
|
|
if (lastWatched != null || nextUp == null)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastWatched = episode;
|
|
|
|
|
lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-01 22:32:04 -07:00
|
|
|
|
if (!episode.IsVirtualUnaired && (includeMissing || !episode.IsMissingEpisode))
|
2014-09-01 13:10:54 -07:00
|
|
|
|
{
|
|
|
|
|
nextUp = episode;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastWatched != null)
|
|
|
|
|
{
|
2015-01-26 09:47:15 -07:00
|
|
|
|
return new Tuple<Episode, DateTime, bool>(nextUp, lastWatchedDate, false);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 22:32:04 -07:00
|
|
|
|
var firstEpisode = allEpisodes.LastOrDefault(i => !i.IsVirtualUnaired && (includeMissing || !i.IsMissingEpisode) && !i.IsPlayed(user));
|
2015-01-22 23:15:15 -07:00
|
|
|
|
|
|
|
|
|
// Return the first episode
|
2015-01-26 09:47:15 -07:00
|
|
|
|
return new Tuple<Episode, DateTime, bool>(firstEpisode, DateTime.MinValue, true);
|
2014-09-01 13:10:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|