2019-01-13 21:01:16 +01:00
using System ;
2014-05-03 06:20:04 +02:00
using System.Collections.Generic ;
using System.Linq ;
2014-03-18 02:45:41 +01:00
using System.Threading ;
2014-08-17 07:38:13 +02:00
using System.Threading.Tasks ;
2020-07-01 03:44:41 +02:00
using Jellyfin.Data.Enums ;
2017-09-04 21:28:22 +02:00
using MediaBrowser.Api.UserLibrary ;
2019-01-13 20:24:58 +01:00
using MediaBrowser.Controller.Channels ;
2020-03-24 16:12:06 +01:00
using MediaBrowser.Controller.Configuration ;
2018-09-12 19:26:21 +02:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
2019-01-13 20:24:58 +01:00
using MediaBrowser.Controller.Net ;
using MediaBrowser.Model.Channels ;
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Querying ;
using MediaBrowser.Model.Services ;
2019-11-17 23:05:39 +01:00
using Microsoft.Extensions.Logging ;
2014-03-18 02:45:41 +01:00
namespace MediaBrowser.Api
{
2014-03-26 17:10:46 +01:00
[Route("/Channels", "GET", Summary = "Gets available channels")]
2014-03-18 02:45:41 +01:00
public class GetChannels : IReturn < QueryResult < BaseItemDto > >
{
2014-05-03 06:20:04 +02:00
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2018-09-12 19:26:21 +02:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid UserId { get ; set ; }
2014-03-18 02:45:41 +01:00
2014-05-03 06:20:04 +02:00
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
2014-03-18 02:45:41 +01:00
public int? StartIndex { get ; set ; }
2014-05-03 06:20:04 +02:00
/// <summary>
2020-06-16 00:37:52 +02:00
/// The maximum number of items to return.
2014-05-03 06:20:04 +02:00
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
2014-03-18 02:45:41 +01:00
public int? Limit { get ; set ; }
2014-06-17 18:03:14 +02:00
[ApiMember(Name = "SupportsLatestItems", Description = "Optional. Filter by channels that support getting latest items.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? SupportsLatestItems { get ; set ; }
2014-06-23 18:05:19 +02:00
2018-09-12 19:26:21 +02:00
public bool? SupportsMediaDeletion { get ; set ; }
2014-06-23 18:05:19 +02:00
/// <summary>
/// Gets or sets a value indicating whether this instance is favorite.
/// </summary>
/// <value><c>null</c> if [is favorite] contains no value, <c>true</c> if [is favorite]; otherwise, <c>false</c>.</value>
public bool? IsFavorite { get ; set ; }
2014-03-18 02:45:41 +01:00
}
2014-05-27 19:09:48 +02:00
[Route("/Channels/{Id}/Features", "GET", Summary = "Gets features for a channel")]
public class GetChannelFeatures : IReturn < ChannelFeatures >
{
[ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
2014-06-02 21:32:41 +02:00
[Route("/Channels/Features", "GET", Summary = "Gets features for a channel")]
2017-08-19 21:43:35 +02:00
public class GetAllChannelFeatures : IReturn < ChannelFeatures [ ] >
2014-06-02 21:32:41 +02:00
{
}
2014-03-26 17:10:46 +01:00
[Route("/Channels/{Id}/Items", "GET", Summary = "Gets channel items")]
2014-06-07 23:06:01 +02:00
public class GetChannelItems : IReturn < QueryResult < BaseItemDto > > , IHasItemFields
2014-03-18 18:05:57 +01:00
{
2014-05-27 19:09:48 +02:00
[ApiMember(Name = "Id", Description = "Channel Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2014-03-18 18:05:57 +01:00
public string Id { get ; set ; }
2014-05-27 19:09:48 +02:00
[ApiMember(Name = "FolderId", Description = "Folder Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2014-05-23 16:09:58 +02:00
public string FolderId { get ; set ; }
2014-03-18 18:05:57 +01:00
2014-05-03 06:20:04 +02:00
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
2014-05-27 19:09:48 +02:00
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2018-09-12 19:26:21 +02:00
public Guid UserId { get ; set ; }
2014-03-18 18:05:57 +01:00
2014-05-03 06:20:04 +02:00
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
2014-03-18 18:05:57 +01:00
public int? StartIndex { get ; set ; }
2014-05-03 06:20:04 +02:00
/// <summary>
2020-06-16 00:37:52 +02:00
/// The maximum number of items to return.
2014-05-03 06:20:04 +02:00
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
2014-03-18 18:05:57 +01:00
public int? Limit { get ; set ; }
2014-05-03 06:20:04 +02:00
[ApiMember(Name = "SortOrder", Description = "Sort Order - Ascending,Descending", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2017-09-04 21:28:22 +02:00
public string SortOrder { get ; set ; }
2014-05-03 06:20:04 +02:00
2014-05-07 04:28:19 +02:00
[ApiMember(Name = "Filters", Description = "Optional. Specify additional filters to apply. This allows multiple, comma delimeted. Options: IsFolder, IsNotFolder, IsUnplayed, IsPlayed, IsFavorite, IsResumable, Likes, Dislikes", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2014-05-03 06:20:04 +02:00
public string Filters { get ; set ; }
[ApiMember(Name = "SortBy", Description = "Optional. Specify one or more sort orders, comma delimeted. Options: Album, AlbumArtist, Artist, Budget, CommunityRating, CriticRating, DateCreated, DatePlayed, PlayCount, PremiereDate, ProductionYear, SortName, Random, Revenue, Runtime", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string SortBy { get ; set ; }
2017-04-29 08:22:33 +02:00
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2014-06-07 23:06:01 +02:00
public string Fields { get ; set ; }
2017-10-03 20:39:37 +02:00
2014-05-03 06:20:04 +02:00
/// <summary>
/// Gets the filters.
/// </summary>
/// <returns>IEnumerable{ItemFilter}.</returns>
public IEnumerable < ItemFilter > GetFilters ( )
{
var val = Filters ;
2020-04-05 20:44:14 +02:00
return string . IsNullOrEmpty ( val )
2020-04-05 23:58:39 +02:00
? Array . Empty < ItemFilter > ( )
: val . Split ( ',' ) . Select ( v = > Enum . Parse < ItemFilter > ( v , true ) ) ;
2014-05-03 06:20:04 +02:00
}
2017-09-04 21:28:22 +02:00
/// <summary>
/// Gets the order by.
/// </summary>
/// <returns>IEnumerable{ItemSortBy}.</returns>
2018-09-12 19:26:21 +02:00
public ValueTuple < string , SortOrder > [ ] GetOrderBy ( )
2017-09-04 21:28:22 +02:00
{
return BaseItemsRequest . GetOrderBy ( SortBy , SortOrder ) ;
}
2014-03-18 18:05:57 +01:00
}
2014-06-16 01:30:04 +02:00
[Route("/Channels/Items/Latest", "GET", Summary = "Gets channel items")]
public class GetLatestChannelItems : IReturn < QueryResult < BaseItemDto > > , IHasItemFields
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2018-09-12 19:26:21 +02:00
public Guid UserId { get ; set ; }
2014-06-16 01:30:04 +02:00
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? StartIndex { get ; set ; }
/// <summary>
2020-06-16 00:37:52 +02:00
/// The maximum number of items to return.
2014-06-16 01:30:04 +02:00
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? Limit { get ; set ; }
[ApiMember(Name = "Filters", Description = "Optional. Specify additional filters to apply. This allows multiple, comma delimeted. Options: IsFolder, IsNotFolder, IsUnplayed, IsPlayed, IsFavorite, IsResumable, Likes, Dislikes", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string Filters { get ; set ; }
2017-04-29 08:22:33 +02:00
[ApiMember(Name = "Fields", Description = "Optional. Specify additional fields of information to return in the output. This allows multiple, comma delimeted. Options: Budget, Chapters, DateCreated, Genres, HomePageUrl, IndexOptions, MediaStreams, Overview, ParentId, Path, People, ProviderIds, PrimaryImageAspectRatio, Revenue, SortName, Studios, Taglines", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
2014-06-16 01:30:04 +02:00
public string Fields { get ; set ; }
2014-06-17 18:03:14 +02:00
[ApiMember(Name = "ChannelIds", Description = "Optional. Specify one or more channel id's, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string ChannelIds { get ; set ; }
2017-10-03 20:39:37 +02:00
2014-06-16 01:30:04 +02:00
/// <summary>
/// Gets the filters.
/// </summary>
/// <returns>IEnumerable{ItemFilter}.</returns>
public IEnumerable < ItemFilter > GetFilters ( )
{
2020-04-05 23:58:39 +02:00
return string . IsNullOrEmpty ( Filters )
? Array . Empty < ItemFilter > ( )
: Filters . Split ( ',' ) . Select ( v = > Enum . Parse < ItemFilter > ( v , true ) ) ;
2014-06-16 01:30:04 +02:00
}
}
2017-10-03 20:39:37 +02:00
2014-07-02 06:57:18 +02:00
[Authenticated]
2014-03-18 02:45:41 +01:00
public class ChannelService : BaseApiService
{
private readonly IChannelManager _channelManager ;
2018-09-12 19:26:21 +02:00
private IUserManager _userManager ;
2014-03-18 02:45:41 +01:00
2019-11-17 23:05:39 +01:00
public ChannelService (
ILogger < ChannelService > logger ,
IServerConfigurationManager serverConfigurationManager ,
IHttpResultFactory httpResultFactory ,
IChannelManager channelManager ,
IUserManager userManager )
: base ( logger , serverConfigurationManager , httpResultFactory )
2014-03-18 02:45:41 +01:00
{
_channelManager = channelManager ;
2018-09-12 19:26:21 +02:00
_userManager = userManager ;
2014-03-18 02:45:41 +01:00
}
2014-06-02 21:32:41 +02:00
public object Get ( GetAllChannelFeatures request )
{
2017-08-19 21:43:35 +02:00
var result = _channelManager . GetAllChannelFeatures ( ) ;
2014-06-02 21:32:41 +02:00
return ToOptimizedResult ( result ) ;
}
2014-05-27 19:09:48 +02:00
public object Get ( GetChannelFeatures request )
{
var result = _channelManager . GetChannelFeatures ( request . Id ) ;
return ToOptimizedResult ( result ) ;
}
2018-09-12 19:26:21 +02:00
public object Get ( GetChannels request )
2014-06-07 21:46:24 +02:00
{
2018-09-12 19:26:21 +02:00
var result = _channelManager . GetChannels ( new ChannelQuery
2014-03-18 02:45:41 +01:00
{
Limit = request . Limit ,
StartIndex = request . StartIndex ,
UserId = request . UserId ,
2014-06-23 18:05:19 +02:00
SupportsLatestItems = request . SupportsLatestItems ,
2018-09-12 19:26:21 +02:00
SupportsMediaDeletion = request . SupportsMediaDeletion ,
2014-06-23 18:05:19 +02:00
IsFavorite = request . IsFavorite
2018-09-12 19:26:21 +02:00
} ) ;
2014-03-18 02:45:41 +01:00
return ToOptimizedResult ( result ) ;
}
2014-03-18 18:05:57 +01:00
2014-08-17 07:38:13 +02:00
public async Task < object > Get ( GetChannelItems request )
2014-03-18 18:05:57 +01:00
{
2018-09-12 19:26:21 +02:00
var user = request . UserId . Equals ( Guid . Empty )
? null
: _userManager . GetUserById ( request . UserId ) ;
var query = new InternalItemsQuery ( user )
2014-03-18 18:05:57 +01:00
{
Limit = request . Limit ,
StartIndex = request . StartIndex ,
2020-04-05 19:00:35 +02:00
ChannelIds = new [ ] { new Guid ( request . Id ) } ,
2018-09-12 19:26:21 +02:00
ParentId = string . IsNullOrWhiteSpace ( request . FolderId ) ? Guid . Empty : new Guid ( request . FolderId ) ,
2017-09-04 21:28:22 +02:00
OrderBy = request . GetOrderBy ( ) ,
2018-09-12 19:26:21 +02:00
DtoOptions = new Controller . Dto . DtoOptions
{
Fields = request . GetItemFields ( )
}
} ;
foreach ( var filter in request . GetFilters ( ) )
{
switch ( filter )
{
case ItemFilter . Dislikes :
query . IsLiked = false ;
break ;
case ItemFilter . IsFavorite :
query . IsFavorite = true ;
break ;
case ItemFilter . IsFavoriteOrLikes :
query . IsFavoriteOrLiked = true ;
break ;
case ItemFilter . IsFolder :
query . IsFolder = true ;
break ;
case ItemFilter . IsNotFolder :
query . IsFolder = false ;
break ;
case ItemFilter . IsPlayed :
query . IsPlayed = true ;
break ;
case ItemFilter . IsResumable :
query . IsResumable = true ;
break ;
case ItemFilter . IsUnplayed :
query . IsPlayed = false ;
break ;
case ItemFilter . Likes :
query . IsLiked = true ;
break ;
}
}
2014-03-18 18:05:57 +01:00
2018-09-12 19:26:21 +02:00
var result = await _channelManager . GetChannelItems ( query , CancellationToken . None ) . ConfigureAwait ( false ) ;
2014-03-18 18:05:57 +01:00
return ToOptimizedResult ( result ) ;
}
2014-06-16 01:30:04 +02:00
2014-08-17 07:38:13 +02:00
public async Task < object > Get ( GetLatestChannelItems request )
2014-06-16 01:30:04 +02:00
{
2018-09-12 19:26:21 +02:00
var user = request . UserId . Equals ( Guid . Empty )
? null
: _userManager . GetUserById ( request . UserId ) ;
var query = new InternalItemsQuery ( user )
2014-06-16 01:30:04 +02:00
{
Limit = request . Limit ,
StartIndex = request . StartIndex ,
2018-09-12 19:26:21 +02:00
ChannelIds = ( request . ChannelIds ? ? string . Empty ) . Split ( ',' ) . Where ( i = > ! string . IsNullOrWhiteSpace ( i ) ) . Select ( i = > new Guid ( i ) ) . ToArray ( ) ,
DtoOptions = new Controller . Dto . DtoOptions
{
Fields = request . GetItemFields ( )
}
} ;
foreach ( var filter in request . GetFilters ( ) )
{
switch ( filter )
{
case ItemFilter . Dislikes :
query . IsLiked = false ;
break ;
case ItemFilter . IsFavorite :
query . IsFavorite = true ;
break ;
case ItemFilter . IsFavoriteOrLikes :
query . IsFavoriteOrLiked = true ;
break ;
case ItemFilter . IsFolder :
query . IsFolder = true ;
break ;
case ItemFilter . IsNotFolder :
query . IsFolder = false ;
break ;
case ItemFilter . IsPlayed :
query . IsPlayed = true ;
break ;
case ItemFilter . IsResumable :
query . IsResumable = true ;
break ;
case ItemFilter . IsUnplayed :
query . IsPlayed = false ;
break ;
case ItemFilter . Likes :
query . IsLiked = true ;
break ;
}
}
2014-06-16 01:30:04 +02:00
2018-09-12 19:26:21 +02:00
var result = await _channelManager . GetLatestChannelItems ( query , CancellationToken . None ) . ConfigureAwait ( false ) ;
2014-06-16 01:30:04 +02:00
return ToOptimizedResult ( result ) ;
}
2014-03-18 02:45:41 +01:00
}
}