2015-01-04 06:55:34 +01:00
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2014-09-30 06:47:30 +02:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using MediaBrowser.Controller.Channels;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Dto;
|
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Channels;
|
|
|
|
|
using MediaBrowser.Model.Dto;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2015-01-17 21:12:02 +01:00
|
|
|
|
using MediaBrowser.Model.Extensions;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2014-09-30 06:47:30 +02:00
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
2014-05-04 16:19:46 +02:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using System;
|
2014-07-09 02:46:11 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2015-01-04 06:55:34 +01:00
|
|
|
|
using System.Net;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-26 08:48:54 +02:00
|
|
|
|
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-03-19 06:04:38 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-03-19 16:38:05 +01:00
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
2016-10-25 21:02:04 +02:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
2016-10-24 04:45:23 +02:00
|
|
|
|
using MediaBrowser.Model.Globalization;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2016-11-02 21:53:50 +01:00
|
|
|
|
namespace Emby.Server.Implementations.Channels
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2016-01-29 04:40:21 +01:00
|
|
|
|
public class ChannelManager : IChannelManager
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
|
|
|
|
private IChannel[] _channels;
|
|
|
|
|
|
|
|
|
|
private readonly IUserManager _userManager;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
private readonly IUserDataManager _userDataManager;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
private readonly IDtoService _dtoService;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-05-04 16:19:46 +02:00
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2014-09-30 06:47:30 +02:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2015-10-17 17:05:12 +02:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-06-07 21:46:24 +02:00
|
|
|
|
private readonly ILocalizationManager _localization;
|
2014-06-20 06:50:30 +02:00
|
|
|
|
private readonly ConcurrentDictionary<Guid, bool> _refreshedItems = new ConcurrentDictionary<Guid, bool>();
|
|
|
|
|
|
2015-10-17 17:05:12 +02:00
|
|
|
|
public ChannelManager(IUserManager userManager, IDtoService dtoService, ILibraryManager libraryManager, ILogger logger, IServerConfigurationManager config, IFileSystem fileSystem, IUserDataManager userDataManager, IJsonSerializer jsonSerializer, ILocalizationManager localization, IHttpClient httpClient, IProviderManager providerManager)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_dtoService = dtoService;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_config = config;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
_userDataManager = userDataManager;
|
2014-05-04 16:19:46 +02:00
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-06-07 21:46:24 +02:00
|
|
|
|
_localization = localization;
|
2014-09-30 06:47:30 +02:00
|
|
|
|
_httpClient = httpClient;
|
2015-10-17 17:05:12 +02:00
|
|
|
|
_providerManager = providerManager;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-16 01:30:04 +02:00
|
|
|
|
private TimeSpan CacheLength
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2014-09-22 23:56:54 +02:00
|
|
|
|
return TimeSpan.FromHours(6);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-16 05:55:26 +02:00
|
|
|
|
public void AddParts(IEnumerable<IChannel> channels)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2015-09-16 05:55:26 +02:00
|
|
|
|
_channels = channels.ToArray();
|
2014-05-04 01:38:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<IChannel> GetAllChannels()
|
|
|
|
|
{
|
2015-09-16 05:55:26 +02:00
|
|
|
|
return _channels
|
2014-05-04 01:38:23 +02:00
|
|
|
|
.OrderBy(i => i.Name);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-21 06:01:16 +01:00
|
|
|
|
public IEnumerable<Guid> GetInstalledChannelIds()
|
|
|
|
|
{
|
|
|
|
|
return GetAllChannels().Select(i => GetInternalChannelId(i.Name));
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
public Task<QueryResult<Channel>> GetChannelsInternal(ChannelQuery query, CancellationToken cancellationToken)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
2014-09-14 17:10:51 +02:00
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var channels = GetAllChannels()
|
|
|
|
|
.Select(GetChannelEntity)
|
|
|
|
|
.OrderBy(i => i.SortName)
|
|
|
|
|
.ToList();
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-06-17 18:03:14 +02:00
|
|
|
|
if (query.SupportsLatestItems.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = query.SupportsLatestItems.Value;
|
2015-10-18 19:35:36 +02:00
|
|
|
|
channels = channels.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-03-27 23:11:27 +02:00
|
|
|
|
return GetChannelProvider(i) is ISupportsLatestMedia == val;
|
2015-10-18 19:35:36 +02:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2014-06-17 18:03:14 +02:00
|
|
|
|
}
|
2014-06-23 18:05:19 +02:00
|
|
|
|
if (query.IsFavorite.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = query.IsFavorite.Value;
|
2017-05-26 08:48:54 +02:00
|
|
|
|
channels = channels.Where(i => _userDataManager.GetUserData(user, i).IsFavorite == val)
|
2014-06-23 18:05:19 +02:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2014-06-17 18:03:14 +02:00
|
|
|
|
|
2014-03-18 02:45:41 +01:00
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
2015-10-19 17:33:49 +02:00
|
|
|
|
channels = channels.Where(i =>
|
|
|
|
|
{
|
|
|
|
|
if (!i.IsVisible(user))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return GetChannelProvider(i).IsEnabledFor(user.Id.ToString("N"));
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}).ToList();
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-14 20:04:26 +02:00
|
|
|
|
var all = channels;
|
|
|
|
|
var totalCount = all.Count;
|
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Skip(query.StartIndex.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Take(query.Limit.Value).ToList();
|
|
|
|
|
}
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var returnItems = all.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<Channel>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
|
|
|
|
TotalRecordCount = totalCount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetChannels(ChannelQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
2014-09-14 17:10:51 +02:00
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
|
|
|
|
var internalResult = await GetChannelsInternal(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-12-27 06:08:39 +01:00
|
|
|
|
var dtoOptions = new DtoOptions();
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2016-06-19 08:18:29 +02:00
|
|
|
|
var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
|
2014-03-18 02:45:41 +01:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
2014-07-30 05:31:35 +02:00
|
|
|
|
TotalRecordCount = internalResult.TotalRecordCount
|
2014-03-18 02:45:41 +01:00
|
|
|
|
};
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
return result;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task RefreshChannels(IProgress<double> progress, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-01-29 04:40:21 +01:00
|
|
|
|
_refreshedItems.Clear();
|
|
|
|
|
|
2014-05-04 01:38:23 +02:00
|
|
|
|
var allChannelsList = GetAllChannels().ToList();
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
|
|
|
|
var numComplete = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var channelInfo in allChannelsList)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-10-25 18:13:30 +01:00
|
|
|
|
await GetChannel(channelInfo, cancellationToken).ConfigureAwait(false);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting channel information for {0}", ex, channelInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
numComplete++;
|
|
|
|
|
double percent = numComplete;
|
|
|
|
|
percent /= allChannelsList.Count;
|
|
|
|
|
|
|
|
|
|
progress.Report(100 * percent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progress.Report(100);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:07:28 +02:00
|
|
|
|
private Channel GetChannelEntity(IChannel channel)
|
|
|
|
|
{
|
|
|
|
|
var item = GetChannel(GetInternalChannelId(channel.Name).ToString("N"));
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
item = GetChannel(channel, CancellationToken.None).Result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 06:51:43 +01:00
|
|
|
|
private List<ChannelMediaInfo> GetSavedMediaSources(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(item.GetInternalMetadataPath(), "channelmediasources.json");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ChannelMediaInfo>>(path) ?? new List<ChannelMediaInfo>();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return new List<ChannelMediaInfo>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveMediaSources(BaseItem item, List<ChannelMediaInfo> mediaSources)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(item.GetInternalMetadataPath(), "channelmediasources.json");
|
|
|
|
|
|
|
|
|
|
if (mediaSources == null || mediaSources.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_fileSystem.DeleteFile(path);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2017-05-26 08:48:54 +02:00
|
|
|
|
|
2016-10-31 06:51:43 +01:00
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 20:14:45 +02:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
2016-10-31 06:51:43 +01:00
|
|
|
|
|
|
|
|
|
_jsonSerializer.SerializeToFile(mediaSources, path);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 08:48:54 +02:00
|
|
|
|
public IEnumerable<MediaSourceInfo> GetStaticMediaSources(BaseItem item, CancellationToken cancellationToken)
|
2014-05-18 21:58:42 +02:00
|
|
|
|
{
|
2017-04-29 08:22:33 +02:00
|
|
|
|
IEnumerable<ChannelMediaInfo> results = GetSavedMediaSources(item);
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2016-12-23 09:54:30 +01:00
|
|
|
|
return SortMediaInfoResults(results)
|
2015-03-28 21:22:27 +01:00
|
|
|
|
.Select(i => GetMediaSource(item, i))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 05:22:33 +01:00
|
|
|
|
public async Task<IEnumerable<MediaSourceInfo>> GetDynamicMediaSources(BaseItem item, CancellationToken cancellationToken)
|
2015-03-28 21:22:27 +01:00
|
|
|
|
{
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var channel = GetChannel(item.ChannelId);
|
2014-05-19 00:26:06 +02:00
|
|
|
|
var channelPlugin = GetChannelProvider(channel);
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2014-05-19 00:26:06 +02:00
|
|
|
|
var requiresCallback = channelPlugin as IRequiresMediaInfoCallback;
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2014-06-02 21:32:41 +02:00
|
|
|
|
IEnumerable<ChannelMediaInfo> results;
|
|
|
|
|
|
2015-03-28 21:22:27 +01:00
|
|
|
|
if (requiresCallback != null)
|
2014-05-18 21:58:42 +02:00
|
|
|
|
{
|
2016-11-13 22:04:21 +01:00
|
|
|
|
results = await GetChannelItemMediaSourcesInternal(requiresCallback, GetItemExternalId(item), cancellationToken)
|
2015-03-28 21:22:27 +01:00
|
|
|
|
.ConfigureAwait(false);
|
2014-06-02 21:32:41 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-03-28 21:22:27 +01:00
|
|
|
|
results = new List<ChannelMediaInfo>();
|
2014-05-18 21:58:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-23 09:54:30 +01:00
|
|
|
|
return SortMediaInfoResults(results)
|
2015-03-28 21:22:27 +01:00
|
|
|
|
.Select(i => GetMediaSource(item, i))
|
2014-06-02 21:32:41 +02:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 06:50:30 +02:00
|
|
|
|
private readonly ConcurrentDictionary<string, Tuple<DateTime, List<ChannelMediaInfo>>> _channelItemMediaInfo =
|
|
|
|
|
new ConcurrentDictionary<string, Tuple<DateTime, List<ChannelMediaInfo>>>();
|
|
|
|
|
|
|
|
|
|
private async Task<IEnumerable<ChannelMediaInfo>> GetChannelItemMediaSourcesInternal(IRequiresMediaInfoCallback channel, string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Tuple<DateTime, List<ChannelMediaInfo>> cachedInfo;
|
|
|
|
|
|
|
|
|
|
if (_channelItemMediaInfo.TryGetValue(id, out cachedInfo))
|
|
|
|
|
{
|
|
|
|
|
if ((DateTime.UtcNow - cachedInfo.Item1).TotalMinutes < 5)
|
|
|
|
|
{
|
|
|
|
|
return cachedInfo.Item2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mediaInfo = await channel.GetChannelItemMediaInfo(id, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
var list = mediaInfo.ToList();
|
|
|
|
|
|
|
|
|
|
var item2 = new Tuple<DateTime, List<ChannelMediaInfo>>(DateTime.UtcNow, list);
|
|
|
|
|
_channelItemMediaInfo.AddOrUpdate(id, item2, (key, oldValue) => item2);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 05:22:33 +01:00
|
|
|
|
private MediaSourceInfo GetMediaSource(BaseItem item, ChannelMediaInfo info)
|
2014-06-03 04:01:30 +02:00
|
|
|
|
{
|
2017-01-19 08:32:39 +01:00
|
|
|
|
var source = info.ToMediaSource(item.Id);
|
2014-06-03 04:01:30 +02:00
|
|
|
|
|
2014-10-15 06:11:40 +02:00
|
|
|
|
source.RunTimeTicks = source.RunTimeTicks ?? item.RunTimeTicks;
|
2014-10-08 03:37:45 +02:00
|
|
|
|
|
2014-06-03 04:01:30 +02:00
|
|
|
|
return source;
|
2014-06-02 21:32:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<ChannelMediaInfo> SortMediaInfoResults(IEnumerable<ChannelMediaInfo> channelMediaSources)
|
|
|
|
|
{
|
|
|
|
|
var list = channelMediaSources.ToList();
|
|
|
|
|
|
2014-06-30 19:40:46 +02:00
|
|
|
|
var options = _config.GetChannelsConfiguration();
|
|
|
|
|
|
|
|
|
|
var width = options.PreferredStreamingWidth;
|
2014-06-02 21:32:41 +02:00
|
|
|
|
|
|
|
|
|
if (width.HasValue)
|
|
|
|
|
{
|
|
|
|
|
var val = width.Value;
|
|
|
|
|
|
2014-07-11 06:27:46 +02:00
|
|
|
|
var res = list
|
2016-03-27 23:11:27 +02:00
|
|
|
|
.OrderBy(i => i.Width.HasValue && i.Width.Value <= val ? 0 : 1)
|
2014-07-11 06:27:46 +02:00
|
|
|
|
.ThenBy(i => Math.Abs((i.Width ?? 0) - val))
|
2014-06-02 21:32:41 +02:00
|
|
|
|
.ThenByDescending(i => i.Width ?? 0)
|
2014-07-11 06:27:46 +02:00
|
|
|
|
.ThenBy(list.IndexOf)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return res;
|
2014-06-02 21:32:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list
|
|
|
|
|
.OrderByDescending(i => i.Width ?? 0)
|
|
|
|
|
.ThenBy(list.IndexOf);
|
2014-05-18 21:58:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 02:45:41 +01:00
|
|
|
|
private async Task<Channel> GetChannel(IChannel channelInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-11-21 06:01:16 +01:00
|
|
|
|
var parentFolder = await GetInternalChannelFolder(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
var parentFolderId = parentFolder.Id;
|
|
|
|
|
|
2015-01-24 05:50:45 +01:00
|
|
|
|
var id = GetInternalChannelId(channelInfo.Name);
|
2016-03-19 20:32:37 +01:00
|
|
|
|
var idString = id.ToString("N");
|
2015-01-24 05:50:45 +01:00
|
|
|
|
|
|
|
|
|
var path = Channel.GetInternalMetadataPath(_config.ApplicationPaths.InternalMetadataPath, id);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
|
|
|
|
var isNew = false;
|
2015-11-21 06:01:16 +01:00
|
|
|
|
var forceUpdate = false;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
|
|
|
|
var item = _libraryManager.GetItemById(id) as Channel;
|
|
|
|
|
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
item = new Channel
|
|
|
|
|
{
|
|
|
|
|
Name = channelInfo.Name,
|
|
|
|
|
Id = id,
|
2015-10-04 05:38:46 +02:00
|
|
|
|
DateCreated = _fileSystem.GetCreationTimeUtc(path),
|
2015-11-21 06:01:16 +01:00
|
|
|
|
DateModified = _fileSystem.GetLastWriteTimeUtc(path)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
isNew = true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-21 06:01:16 +01:00
|
|
|
|
if (!string.Equals(item.Path, path, StringComparison.OrdinalIgnoreCase))
|
2015-11-06 16:02:22 +01:00
|
|
|
|
{
|
2015-11-21 05:57:11 +01:00
|
|
|
|
isNew = true;
|
2015-11-02 18:25:01 +01:00
|
|
|
|
}
|
2015-11-21 06:01:16 +01:00
|
|
|
|
item.Path = path;
|
|
|
|
|
|
2016-03-19 20:32:37 +01:00
|
|
|
|
if (!string.Equals(item.ChannelId, idString, StringComparison.OrdinalIgnoreCase))
|
2015-11-21 06:01:16 +01:00
|
|
|
|
{
|
|
|
|
|
forceUpdate = true;
|
|
|
|
|
}
|
2016-03-19 20:32:37 +01:00
|
|
|
|
item.ChannelId = idString;
|
2015-11-21 06:01:16 +01:00
|
|
|
|
|
|
|
|
|
if (item.ParentId != parentFolderId)
|
|
|
|
|
{
|
|
|
|
|
forceUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
item.ParentId = parentFolderId;
|
2015-09-29 19:35:23 +02:00
|
|
|
|
|
2014-06-07 21:46:24 +02:00
|
|
|
|
item.OfficialRating = GetOfficialRating(channelInfo.ParentalRating);
|
|
|
|
|
item.Overview = channelInfo.Description;
|
2014-05-24 01:58:28 +02:00
|
|
|
|
item.HomePageUrl = channelInfo.HomePageUrl;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2015-10-20 04:06:05 +02:00
|
|
|
|
if (string.IsNullOrWhiteSpace(item.Name))
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
|
|
|
|
item.Name = channelInfo.Name;
|
|
|
|
|
}
|
2015-11-21 05:57:11 +01:00
|
|
|
|
|
2015-11-21 06:01:16 +01:00
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
else if (forceUpdate)
|
|
|
|
|
{
|
|
|
|
|
await item.UpdateToRepository(ItemUpdateType.None, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2015-11-21 06:01:16 +01:00
|
|
|
|
await item.RefreshMetadata(new MetadataRefreshOptions(_fileSystem), cancellationToken);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
private string GetOfficialRating(ChannelParentalRating rating)
|
|
|
|
|
{
|
|
|
|
|
switch (rating)
|
|
|
|
|
{
|
|
|
|
|
case ChannelParentalRating.Adult:
|
|
|
|
|
return "XXX";
|
|
|
|
|
case ChannelParentalRating.UsR:
|
|
|
|
|
return "R";
|
|
|
|
|
case ChannelParentalRating.UsPG13:
|
|
|
|
|
return "PG-13";
|
|
|
|
|
case ChannelParentalRating.UsPG:
|
|
|
|
|
return "PG";
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
public Channel GetChannel(string id)
|
|
|
|
|
{
|
2016-03-19 20:32:37 +01:00
|
|
|
|
return _libraryManager.GetItemById(id) as Channel;
|
2014-05-19 21:51:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-02 21:32:41 +02:00
|
|
|
|
public IEnumerable<ChannelFeatures> GetAllChannelFeatures()
|
|
|
|
|
{
|
2017-05-21 09:25:49 +02:00
|
|
|
|
return _libraryManager.GetItemIds(new InternalItemsQuery
|
2015-10-20 04:06:05 +02:00
|
|
|
|
{
|
|
|
|
|
IncludeItemTypes = new[] { typeof(Channel).Name },
|
|
|
|
|
SortBy = new[] { ItemSortBy.SortName }
|
|
|
|
|
|
2017-05-21 09:25:49 +02:00
|
|
|
|
}).Select(i => GetChannelFeatures(i.ToString("N")));
|
2014-06-02 21:32:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 19:09:48 +02:00
|
|
|
|
public ChannelFeatures GetChannelFeatures(string id)
|
|
|
|
|
{
|
2016-03-19 20:32:37 +01:00
|
|
|
|
if (string.IsNullOrWhiteSpace(id))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("id");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 19:09:48 +02:00
|
|
|
|
var channel = GetChannel(id);
|
|
|
|
|
var channelProvider = GetChannelProvider(channel);
|
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
return GetChannelFeaturesDto(channel, channelProvider, channelProvider.GetChannelFeatures());
|
2014-05-27 19:57:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 22:11:34 +02:00
|
|
|
|
public bool SupportsSync(string channelId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(channelId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("channelId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//var channel = GetChannel(channelId);
|
|
|
|
|
var channelProvider = GetChannelProvider(channelId);
|
|
|
|
|
|
|
|
|
|
return channelProvider.GetChannelFeatures().SupportsContentDownloading;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
public ChannelFeatures GetChannelFeaturesDto(Channel channel,
|
|
|
|
|
IChannel provider,
|
|
|
|
|
InternalChannelFeatures features)
|
2014-05-27 19:57:29 +02:00
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
var isIndexable = provider is IIndexableChannel;
|
2014-06-16 01:30:04 +02:00
|
|
|
|
var supportsLatest = provider is ISupportsLatestMedia;
|
2014-06-06 19:14:02 +02:00
|
|
|
|
|
2014-05-27 19:57:29 +02:00
|
|
|
|
return new ChannelFeatures
|
|
|
|
|
{
|
|
|
|
|
CanFilter = !features.MaxPageSize.HasValue,
|
2014-06-06 19:14:02 +02:00
|
|
|
|
CanSearch = provider is ISearchableChannel,
|
2014-05-27 19:57:29 +02:00
|
|
|
|
ContentTypes = features.ContentTypes,
|
|
|
|
|
DefaultSortFields = features.DefaultSortFields,
|
|
|
|
|
MaxPageSize = features.MaxPageSize,
|
|
|
|
|
MediaTypes = features.MediaTypes,
|
2014-06-02 21:32:41 +02:00
|
|
|
|
SupportsSortOrderToggle = features.SupportsSortOrderToggle,
|
2014-06-16 01:30:04 +02:00
|
|
|
|
SupportsLatestMedia = supportsLatest,
|
2014-06-02 21:32:41 +02:00
|
|
|
|
Name = channel.Name,
|
|
|
|
|
Id = channel.Id.ToString("N"),
|
2015-02-11 04:28:34 +01:00
|
|
|
|
SupportsContentDownloading = features.SupportsContentDownloading && (isIndexable || supportsLatest),
|
2014-09-28 17:27:26 +02:00
|
|
|
|
AutoRefreshLevels = features.AutoRefreshLevels
|
2014-05-27 19:57:29 +02:00
|
|
|
|
};
|
2014-05-27 19:09:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 02:45:41 +01:00
|
|
|
|
private Guid GetInternalChannelId(string name)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("name");
|
|
|
|
|
}
|
2016-03-24 07:04:58 +01:00
|
|
|
|
return _libraryManager.GetNewItemId("Channel " + name, typeof(Channel));
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-16 01:30:04 +02:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetLatestChannelItems(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
2014-09-30 06:47:30 +02:00
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
|
|
|
|
|
2014-12-16 06:01:57 +01:00
|
|
|
|
var limit = query.Limit;
|
|
|
|
|
|
2014-12-14 06:23:04 +01:00
|
|
|
|
// See below about parental control
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
query.StartIndex = null;
|
2014-12-16 06:01:57 +01:00
|
|
|
|
query.Limit = null;
|
2014-12-14 06:23:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 06:47:30 +02:00
|
|
|
|
var internalResult = await GetLatestChannelItemsInternal(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2014-12-14 06:23:04 +01:00
|
|
|
|
var items = internalResult.Items;
|
|
|
|
|
var totalRecordCount = internalResult.TotalRecordCount;
|
|
|
|
|
|
|
|
|
|
// Supporting parental control is a hack because it has to be done after querying the remote data source
|
|
|
|
|
// This will get screwy if apps try to page, so limit to 10 results in an attempt to always keep them on the first page
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
items = items.Where(i => i.IsVisible(user))
|
2014-12-16 06:01:57 +01:00
|
|
|
|
.Take(limit ?? 10)
|
2014-12-14 06:23:04 +01:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
totalRecordCount = items.Length;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-27 06:08:39 +01:00
|
|
|
|
var dtoOptions = new DtoOptions();
|
|
|
|
|
|
2016-06-19 08:18:29 +02:00
|
|
|
|
var returnItems = (await _dtoService.GetBaseItemDtos(items, dtoOptions, user).ConfigureAwait(false))
|
2014-09-30 06:47:30 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
2014-12-14 06:23:04 +01:00
|
|
|
|
TotalRecordCount = totalRecordCount
|
2014-09-30 06:47:30 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<QueryResult<BaseItem>> GetLatestChannelItemsInternal(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
2014-06-16 01:30:04 +02:00
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
2014-09-14 17:10:51 +02:00
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
2014-07-11 06:27:46 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(query.UserId) && user == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("User not found.");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 02:39:07 +02:00
|
|
|
|
var channels = GetAllChannels();
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
|
|
|
|
if (query.ChannelIds.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var ids = query.ChannelIds;
|
|
|
|
|
channels = channels
|
|
|
|
|
.Where(i => ids.Contains(GetInternalChannelId(i.Name).ToString("N")))
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var userId = query.UserId;
|
|
|
|
|
|
|
|
|
|
var tasks = channels
|
|
|
|
|
.Select(async i =>
|
|
|
|
|
{
|
|
|
|
|
var indexable = i as ISupportsLatestMedia;
|
|
|
|
|
|
|
|
|
|
if (indexable != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-06-20 06:50:30 +02:00
|
|
|
|
var result = await GetLatestItems(indexable, i, userId, cancellationToken).ConfigureAwait(false);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
|
|
|
|
var resultItems = result.ToList();
|
|
|
|
|
|
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult
|
|
|
|
|
{
|
|
|
|
|
Items = resultItems,
|
|
|
|
|
TotalRecordCount = resultItems.Count
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting all media from {0}", ex, i.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-29 21:18:03 +02:00
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult());
|
2014-06-16 01:30:04 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var totalCount = results.Length;
|
|
|
|
|
|
|
|
|
|
IEnumerable<Tuple<IChannel, ChannelItemInfo>> items = results
|
2014-06-18 07:16:00 +02:00
|
|
|
|
.SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)));
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
|
|
|
|
if (query.ContentTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var contentTypes = query.ContentTypes;
|
|
|
|
|
|
|
|
|
|
items = items.Where(i => contentTypes.Contains(i.Item2.ContentType));
|
|
|
|
|
}
|
2014-09-28 17:27:26 +02:00
|
|
|
|
if (query.ExtraTypes.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var contentTypes = query.ExtraTypes;
|
|
|
|
|
|
|
|
|
|
items = items.Where(i => contentTypes.Contains(i.Item2.ExtraType));
|
|
|
|
|
}
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var token = cancellationToken;
|
|
|
|
|
var itemTasks = items.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var channelProvider = i.Item1;
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var internalChannelId = GetInternalChannelId(channelProvider.Name);
|
|
|
|
|
return GetChannelItemEntity(i.Item2, channelProvider, internalChannelId, token);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
});
|
|
|
|
|
|
2014-06-17 03:56:23 +02:00
|
|
|
|
var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
2014-06-17 03:56:23 +02:00
|
|
|
|
internalItems = ApplyFilters(internalItems, query.Filters, user).ToArray();
|
2015-10-17 17:05:12 +02:00
|
|
|
|
RefreshIfNeeded(internalItems);
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
2014-06-17 03:56:23 +02:00
|
|
|
|
internalItems = internalItems.Skip(query.StartIndex.Value).ToArray();
|
2014-06-16 01:30:04 +02:00
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
2014-06-17 03:56:23 +02:00
|
|
|
|
internalItems = internalItems.Take(query.Limit.Value).ToArray();
|
2014-06-16 01:30:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 06:47:30 +02:00
|
|
|
|
var returnItemArray = internalItems.ToArray();
|
2014-06-16 01:30:04 +02:00
|
|
|
|
|
2014-09-30 06:47:30 +02:00
|
|
|
|
return new QueryResult<BaseItem>
|
2014-06-16 01:30:04 +02:00
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = totalCount,
|
|
|
|
|
Items = returnItemArray
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 06:50:30 +02:00
|
|
|
|
private async Task<IEnumerable<ChannelItemInfo>> GetLatestItems(ISupportsLatestMedia indexable, IChannel channel, string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-09-22 23:56:54 +02:00
|
|
|
|
var cacheLength = CacheLength;
|
2014-06-20 06:50:30 +02:00
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, userId, "channelmanager-latest", null, false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ChannelItemInfo>>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-06-20 06:50:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<List<ChannelItemInfo>>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-06-20 06:50:30 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await indexable.GetLatestMedia(new ChannelLatestMediaSearch
|
|
|
|
|
{
|
|
|
|
|
UserId = userId
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var resultItems = result.ToList();
|
|
|
|
|
|
|
|
|
|
CacheResponse(resultItems, cachePath);
|
|
|
|
|
|
|
|
|
|
return resultItems;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
|
public async Task<QueryResult<BaseItem>> GetAllMediaInternal(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
2014-06-02 21:32:41 +02:00
|
|
|
|
{
|
2014-07-18 02:39:07 +02:00
|
|
|
|
var channels = GetAllChannels();
|
2014-06-02 21:32:41 +02:00
|
|
|
|
|
|
|
|
|
if (query.ChannelIds.Length > 0)
|
|
|
|
|
{
|
2014-06-16 01:30:04 +02:00
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var ids = query.ChannelIds;
|
2014-06-02 21:32:41 +02:00
|
|
|
|
channels = channels
|
2014-06-16 01:30:04 +02:00
|
|
|
|
.Where(i => ids.Contains(GetInternalChannelId(i.Name).ToString("N")))
|
2014-06-02 21:32:41 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tasks = channels
|
|
|
|
|
.Select(async i =>
|
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
var indexable = i as IIndexableChannel;
|
|
|
|
|
|
|
|
|
|
if (indexable != null)
|
2014-06-02 21:32:41 +02:00
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
try
|
2014-06-02 21:32:41 +02:00
|
|
|
|
{
|
2014-09-28 22:49:43 +02:00
|
|
|
|
var result = await GetAllItems(indexable, i, new InternalAllChannelMediaQuery
|
|
|
|
|
{
|
|
|
|
|
UserId = query.UserId,
|
|
|
|
|
ContentTypes = query.ContentTypes,
|
|
|
|
|
ExtraTypes = query.ExtraTypes,
|
|
|
|
|
TrailerTypes = query.TrailerTypes
|
|
|
|
|
|
|
|
|
|
}, cancellationToken).ConfigureAwait(false);
|
2014-06-02 21:32:41 +02:00
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, result);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error getting all media from {0}", ex, i.Name);
|
|
|
|
|
}
|
2014-06-02 21:32:41 +02:00
|
|
|
|
}
|
2016-07-29 21:18:03 +02:00
|
|
|
|
return new Tuple<IChannel, ChannelItemResult>(i, new ChannelItemResult());
|
2014-06-02 21:32:41 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var totalCount = results.Length;
|
|
|
|
|
|
|
|
|
|
IEnumerable<Tuple<IChannel, ChannelItemInfo>> items = results
|
|
|
|
|
.SelectMany(i => i.Item2.Items.Select(m => new Tuple<IChannel, ChannelItemInfo>(i.Item1, m)))
|
|
|
|
|
.OrderBy(i => i.Item2.Name);
|
|
|
|
|
|
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Skip(query.StartIndex.Value);
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
items = items.Take(query.Limit.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var token = cancellationToken;
|
|
|
|
|
var itemTasks = items.Select(i =>
|
|
|
|
|
{
|
|
|
|
|
var channelProvider = i.Item1;
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var internalChannelId = GetInternalChannelId(channelProvider.Name);
|
|
|
|
|
return GetChannelItemEntity(i.Item2, channelProvider, internalChannelId, token);
|
2014-06-02 21:32:41 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var internalItems = await Task.WhenAll(itemTasks).ConfigureAwait(false);
|
|
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
|
var returnItemArray = internalItems.ToArray();
|
2014-06-02 21:32:41 +02:00
|
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
|
return new QueryResult<BaseItem>
|
2014-06-02 21:32:41 +02:00
|
|
|
|
{
|
|
|
|
|
TotalRecordCount = totalCount,
|
|
|
|
|
Items = returnItemArray
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-09-28 17:27:26 +02:00
|
|
|
|
|
2014-09-22 23:56:54 +02:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetAllMedia(AllChannelMediaQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
|
|
|
|
|
|
|
|
|
var internalResult = await GetAllMediaInternal(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2015-10-17 17:05:12 +02:00
|
|
|
|
RefreshIfNeeded(internalResult.Items);
|
2014-09-30 06:47:30 +02:00
|
|
|
|
|
2014-12-27 06:08:39 +01:00
|
|
|
|
var dtoOptions = new DtoOptions();
|
2014-09-22 23:56:54 +02:00
|
|
|
|
|
2016-06-19 08:18:29 +02:00
|
|
|
|
var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
|
2014-09-22 23:56:54 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
|
|
|
|
TotalRecordCount = internalResult.TotalRecordCount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-06-02 21:32:41 +02:00
|
|
|
|
|
2014-09-28 22:49:43 +02:00
|
|
|
|
private async Task<ChannelItemResult> GetAllItems(IIndexableChannel indexable, IChannel channel, InternalAllChannelMediaQuery query, CancellationToken cancellationToken)
|
2014-06-20 06:50:30 +02:00
|
|
|
|
{
|
2014-10-02 02:28:16 +02:00
|
|
|
|
var cacheLength = CacheLength;
|
|
|
|
|
var folderId = _jsonSerializer.SerializeToString(query).GetMD5().ToString("N");
|
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, query.UserId, folderId, null, false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-10-02 02:28:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
return _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-10-02 02:28:16 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = await indexable.GetAllMedia(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
CacheResponse(result, cachePath);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
2014-06-20 06:50:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 06:47:30 +02:00
|
|
|
|
public async Task<QueryResult<BaseItem>> GetChannelItemsInternal(ChannelItemQuery query, IProgress<double> progress, CancellationToken cancellationToken)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2014-05-19 21:51:56 +02:00
|
|
|
|
// Get the internal channel entity
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var channel = GetChannel(query.ChannelId);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
// Find the corresponding channel provider plugin
|
|
|
|
|
var channelProvider = GetChannelProvider(channel);
|
|
|
|
|
|
2014-05-24 01:58:28 +02:00
|
|
|
|
var channelInfo = channelProvider.GetChannelFeatures();
|
2014-05-19 21:51:56 +02:00
|
|
|
|
|
|
|
|
|
int? providerStartIndex = null;
|
|
|
|
|
int? providerLimit = null;
|
|
|
|
|
|
|
|
|
|
if (channelInfo.MaxPageSize.HasValue)
|
2014-05-05 02:46:52 +02:00
|
|
|
|
{
|
2014-05-19 21:51:56 +02:00
|
|
|
|
providerStartIndex = query.StartIndex;
|
2014-05-05 02:46:52 +02:00
|
|
|
|
|
2014-05-21 02:56:24 +02:00
|
|
|
|
if (query.Limit.HasValue && query.Limit.Value > channelInfo.MaxPageSize.Value)
|
2014-05-19 21:51:56 +02:00
|
|
|
|
{
|
2015-08-12 23:39:02 +02:00
|
|
|
|
query.Limit = Math.Min(query.Limit.Value, channelInfo.MaxPageSize.Value);
|
2014-05-19 21:51:56 +02:00
|
|
|
|
}
|
|
|
|
|
providerLimit = query.Limit;
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
|
|
|
|
// This will cause some providers to fail
|
|
|
|
|
if (providerLimit == 0)
|
|
|
|
|
{
|
|
|
|
|
providerLimit = 1;
|
|
|
|
|
}
|
2014-05-19 21:51:56 +02:00
|
|
|
|
}
|
2014-05-05 02:46:52 +02:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
2014-09-14 17:10:51 +02:00
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
2014-05-05 02:46:52 +02:00
|
|
|
|
|
2014-05-27 23:17:48 +02:00
|
|
|
|
ChannelItemSortField? sortField = null;
|
|
|
|
|
ChannelItemSortField parsedField;
|
2014-06-02 21:32:41 +02:00
|
|
|
|
if (query.SortBy.Length == 1 &&
|
2014-05-27 23:17:48 +02:00
|
|
|
|
Enum.TryParse(query.SortBy[0], true, out parsedField))
|
|
|
|
|
{
|
|
|
|
|
sortField = parsedField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sortDescending = query.SortOrder.HasValue && query.SortOrder.Value == SortOrder.Descending;
|
|
|
|
|
|
2014-06-02 21:32:41 +02:00
|
|
|
|
var itemsResult = await GetChannelItems(channelProvider,
|
|
|
|
|
user,
|
|
|
|
|
query.FolderId,
|
|
|
|
|
providerStartIndex,
|
|
|
|
|
providerLimit,
|
2014-05-27 23:17:48 +02:00
|
|
|
|
sortField,
|
|
|
|
|
sortDescending,
|
|
|
|
|
cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
2014-05-19 00:26:06 +02:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
var providerTotalRecordCount = providerLimit.HasValue ? itemsResult.TotalRecordCount : null;
|
2014-05-05 02:46:52 +02:00
|
|
|
|
|
2014-07-18 21:07:28 +02:00
|
|
|
|
var tasks = itemsResult.Items.Select(i => GetChannelItemEntity(i, channelProvider, channel.Id, cancellationToken));
|
2014-05-19 21:51:56 +02:00
|
|
|
|
|
|
|
|
|
var internalItems = await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
internalItems = internalItems.Where(i => i.IsVisible(user)).ToArray();
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (providerTotalRecordCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
providerTotalRecordCount = providerTotalRecordCount.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-05 02:46:52 +02:00
|
|
|
|
|
2017-05-26 08:48:54 +02:00
|
|
|
|
return GetReturnItems(internalItems, providerTotalRecordCount, user, query);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
public async Task<QueryResult<BaseItemDto>> GetChannelItems(ChannelItemQuery query, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var user = string.IsNullOrWhiteSpace(query.UserId)
|
|
|
|
|
? null
|
2014-09-14 17:10:51 +02:00
|
|
|
|
: _userManager.GetUserById(query.UserId);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2014-09-30 06:47:30 +02:00
|
|
|
|
var internalResult = await GetChannelItemsInternal(query, new Progress<double>(), cancellationToken).ConfigureAwait(false);
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2014-12-27 06:08:39 +01:00
|
|
|
|
var dtoOptions = new DtoOptions();
|
2014-07-30 05:31:35 +02:00
|
|
|
|
|
2016-06-19 08:18:29 +02:00
|
|
|
|
var returnItems = (await _dtoService.GetBaseItemDtos(internalResult.Items, dtoOptions, user).ConfigureAwait(false))
|
2014-07-30 05:31:35 +02:00
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
|
|
|
|
{
|
|
|
|
|
Items = returnItems,
|
|
|
|
|
TotalRecordCount = internalResult.TotalRecordCount
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-13 22:04:21 +01:00
|
|
|
|
private string GetItemExternalId(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
var externalId = item.ExternalId;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(externalId))
|
|
|
|
|
{
|
|
|
|
|
externalId = item.GetProviderId("ProviderExternalId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return externalId;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-04 16:19:46 +02:00
|
|
|
|
private readonly SemaphoreSlim _resourcePool = new SemaphoreSlim(1, 1);
|
2014-05-27 23:17:48 +02:00
|
|
|
|
private async Task<ChannelItemResult> GetChannelItems(IChannel channel,
|
|
|
|
|
User user,
|
|
|
|
|
string folderId,
|
|
|
|
|
int? startIndex,
|
|
|
|
|
int? limit,
|
|
|
|
|
ChannelItemSortField? sortField,
|
|
|
|
|
bool sortDescending,
|
|
|
|
|
CancellationToken cancellationToken)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
var userId = user.Id.ToString("N");
|
|
|
|
|
|
2014-06-16 01:30:04 +02:00
|
|
|
|
var cacheLength = CacheLength;
|
2014-06-06 19:14:02 +02:00
|
|
|
|
var cachePath = GetChannelDataCachePath(channel, userId, folderId, sortField, sortDescending);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-05-04 16:19:46 +02:00
|
|
|
|
try
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
2014-05-19 21:51:56 +02:00
|
|
|
|
{
|
2016-11-14 04:44:54 +01:00
|
|
|
|
var cachedResult = _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
if (cachedResult != null)
|
|
|
|
|
{
|
|
|
|
|
return cachedResult;
|
|
|
|
|
}
|
2014-05-19 21:51:56 +02:00
|
|
|
|
}
|
2014-05-04 16:19:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
if (_fileSystem.GetLastWriteTimeUtc(cachePath).Add(cacheLength) > DateTime.UtcNow)
|
2014-05-19 21:51:56 +02:00
|
|
|
|
{
|
2016-11-14 04:44:54 +01:00
|
|
|
|
var cachedResult = _jsonSerializer.DeserializeFromFile<ChannelItemResult>(cachePath);
|
|
|
|
|
if (cachedResult != null)
|
|
|
|
|
{
|
|
|
|
|
return cachedResult;
|
|
|
|
|
}
|
2014-05-19 21:51:56 +02:00
|
|
|
|
}
|
2014-05-04 16:19:46 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2016-11-02 21:53:50 +01:00
|
|
|
|
catch (IOException)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query = new InternalChannelItemQuery
|
|
|
|
|
{
|
2014-06-06 19:14:02 +02:00
|
|
|
|
UserId = userId,
|
2014-05-19 21:51:56 +02:00
|
|
|
|
StartIndex = startIndex,
|
2014-05-27 23:17:48 +02:00
|
|
|
|
Limit = limit,
|
|
|
|
|
SortBy = sortField,
|
|
|
|
|
SortDescending = sortDescending
|
2014-05-04 16:19:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
2014-05-23 16:09:58 +02:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(folderId))
|
2014-05-18 21:58:42 +02:00
|
|
|
|
{
|
2016-03-19 05:05:33 +01:00
|
|
|
|
var categoryItem = _libraryManager.GetItemById(new Guid(folderId));
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2016-11-13 22:04:21 +01:00
|
|
|
|
query.FolderId = GetItemExternalId(categoryItem);
|
2014-05-18 21:58:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-04 16:19:46 +02:00
|
|
|
|
var result = await channel.GetChannelItems(query, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
2016-11-14 04:44:54 +01:00
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Channel returned a null result from GetChannelItems");
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (!startIndex.HasValue && !limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
CacheResponse(result, cachePath);
|
|
|
|
|
}
|
2014-05-04 16:19:46 +02:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
return result;
|
2014-05-04 16:19:46 +02:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
_resourcePool.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 06:50:30 +02:00
|
|
|
|
private void CacheResponse(object result, string path)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-04 20:14:45 +02:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
|
2014-05-04 16:19:46 +02:00
|
|
|
|
|
|
|
|
|
_jsonSerializer.SerializeToFile(result, path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error writing to channel cache file: {0}", ex, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-27 23:17:48 +02:00
|
|
|
|
private string GetChannelDataCachePath(IChannel channel,
|
2014-06-06 19:14:02 +02:00
|
|
|
|
string userId,
|
2014-05-27 23:17:48 +02:00
|
|
|
|
string folderId,
|
|
|
|
|
ChannelItemSortField? sortField,
|
|
|
|
|
bool sortDescending)
|
2014-05-04 16:19:46 +02:00
|
|
|
|
{
|
|
|
|
|
var channelId = GetInternalChannelId(channel.Name).ToString("N");
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
var userCacheKey = string.Empty;
|
|
|
|
|
|
|
|
|
|
var hasCacheKey = channel as IHasCacheKey;
|
|
|
|
|
if (hasCacheKey != null)
|
|
|
|
|
{
|
|
|
|
|
userCacheKey = hasCacheKey.GetCacheKey(userId) ?? string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 21:46:24 +02:00
|
|
|
|
var filename = string.IsNullOrWhiteSpace(folderId) ? "root" : folderId;
|
|
|
|
|
filename += userCacheKey;
|
2014-03-18 18:05:57 +01:00
|
|
|
|
|
2014-06-07 21:46:24 +02:00
|
|
|
|
var version = (channel.DataVersion ?? string.Empty).GetMD5().ToString("N");
|
2014-05-27 23:17:48 +02:00
|
|
|
|
|
|
|
|
|
if (sortField.HasValue)
|
|
|
|
|
{
|
|
|
|
|
filename += "-sortField-" + sortField.Value;
|
|
|
|
|
}
|
|
|
|
|
if (sortDescending)
|
|
|
|
|
{
|
|
|
|
|
filename += "-sortDescending";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-07 21:46:24 +02:00
|
|
|
|
filename = filename.GetMD5().ToString("N");
|
2014-05-27 23:17:48 +02:00
|
|
|
|
|
2014-06-06 19:14:02 +02:00
|
|
|
|
return Path.Combine(_config.ApplicationPaths.CachePath,
|
|
|
|
|
"channels",
|
|
|
|
|
channelId,
|
|
|
|
|
version,
|
|
|
|
|
filename + ".json");
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-26 08:48:54 +02:00
|
|
|
|
private QueryResult<BaseItem> GetReturnItems(IEnumerable<BaseItem> items,
|
2014-09-30 06:47:30 +02:00
|
|
|
|
int? totalCountFromProvider,
|
|
|
|
|
User user,
|
2015-10-17 17:05:12 +02:00
|
|
|
|
ChannelItemQuery query)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2014-05-05 02:46:52 +02:00
|
|
|
|
items = ApplyFilters(items, query.Filters, user);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2016-06-13 02:45:15 +02:00
|
|
|
|
items = _libraryManager.Sort(items, user, query.SortBy, query.SortOrder ?? SortOrder.Ascending);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-05-05 02:46:52 +02:00
|
|
|
|
var all = items.ToList();
|
2014-05-19 21:51:56 +02:00
|
|
|
|
var totalCount = totalCountFromProvider ?? all.Count;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (!totalCountFromProvider.HasValue)
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
2014-05-19 21:51:56 +02:00
|
|
|
|
if (query.StartIndex.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Skip(query.StartIndex.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
if (query.Limit.HasValue)
|
|
|
|
|
{
|
|
|
|
|
all = all.Take(query.Limit.Value).ToList();
|
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
var returnItemArray = all.ToArray();
|
2015-10-17 17:05:12 +02:00
|
|
|
|
RefreshIfNeeded(returnItemArray);
|
2014-03-18 02:45:41 +01:00
|
|
|
|
|
2014-07-30 05:31:35 +02:00
|
|
|
|
return new QueryResult<BaseItem>
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
|
|
|
|
Items = returnItemArray,
|
2014-05-03 06:20:04 +02:00
|
|
|
|
TotalRecordCount = totalCount
|
2014-03-18 02:45:41 +01:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-28 17:27:26 +02:00
|
|
|
|
private string GetIdToHash(string externalId, string channelName)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2014-05-03 06:20:04 +02:00
|
|
|
|
// Increment this as needed to force new downloads
|
2014-05-19 00:26:06 +02:00
|
|
|
|
// Incorporate Name because it's being used to convert channel entity to provider
|
2014-09-28 17:27:26 +02:00
|
|
|
|
return externalId + (channelName ?? string.Empty) + "16";
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
2014-03-18 18:05:57 +01:00
|
|
|
|
|
2014-09-28 17:27:26 +02:00
|
|
|
|
private T GetItemById<T>(string idString, string channelName, string channnelDataVersion, out bool isNew)
|
2016-03-19 05:05:33 +01:00
|
|
|
|
where T : BaseItem, new()
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
2014-09-28 17:27:26 +02:00
|
|
|
|
var id = GetIdToHash(idString, channelName).GetMBId(typeof(T));
|
2014-03-19 02:35:40 +01:00
|
|
|
|
|
2014-09-28 17:27:26 +02:00
|
|
|
|
T item = null;
|
2014-05-19 00:26:06 +02:00
|
|
|
|
|
2014-09-28 17:27:26 +02:00
|
|
|
|
try
|
2014-03-18 18:05:57 +01:00
|
|
|
|
{
|
2014-09-28 17:27:26 +02:00
|
|
|
|
item = _libraryManager.GetItemById(id) as T;
|
2014-03-18 18:05:57 +01:00
|
|
|
|
}
|
2014-09-28 17:27:26 +02:00
|
|
|
|
catch (Exception ex)
|
2014-03-18 18:05:57 +01:00
|
|
|
|
{
|
2014-09-28 17:27:26 +02:00
|
|
|
|
_logger.ErrorException("Error retrieving channel item from database", ex);
|
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2015-10-04 20:10:50 +02:00
|
|
|
|
if (item == null || !string.Equals(item.ExternalEtag, channnelDataVersion, StringComparison.Ordinal))
|
2014-09-28 17:27:26 +02:00
|
|
|
|
{
|
|
|
|
|
item = new T();
|
|
|
|
|
isNew = true;
|
2014-03-18 18:05:57 +01:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-09-28 17:27:26 +02:00
|
|
|
|
isNew = false;
|
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2015-10-04 20:10:50 +02:00
|
|
|
|
item.ExternalEtag = channnelDataVersion;
|
2014-09-28 17:27:26 +02:00
|
|
|
|
item.Id = id;
|
|
|
|
|
return item;
|
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2014-09-28 17:27:26 +02:00
|
|
|
|
private async Task<BaseItem> GetChannelItemEntity(ChannelItemInfo info, IChannel channelProvider, Guid internalChannelId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
BaseItem item;
|
|
|
|
|
bool isNew;
|
2015-11-21 06:01:16 +01:00
|
|
|
|
bool forceUpdate = false;
|
2014-09-28 17:27:26 +02:00
|
|
|
|
|
|
|
|
|
if (info.Type == ChannelItemType.Folder)
|
|
|
|
|
{
|
2016-03-19 06:04:38 +01:00
|
|
|
|
if (info.FolderType == ChannelFolderType.MusicAlbum)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<MusicAlbum>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2016-06-22 06:39:06 +02:00
|
|
|
|
else if (info.FolderType == ChannelFolderType.MusicArtist)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<MusicArtist>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2016-03-19 06:04:38 +01:00
|
|
|
|
else if (info.FolderType == ChannelFolderType.PhotoAlbum)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<PhotoAlbum>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2016-06-24 05:23:31 +02:00
|
|
|
|
else if (info.FolderType == ChannelFolderType.Series)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Series>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
|
|
|
|
else if (info.FolderType == ChannelFolderType.Season)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Season>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2016-03-19 06:04:38 +01:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Folder>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2014-09-28 17:27:26 +02:00
|
|
|
|
}
|
|
|
|
|
else if (info.MediaType == ChannelMediaType.Audio)
|
|
|
|
|
{
|
2016-03-19 20:32:37 +01:00
|
|
|
|
if (info.ContentType == ChannelMediaContentType.Podcast)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<AudioPodcast>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Audio>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2014-09-28 17:27:26 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-03-19 16:38:05 +01:00
|
|
|
|
if (info.ContentType == ChannelMediaContentType.Episode)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Episode>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
|
|
|
|
else if (info.ContentType == ChannelMediaContentType.Movie)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Movie>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
|
|
|
|
else if (info.ContentType == ChannelMediaContentType.Trailer || info.ExtraType == ExtraType.Trailer)
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Trailer>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item = GetItemById<Video>(info.Id, channelProvider.Name, channelProvider.DataVersion, out isNew);
|
|
|
|
|
}
|
2014-03-18 18:05:57 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
item.RunTimeTicks = info.RunTimeTicks;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
item.Name = info.Name;
|
|
|
|
|
item.Genres = info.Genres;
|
|
|
|
|
item.Studios = info.Studios;
|
|
|
|
|
item.CommunityRating = info.CommunityRating;
|
|
|
|
|
item.Overview = info.Overview;
|
2014-07-08 03:41:03 +02:00
|
|
|
|
item.IndexNumber = info.IndexNumber;
|
|
|
|
|
item.ParentIndexNumber = info.ParentIndexNumber;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
item.PremiereDate = info.PremiereDate;
|
|
|
|
|
item.ProductionYear = info.ProductionYear;
|
|
|
|
|
item.ProviderIds = info.ProviderIds;
|
2014-09-22 23:56:54 +02:00
|
|
|
|
item.OfficialRating = info.OfficialRating;
|
2015-10-04 20:10:50 +02:00
|
|
|
|
item.DateCreated = info.DateCreated ?? DateTime.UtcNow;
|
2015-11-21 06:01:16 +01:00
|
|
|
|
item.Tags = info.Tags;
|
2016-06-12 07:03:52 +02:00
|
|
|
|
item.HomePageUrl = info.HomePageUrl;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
2016-06-25 02:20:04 +02:00
|
|
|
|
else if (info.Type == ChannelItemType.Folder && info.FolderType == ChannelFolderType.Container)
|
2016-06-22 23:23:50 +02:00
|
|
|
|
{
|
2016-06-25 02:20:04 +02:00
|
|
|
|
// At least update names of container folders
|
|
|
|
|
if (item.Name != info.Name)
|
|
|
|
|
{
|
|
|
|
|
item.Name = info.Name;
|
|
|
|
|
forceUpdate = true;
|
|
|
|
|
}
|
2016-06-22 23:23:50 +02:00
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2016-06-20 19:08:13 +02:00
|
|
|
|
var hasArtists = item as IHasArtist;
|
|
|
|
|
if (hasArtists != null)
|
|
|
|
|
{
|
|
|
|
|
hasArtists.Artists = info.Artists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hasAlbumArtists = item as IHasAlbumArtist;
|
|
|
|
|
if (hasAlbumArtists != null)
|
|
|
|
|
{
|
|
|
|
|
hasAlbumArtists.AlbumArtists = info.AlbumArtists;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 20:32:37 +01:00
|
|
|
|
var trailer = item as Trailer;
|
|
|
|
|
if (trailer != null)
|
|
|
|
|
{
|
|
|
|
|
if (!info.TrailerTypes.SequenceEqual(trailer.TrailerTypes))
|
|
|
|
|
{
|
|
|
|
|
forceUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
trailer.TrailerTypes = info.TrailerTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 05:05:33 +01:00
|
|
|
|
item.ChannelId = internalChannelId.ToString("N");
|
2015-10-04 20:10:50 +02:00
|
|
|
|
|
2015-11-21 06:01:16 +01:00
|
|
|
|
if (item.ParentId != internalChannelId)
|
2015-11-02 18:25:01 +01:00
|
|
|
|
{
|
2015-11-21 06:01:16 +01:00
|
|
|
|
forceUpdate = true;
|
2015-11-02 18:25:01 +01:00
|
|
|
|
}
|
2015-11-21 06:01:16 +01:00
|
|
|
|
item.ParentId = internalChannelId;
|
2015-11-02 18:25:01 +01:00
|
|
|
|
|
2016-03-19 05:05:33 +01:00
|
|
|
|
if (!string.Equals(item.ExternalId, info.Id, StringComparison.OrdinalIgnoreCase))
|
2015-10-04 20:10:50 +02:00
|
|
|
|
{
|
2015-11-21 06:01:16 +01:00
|
|
|
|
forceUpdate = true;
|
2015-10-04 20:10:50 +02:00
|
|
|
|
}
|
2016-03-19 05:05:33 +01:00
|
|
|
|
item.ExternalId = info.Id;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2016-03-19 06:14:47 +01:00
|
|
|
|
var channelAudioItem = item as Audio;
|
2016-03-19 05:22:33 +01:00
|
|
|
|
if (channelAudioItem != null)
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
2016-03-19 05:22:33 +01:00
|
|
|
|
channelAudioItem.ExtraType = info.ExtraType;
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
|
|
|
|
var mediaSource = info.MediaSources.FirstOrDefault();
|
2016-03-19 05:22:33 +01:00
|
|
|
|
item.Path = mediaSource == null ? null : mediaSource.Path;
|
|
|
|
|
}
|
2014-05-18 21:58:42 +02:00
|
|
|
|
|
2016-03-19 16:38:05 +01:00
|
|
|
|
var channelVideoItem = item as Video;
|
2016-03-19 05:22:33 +01:00
|
|
|
|
if (channelVideoItem != null)
|
|
|
|
|
{
|
|
|
|
|
channelVideoItem.ExtraType = info.ExtraType;
|
|
|
|
|
|
|
|
|
|
var mediaSource = info.MediaSources.FirstOrDefault();
|
2014-05-18 21:58:42 +02:00
|
|
|
|
item.Path = mediaSource == null ? null : mediaSource.Path;
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-02 18:32:40 +01:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(info.ImageUrl) && !item.HasImage(ImageType.Primary))
|
2015-10-25 18:13:30 +01:00
|
|
|
|
{
|
|
|
|
|
item.SetImagePath(ImageType.Primary, info.ImageUrl);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-19 06:04:38 +01:00
|
|
|
|
if (item.SourceType != SourceType.Channel)
|
|
|
|
|
{
|
|
|
|
|
item.SourceType = SourceType.Channel;
|
|
|
|
|
forceUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 06:20:04 +02:00
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
|
|
|
|
await _libraryManager.CreateItem(item, cancellationToken).ConfigureAwait(false);
|
2015-06-28 18:36:25 +02:00
|
|
|
|
|
2015-10-25 18:13:30 +01:00
|
|
|
|
if (info.People != null && info.People.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
await _libraryManager.UpdatePeople(item, info.People ?? new List<PersonInfo>()).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
2015-11-21 06:01:16 +01:00
|
|
|
|
else if (forceUpdate)
|
|
|
|
|
{
|
|
|
|
|
await item.UpdateToRepository(ItemUpdateType.None, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-03-18 18:05:57 +01:00
|
|
|
|
|
2016-10-31 06:51:43 +01:00
|
|
|
|
SaveMediaSources(item, info.MediaSources);
|
|
|
|
|
|
2014-03-18 18:05:57 +01:00
|
|
|
|
return item;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 17:05:12 +02:00
|
|
|
|
private void RefreshIfNeeded(BaseItem[] programs)
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
|
|
|
|
foreach (var program in programs)
|
|
|
|
|
{
|
2015-10-17 17:05:12 +02:00
|
|
|
|
RefreshIfNeeded(program);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 17:05:12 +02:00
|
|
|
|
private void RefreshIfNeeded(BaseItem program)
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
2015-01-04 06:55:34 +01:00
|
|
|
|
if (!_refreshedItems.ContainsKey(program.Id))
|
2014-05-03 06:20:04 +02:00
|
|
|
|
{
|
2015-01-04 06:55:34 +01:00
|
|
|
|
_refreshedItems.TryAdd(program.Id, true);
|
2017-04-30 04:37:51 +02:00
|
|
|
|
_providerManager.QueueRefresh(program.Id, new MetadataRefreshOptions(_fileSystem), RefreshPriority.Low);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-18 18:05:57 +01:00
|
|
|
|
internal IChannel GetChannelProvider(Channel channel)
|
2014-03-18 02:45:41 +01:00
|
|
|
|
{
|
2016-03-19 20:32:37 +01:00
|
|
|
|
if (channel == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("channel");
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-08 20:32:38 +02:00
|
|
|
|
var result = GetAllChannels()
|
|
|
|
|
.FirstOrDefault(i => string.Equals(GetInternalChannelId(i.Name).ToString("N"), channel.ChannelId, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Name, channel.Name, StringComparison.OrdinalIgnoreCase));
|
2015-10-18 19:35:36 +02:00
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("No channel provider found for channel " + channel.Name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
2016-09-07 22:11:34 +02:00
|
|
|
|
internal IChannel GetChannelProvider(string internalChannelId)
|
|
|
|
|
{
|
|
|
|
|
if (internalChannelId == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("internalChannelId");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = GetAllChannels()
|
|
|
|
|
.FirstOrDefault(i => string.Equals(GetInternalChannelId(i.Name).ToString("N"), internalChannelId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ResourceNotFoundException("No channel provider found for channel id " + internalChannelId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-03 06:20:04 +02:00
|
|
|
|
private IEnumerable<BaseItem> ApplyFilters(IEnumerable<BaseItem> items, IEnumerable<ItemFilter> filters, User user)
|
|
|
|
|
{
|
|
|
|
|
foreach (var filter in filters.OrderByDescending(f => (int)f))
|
|
|
|
|
{
|
|
|
|
|
items = ApplyFilter(items, filter, user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<BaseItem> ApplyFilter(IEnumerable<BaseItem> items, ItemFilter filter, User user)
|
|
|
|
|
{
|
|
|
|
|
// Avoid implicitly captured closure
|
|
|
|
|
var currentUser = user;
|
|
|
|
|
|
|
|
|
|
switch (filter)
|
|
|
|
|
{
|
|
|
|
|
case ItemFilter.IsFavoriteOrLikes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
2016-05-01 00:05:13 +02:00
|
|
|
|
var userdata = _userDataManager.GetUserData(user, item);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
if (userdata == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var likes = userdata.Likes ?? false;
|
|
|
|
|
var favorite = userdata.IsFavorite;
|
|
|
|
|
|
|
|
|
|
return likes || favorite;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.Likes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
2016-05-01 00:05:13 +02:00
|
|
|
|
var userdata = _userDataManager.GetUserData(user, item);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.Likes.HasValue && userdata.Likes.Value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.Dislikes:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
2016-05-01 00:05:13 +02:00
|
|
|
|
var userdata = _userDataManager.GetUserData(user, item);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.Likes.HasValue && !userdata.Likes.Value;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsFavorite:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
2016-05-01 00:05:13 +02:00
|
|
|
|
var userdata = _userDataManager.GetUserData(user, item);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.IsFavorite;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsResumable:
|
|
|
|
|
return items.Where(item =>
|
|
|
|
|
{
|
2016-05-01 00:05:13 +02:00
|
|
|
|
var userdata = _userDataManager.GetUserData(user, item);
|
2014-05-03 06:20:04 +02:00
|
|
|
|
|
|
|
|
|
return userdata != null && userdata.PlaybackPositionTicks > 0;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsPlayed:
|
|
|
|
|
return items.Where(item => item.IsPlayed(currentUser));
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsUnplayed:
|
|
|
|
|
return items.Where(item => item.IsUnplayed(currentUser));
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsFolder:
|
|
|
|
|
return items.Where(item => item.IsFolder);
|
|
|
|
|
|
|
|
|
|
case ItemFilter.IsNotFolder:
|
|
|
|
|
return items.Where(item => !item.IsFolder);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return items;
|
|
|
|
|
}
|
2014-06-07 21:46:24 +02:00
|
|
|
|
|
|
|
|
|
public async Task<BaseItemDto> GetChannelFolder(string userId, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-09-14 17:10:51 +02:00
|
|
|
|
var user = string.IsNullOrEmpty(userId) ? null : _userManager.GetUserById(userId);
|
2014-06-07 21:46:24 +02:00
|
|
|
|
|
2015-08-14 20:30:08 +02:00
|
|
|
|
var folder = await GetInternalChannelFolder(cancellationToken).ConfigureAwait(false);
|
2014-06-07 21:46:24 +02:00
|
|
|
|
|
2014-12-27 06:08:39 +01:00
|
|
|
|
return _dtoService.GetBaseItemDto(folder, new DtoOptions(), user);
|
2014-06-07 21:46:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2015-08-14 20:30:08 +02:00
|
|
|
|
public async Task<Folder> GetInternalChannelFolder(CancellationToken cancellationToken)
|
2014-06-07 21:46:24 +02:00
|
|
|
|
{
|
|
|
|
|
var name = _localization.GetLocalizedString("ViewTypeChannels");
|
2015-03-14 05:50:23 +01:00
|
|
|
|
|
2015-08-14 20:00:26 +02:00
|
|
|
|
return await _libraryManager.GetNamedView(name, "channels", "zz_" + name, cancellationToken).ConfigureAwait(false);
|
2014-06-07 21:46:24 +02:00
|
|
|
|
}
|
2014-03-18 02:45:41 +01:00
|
|
|
|
}
|
2015-11-21 06:01:16 +01:00
|
|
|
|
}
|