diff --git a/MediaBrowser.Api/LiveTv/LiveTvService.cs b/MediaBrowser.Api/LiveTv/LiveTvService.cs index fea548d12..5b7bc78a8 100644 --- a/MediaBrowser.Api/LiveTv/LiveTvService.cs +++ b/MediaBrowser.Api/LiveTv/LiveTvService.cs @@ -491,13 +491,17 @@ namespace MediaBrowser.Api.LiveTv private readonly IUserManager _userManager; private readonly IConfigurationManager _config; private readonly IHttpClient _httpClient; + private readonly ILibraryManager _libraryManager; + private readonly IDtoService _dtoService; - public LiveTvService(ILiveTvManager liveTvManager, IUserManager userManager, IConfigurationManager config, IHttpClient httpClient) + public LiveTvService(ILiveTvManager liveTvManager, IUserManager userManager, IConfigurationManager config, IHttpClient httpClient, ILibraryManager libraryManager, IDtoService dtoService) { _liveTvManager = liveTvManager; _userManager = userManager; _config = config; _httpClient = httpClient; + _libraryManager = libraryManager; + _dtoService = dtoService; } public async Task Get(GetLiveTvRegistrationInfo request) @@ -593,7 +597,7 @@ namespace MediaBrowser.Api.LiveTv public async Task Get(GetChannels request) { - var result = await _liveTvManager.GetChannels(new LiveTvChannelQuery + var channelResult = await _liveTvManager.GetInternalChannels(new LiveTvChannelQuery { ChannelType = request.Type, UserId = request.UserId, @@ -605,16 +609,30 @@ namespace MediaBrowser.Api.LiveTv EnableFavoriteSorting = request.EnableFavoriteSorting, AddCurrentProgram = request.AddCurrentProgram - }, GetDtoOptions(request), CancellationToken.None).ConfigureAwait(false); + }, CancellationToken.None).ConfigureAwait(false); + var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(request.UserId); + + var returnArray = _dtoService.GetBaseItemDtos(channelResult.Items, GetDtoOptions(Request), user).ToArray(); + + var result = new QueryResult + { + Items = returnArray, + TotalRecordCount = channelResult.TotalRecordCount + }; + return ToOptimizedSerializedResultUsingCache(result); } - public async Task Get(GetChannel request) + public object Get(GetChannel request) { - var user = string.IsNullOrEmpty(request.UserId) ? null : _userManager.GetUserById(request.UserId); + var user = string.IsNullOrWhiteSpace(request.UserId) ? null : _userManager.GetUserById(request.UserId); - var result = await _liveTvManager.GetChannel(request.Id, CancellationToken.None, user).ConfigureAwait(false); + var item = _libraryManager.GetItemById(request.Id); + + var dtoOptions = GetDtoOptions(request); + + var result = _dtoService.GetBaseItemDto(item, dtoOptions, user); return ToOptimizedSerializedResultUsingCache(result); } diff --git a/MediaBrowser.Controller/Dto/DtoOptions.cs b/MediaBrowser.Controller/Dto/DtoOptions.cs index a8d1b1862..d627cc67a 100644 --- a/MediaBrowser.Controller/Dto/DtoOptions.cs +++ b/MediaBrowser.Controller/Dto/DtoOptions.cs @@ -17,6 +17,7 @@ namespace MediaBrowser.Controller.Dto public List ImageTypes { get; set; } public int ImageTypeLimit { get; set; } public bool EnableImages { get; set; } + public bool AddProgramRecordingInfo { get; set; } public string DeviceId { get; set; } public DtoOptions() diff --git a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs index bd25b23f7..4cc7b27ea 100644 --- a/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs +++ b/MediaBrowser.Controller/LiveTv/ILiveTvManager.cs @@ -74,15 +74,6 @@ namespace MediaBrowser.Controller.LiveTv /// The listing providers. void AddParts(IEnumerable services, IEnumerable tunerHosts, IEnumerable listingProviders); - /// - /// Gets the channels. - /// - /// The query. - /// The options. - /// The cancellation token. - /// IEnumerable{Channel}. - Task> GetChannels(LiveTvChannelQuery query, DtoOptions options, CancellationToken cancellationToken); - /// /// Gets the recording. /// @@ -92,15 +83,6 @@ namespace MediaBrowser.Controller.LiveTv /// The user. /// Task{RecordingInfoDto}. Task GetRecording(string id, DtoOptions options, CancellationToken cancellationToken, User user = null); - - /// - /// Gets the channel. - /// - /// The identifier. - /// The cancellation token. - /// The user. - /// Task{RecordingInfoDto}. - Task GetChannel(string id, CancellationToken cancellationToken, User user = null); /// /// Gets the timer. @@ -385,11 +367,10 @@ namespace MediaBrowser.Controller.LiveTv /// /// Adds the channel information. /// - /// The dto. - /// The channel. + /// The items. /// The options. /// The user. - void AddChannelInfo(BaseItemDto dto, LiveTvChannel channel, DtoOptions options, User user); + void AddChannelInfo(List> items, DtoOptions options, User user); /// /// Called when [recording file deleted]. diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index 5fe9ee434..61465e1d7 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -94,12 +94,18 @@ namespace MediaBrowser.Server.Implementations.Dto var list = new List(); var programTuples = new List> { }; + var channelTuples = new List> { }; foreach (var item in items) { var dto = GetBaseItemDtoInternal(item, options, syncDictionary, user, owner); - if (item is LiveTvProgram) + var tvChannel = item as LiveTvChannel; + if (tvChannel != null) + { + channelTuples.Add(new Tuple(dto, tvChannel)); + } + else if (item is LiveTvProgram) { programTuples.Add(new Tuple(item, dto)); } @@ -131,6 +137,11 @@ namespace MediaBrowser.Server.Implementations.Dto Task.WaitAll(task); } + if (channelTuples.Count > 0) + { + _livetvManager().AddChannelInfo(channelTuples, options, user); + } + return list; } @@ -151,8 +162,13 @@ namespace MediaBrowser.Server.Implementations.Dto var syncProgress = GetSyncedItemProgress(options); var dto = GetBaseItemDtoInternal(item, options, GetSyncedItemProgressDictionary(syncProgress), user, owner); - - if (item is LiveTvProgram) + var tvChannel = item as LiveTvChannel; + if (tvChannel != null) + { + var list = new List> { new Tuple(dto, tvChannel) }; + _livetvManager().AddChannelInfo(list, options, user); + } + else if (item is LiveTvProgram) { var list = new List> { new Tuple(item, dto) }; var task = _livetvManager().AddInfoToProgramDto(list, options.Fields, user); @@ -372,12 +388,6 @@ namespace MediaBrowser.Server.Implementations.Dto AttachBasicFields(dto, item, owner, options); - var tvChannel = item as LiveTvChannel; - if (tvChannel != null) - { - _livetvManager().AddChannelInfo(dto, tvChannel, options, user); - } - var collectionFolder = item as ICollectionFolder; if (collectionFolder != null) { diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs index 81ad6a387..3da0d15d3 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvDtoService.cs @@ -152,21 +152,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv return pattern; } - /// - /// Convert the provider 0-5 scale to our 0-10 scale - /// - /// - /// - private float? GetClientCommunityRating(float? val) - { - if (!val.HasValue) - { - return null; - } - - return val.Value; - } - public LiveTvTunerInfoDto GetTunerInfoDto(string serviceName, LiveTvTunerInfo info, string channelName) { var dto = new LiveTvTunerInfoDto @@ -195,54 +180,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv return dto; } - /// - /// Gets the channel info dto. - /// - /// The info. - /// The options. - /// The current program. - /// The user. - /// ChannelInfoDto. - public ChannelInfoDto GetChannelInfoDto(LiveTvChannel info, DtoOptions options, LiveTvProgram currentProgram, User user = null) - { - var dto = new ChannelInfoDto - { - Name = info.Name, - ServiceName = info.ServiceName, - ChannelType = info.ChannelType, - Number = info.Number, - Type = info.GetClientTypeName(), - Id = info.Id.ToString("N"), - MediaType = info.MediaType, - ExternalId = info.ExternalId, - MediaSources = info.GetMediaSources(true).ToList(), - ServerId = _appHost.SystemId - }; - - if (user != null) - { - dto.UserData = _userDataManager.GetUserDataDto(info, user); - - dto.PlayAccess = info.GetPlayAccess(user); - } - - var imageTag = GetImageTag(info); - - if (imageTag != null) - { - dto.ImageTags[ImageType.Primary] = imageTag; - - _dtoService.AttachPrimaryImageAspectRatio(dto, info); - } - - if (currentProgram != null) - { - dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, options, user); - } - - return dto; - } - internal string GetImageTag(IHasImages info) { try @@ -324,7 +261,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId)) { - var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false); + var channel = liveTv.GetInternalChannel(dto.ChannelId); if (channel != null) { @@ -387,7 +324,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv if (!string.IsNullOrEmpty(dto.ChannelId) && string.IsNullOrEmpty(info.ChannelId)) { - var channel = await liveTv.GetChannel(dto.ChannelId, cancellationToken).ConfigureAwait(false); + var channel = liveTv.GetInternalChannel(dto.ChannelId); if (channel != null) { diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 22516e7a6..6c00d61f2 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -244,42 +244,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv return result; } - public async Task> GetChannels(LiveTvChannelQuery query, DtoOptions options, CancellationToken cancellationToken) - { - var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(query.UserId); - - var internalResult = await GetInternalChannels(query, cancellationToken).ConfigureAwait(false); - - var returnList = new List(); - - var now = DateTime.UtcNow; - - var programs = query.AddCurrentProgram ? _libraryManager.QueryItems(new InternalItemsQuery - { - IncludeItemTypes = new[] { typeof(LiveTvProgram).Name }, - MaxStartDate = now, - MinEndDate = now, - ChannelIds = internalResult.Items.Select(i => i.Id.ToString("N")).ToArray() - - }).Items.Cast().OrderBy(i => i.StartDate).ToList() : new List(); - - foreach (var channel in internalResult.Items) - { - var channelIdString = channel.Id.ToString("N"); - var currentProgram = programs.FirstOrDefault(i => string.Equals(i.ChannelId, channelIdString, StringComparison.OrdinalIgnoreCase)); - - returnList.Add(_tvDtoService.GetChannelInfoDto(channel, options, currentProgram, user)); - } - - var result = new QueryResult - { - Items = returnList.ToArray(), - TotalRecordCount = internalResult.TotalRecordCount - }; - - return result; - } - public LiveTvChannel GetInternalChannel(string id) { return GetInternalChannel(new Guid(id)); @@ -1859,52 +1823,37 @@ namespace MediaBrowser.Server.Implementations.LiveTv }; } - public async Task GetChannel(string id, CancellationToken cancellationToken, User user = null) + public void AddChannelInfo(List> tuples, DtoOptions options, User user) { - var channel = GetInternalChannel(id); - var now = DateTime.UtcNow; + var channelIds = tuples.Select(i => i.Item2.Id.ToString("N")).Distinct().ToArray(); + var programs = _libraryManager.GetItemList(new InternalItemsQuery(user) { IncludeItemTypes = new[] { typeof(LiveTvProgram).Name }, - ChannelIds = new[] { id }, + ChannelIds = channelIds, MaxStartDate = now, MinEndDate = now, - Limit = 1, + Limit = channelIds.Length, SortBy = new[] { "StartDate" } - }, new string[] { }).Cast(); + }, new string[] { }).ToList(); - var currentProgram = programs.FirstOrDefault(); - - var dto = _tvDtoService.GetChannelInfoDto(channel, new DtoOptions(), currentProgram, user); - - return dto; - } - - public void AddChannelInfo(BaseItemDto dto, LiveTvChannel channel, DtoOptions options, User user) - { - dto.MediaSources = channel.GetMediaSources(true).ToList(); - - var now = DateTime.UtcNow; - - var programs = _libraryManager.GetItemList(new InternalItemsQuery(user) + foreach (var tuple in tuples) { - IncludeItemTypes = new[] { typeof(LiveTvProgram).Name }, - ChannelIds = new[] { channel.Id.ToString("N") }, - MaxStartDate = now, - MinEndDate = now, - Limit = 1, - SortBy = new[] { "StartDate" } + var dto = tuple.Item1; + var channel = tuple.Item2; - }, new string[] { }).Cast(); + dto.MediaSources = channel.GetMediaSources(true).ToList(); - var currentProgram = programs.FirstOrDefault(); + var channelIdString = channel.Id.ToString("N"); + var currentProgram = programs.FirstOrDefault(i => string.Equals(i.ChannelId, channelIdString)); - if (currentProgram != null) - { - dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, options, user); + if (currentProgram != null) + { + dto.CurrentProgram = _dtoService.GetBaseItemDto(currentProgram, options, user); + } } }