2014-09-01 16:10:54 -04: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;
|
2016-07-01 11:51:35 -04:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2017-05-21 03:25:49 -04:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2016-11-03 03:14:14 -04:00
|
|
|
|
namespace Emby.Server.Implementations.TV
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
|
|
|
|
public class TVSeriesManager : ITVSeriesManager
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
2016-07-01 11:51:35 -04:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2016-07-01 11:51:35 -04:00
|
|
|
|
public TVSeriesManager(IUserManager userManager, IUserDataManager userDataManager, ILibraryManager libraryManager, IServerConfigurationManager config)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_userDataManager = userDataManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
2016-07-01 11:51:35 -04:00
|
|
|
|
_config = config;
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 03:25:49 -04:00
|
|
|
|
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, DtoOptions dtoOptions)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2014-09-14 11:10:51 -04:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 15:10:12 -04:00
|
|
|
|
var parentIdGuid = string.IsNullOrWhiteSpace(request.ParentId) ? (Guid?)null : new Guid(request.ParentId);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2016-05-02 01:32:04 -04:00
|
|
|
|
string presentationUniqueKey = null;
|
|
|
|
|
int? limit = null;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SeriesId))
|
|
|
|
|
{
|
|
|
|
|
var series = _libraryManager.GetItemById(request.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (series != null)
|
|
|
|
|
{
|
2016-07-01 11:51:35 -04:00
|
|
|
|
presentationUniqueKey = GetUniqueSeriesKey(series);
|
2016-05-02 01:32:04 -04:00
|
|
|
|
limit = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 02:09:11 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(presentationUniqueKey) && limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
limit = limit.Value + 10;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 02:46:51 -04:00
|
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2015-10-28 15:40:38 -04:00
|
|
|
|
{
|
2015-10-29 09:28:05 -04:00
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-12-12 03:53:25 -05:00
|
|
|
|
SortBy = new[] { ItemSortBy.SeriesDatePlayed },
|
|
|
|
|
SortOrder = SortOrder.Descending,
|
2016-05-02 01:32:04 -04:00
|
|
|
|
PresentationUniqueKey = presentationUniqueKey,
|
2016-06-30 15:10:12 -04:00
|
|
|
|
Limit = limit,
|
2016-06-30 15:13:14 -04:00
|
|
|
|
ParentId = parentIdGuid,
|
2016-12-12 00:49:19 -05:00
|
|
|
|
Recursive = true,
|
|
|
|
|
DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions
|
|
|
|
|
{
|
|
|
|
|
Fields = new List<ItemFields>
|
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
ItemFields.PresentationUniqueKey
|
2016-12-12 00:49:19 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-10-28 15:40:38 -04:00
|
|
|
|
|
2016-12-12 03:53:25 -05:00
|
|
|
|
}).Cast<Series>().Select(GetUniqueSeriesKey);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
2017-05-21 03:25:49 -04:00
|
|
|
|
var episodes = GetNextUpEpisodes(request, user, items, dtoOptions);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2017-03-21 13:31:40 -04:00
|
|
|
|
return GetResult(episodes, request);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 03:25:49 -04:00
|
|
|
|
public QueryResult<BaseItem> GetNextUp(NextUpQuery request, List<Folder> parentsFolders, DtoOptions dtoOptions)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2014-09-14 11:10:51 -04:00
|
|
|
|
var user = _userManager.GetUserById(request.UserId);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found");
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 01:32:04 -04:00
|
|
|
|
string presentationUniqueKey = null;
|
|
|
|
|
int? limit = null;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(request.SeriesId))
|
|
|
|
|
{
|
|
|
|
|
var series = _libraryManager.GetItemById(request.SeriesId);
|
|
|
|
|
|
|
|
|
|
if (series != null)
|
|
|
|
|
{
|
2016-07-01 11:51:35 -04:00
|
|
|
|
presentationUniqueKey = GetUniqueSeriesKey(series);
|
2016-05-02 01:32:04 -04:00
|
|
|
|
limit = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-05 02:09:11 -04:00
|
|
|
|
if (string.IsNullOrWhiteSpace(presentationUniqueKey) && limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
limit = limit.Value + 10;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-20 02:46:51 -04:00
|
|
|
|
var items = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2015-10-29 09:28:05 -04:00
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Series).Name },
|
2016-12-12 03:53:25 -05:00
|
|
|
|
SortBy = new[] { ItemSortBy.SeriesDatePlayed },
|
|
|
|
|
SortOrder = SortOrder.Descending,
|
2016-05-02 01:32:04 -04:00
|
|
|
|
PresentationUniqueKey = presentationUniqueKey,
|
2016-12-12 00:49:19 -05:00
|
|
|
|
Limit = limit,
|
|
|
|
|
DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions
|
|
|
|
|
{
|
|
|
|
|
Fields = new List<ItemFields>
|
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
ItemFields.PresentationUniqueKey
|
2016-12-12 00:49:19 -05:00
|
|
|
|
},
|
|
|
|
|
EnableImages = false
|
|
|
|
|
}
|
2015-10-29 09:28:05 -04:00
|
|
|
|
|
2016-12-12 03:53:25 -05:00
|
|
|
|
}, parentsFolders.Cast<BaseItem>().ToList()).Cast<Series>().Select(GetUniqueSeriesKey);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
2017-05-21 03:25:49 -04:00
|
|
|
|
var episodes = GetNextUpEpisodes(request, user, items, dtoOptions);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2017-03-21 13:31:40 -04:00
|
|
|
|
return GetResult(episodes, request);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-21 03:25:49 -04:00
|
|
|
|
public IEnumerable<Episode> GetNextUpEpisodes(NextUpQuery request, User user, IEnumerable<string> seriesKeys, DtoOptions dtoOptions)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentUser = user;
|
|
|
|
|
|
2016-12-12 03:53:25 -05:00
|
|
|
|
var allNextUp = seriesKeys
|
2017-05-21 03:25:49 -04:00
|
|
|
|
.Select(i => GetNextUp(i, currentUser, dtoOptions));
|
2016-12-12 03:53:25 -05:00
|
|
|
|
|
|
|
|
|
//allNextUp = allNextUp.OrderByDescending(i => i.Item1);
|
2016-07-23 16:27:22 -04:00
|
|
|
|
|
|
|
|
|
// If viewing all next up for all series, remove first episodes
|
2016-12-12 00:49:19 -05:00
|
|
|
|
// But if that returns empty, keep those first episodes (avoid completely empty view)
|
2016-12-12 17:43:07 -05:00
|
|
|
|
var alwaysEnableFirstEpisode = !string.IsNullOrWhiteSpace(request.SeriesId);
|
2017-01-31 16:15:22 -05:00
|
|
|
|
var anyFound = false;
|
2016-07-23 16:27:22 -04:00
|
|
|
|
|
|
|
|
|
return allNextUp
|
2016-12-12 00:49:19 -05:00
|
|
|
|
.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
if (alwaysEnableFirstEpisode || i.Item1 != DateTime.MinValue)
|
2017-01-31 16:15:22 -05:00
|
|
|
|
{
|
|
|
|
|
anyFound = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!anyFound && i.Item1 == DateTime.MinValue)
|
2016-12-12 00:49:19 -05:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-15 01:41:10 -05:00
|
|
|
|
return false;
|
2016-12-12 00:49:19 -05:00
|
|
|
|
})
|
2016-11-21 12:17:26 -05:00
|
|
|
|
.Select(i => i.Item2())
|
2017-03-21 13:31:40 -04:00
|
|
|
|
.Where(i => i != null);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-01 11:51:35 -04:00
|
|
|
|
private string GetUniqueSeriesKey(BaseItem series)
|
|
|
|
|
{
|
2016-08-12 15:11:45 -04:00
|
|
|
|
return series.GetPresentationUniqueKey();
|
2016-07-01 11:51:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-01 16:10:54 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the next up.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task{Episode}.</returns>
|
2017-05-21 03:25:49 -04:00
|
|
|
|
private Tuple<DateTime, Func<Episode>> GetNextUp(string seriesKey, User user, DtoOptions dtoOptions)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2016-06-29 12:31:01 -04:00
|
|
|
|
var lastWatchedEpisode = _libraryManager.GetItemList(new InternalItemsQuery(user)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
AncestorWithPresentationUniqueKey = null,
|
|
|
|
|
SeriesPresentationUniqueKey = seriesKey,
|
2016-06-14 15:21:26 -04:00
|
|
|
|
IncludeItemTypes = new[] { typeof(Episode).Name },
|
|
|
|
|
SortBy = new[] { ItemSortBy.SortName },
|
2016-06-29 12:31:01 -04:00
|
|
|
|
SortOrder = SortOrder.Descending,
|
|
|
|
|
IsPlayed = true,
|
2016-06-14 15:21:26 -04:00
|
|
|
|
Limit = 1,
|
2016-12-12 00:49:19 -05:00
|
|
|
|
ParentIndexNumberNotEquals = 0,
|
|
|
|
|
DtoOptions = new MediaBrowser.Controller.Dto.DtoOptions
|
|
|
|
|
{
|
|
|
|
|
Fields = new List<ItemFields>
|
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
ItemFields.SortName
|
2016-12-12 00:49:19 -05:00
|
|
|
|
},
|
|
|
|
|
EnableImages = false
|
|
|
|
|
}
|
2016-06-03 20:15:14 -04:00
|
|
|
|
|
2016-06-29 12:31:01 -04:00
|
|
|
|
}).FirstOrDefault();
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2016-11-21 12:17:26 -05:00
|
|
|
|
Func<Episode> getEpisode = () =>
|
2016-06-03 20:15:14 -04:00
|
|
|
|
{
|
2016-11-21 12:17:26 -05:00
|
|
|
|
return _libraryManager.GetItemList(new InternalItemsQuery(user)
|
|
|
|
|
{
|
2017-05-24 15:12:55 -04:00
|
|
|
|
AncestorWithPresentationUniqueKey = null,
|
|
|
|
|
SeriesPresentationUniqueKey = seriesKey,
|
2016-11-21 12:17:26 -05:00
|
|
|
|
IncludeItemTypes = new[] { typeof(Episode).Name },
|
|
|
|
|
SortBy = new[] { ItemSortBy.SortName },
|
|
|
|
|
SortOrder = SortOrder.Ascending,
|
|
|
|
|
Limit = 1,
|
|
|
|
|
IsPlayed = false,
|
|
|
|
|
IsVirtualItem = false,
|
|
|
|
|
ParentIndexNumberNotEquals = 0,
|
2017-05-21 03:25:49 -04:00
|
|
|
|
MinSortName = lastWatchedEpisode == null ? null : lastWatchedEpisode.SortName,
|
|
|
|
|
DtoOptions = dtoOptions
|
2016-11-21 12:17:26 -05:00
|
|
|
|
|
|
|
|
|
}).Cast<Episode>().FirstOrDefault();
|
|
|
|
|
};
|
2016-06-14 15:21:26 -04:00
|
|
|
|
|
2016-11-21 12:17:26 -05:00
|
|
|
|
if (lastWatchedEpisode != null)
|
2016-06-14 15:21:26 -04:00
|
|
|
|
{
|
|
|
|
|
var userData = _userDataManager.GetUserData(user, lastWatchedEpisode);
|
2016-06-03 20:15:14 -04:00
|
|
|
|
|
2016-07-01 11:51:35 -04:00
|
|
|
|
var lastWatchedDate = userData.LastPlayedDate ?? DateTime.MinValue.AddDays(1);
|
|
|
|
|
|
2016-11-21 12:17:26 -05:00
|
|
|
|
return new Tuple<DateTime, Func<Episode>>(lastWatchedDate, getEpisode);
|
2016-06-03 20:15:14 -04:00
|
|
|
|
}
|
2015-01-23 01:15:15 -05:00
|
|
|
|
|
|
|
|
|
// Return the first episode
|
2016-11-21 12:17:26 -05:00
|
|
|
|
return new Tuple<DateTime, Func<Episode>>(DateTime.MinValue, getEpisode);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-21 13:31:40 -04:00
|
|
|
|
private QueryResult<BaseItem> GetResult(IEnumerable<BaseItem> items, NextUpQuery query)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2017-03-21 13:31:40 -04:00
|
|
|
|
int totalCount = 0;
|
2014-09-01 16:10:54 -04:00
|
|
|
|
|
2017-03-21 13:31:40 -04:00
|
|
|
|
if (query.EnableTotalRecordCount)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2017-03-21 13:31:40 -04:00
|
|
|
|
var list = items.ToList();
|
|
|
|
|
totalCount = list.Count;
|
|
|
|
|
items = list;
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
2017-03-21 13:31:40 -04:00
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(query.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
2014-09-01 16:10:54 -04:00
|
|
|
|
{
|
2017-03-21 13:31:40 -04:00
|
|
|
|
items = items.Take(query.Limit.Value);
|
2014-09-01 16:10:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new QueryResult<BaseItem>
|
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = totalCount,
|
2017-03-21 13:31:40 -04:00
|
|
|
|
Items = items.ToArray()
|
2014-09-01 16:10:54 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|