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