2019-01-13 20:01:16 +00:00
using System ;
2019-02-26 19:47:23 +00:00
using System.Collections.Generic ;
2019-01-13 19:24:58 +00:00
using System.Linq ;
2019-11-17 22:05:39 +00:00
using MediaBrowser.Controller.Configuration ;
2019-01-13 19:24:58 +00:00
using MediaBrowser.Controller.Entities ;
2014-11-09 18:24:57 +00:00
using MediaBrowser.Controller.Library ;
using MediaBrowser.Controller.Net ;
2017-12-03 22:15:21 +00:00
using MediaBrowser.Model.Dto ;
2019-01-13 19:24:58 +00:00
using MediaBrowser.Model.Querying ;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services ;
2019-11-17 22:05:39 +00:00
using Microsoft.Extensions.Logging ;
2014-11-09 18:24:57 +00:00
namespace MediaBrowser.Api
{
[Route("/Items/Filters", "GET", Summary = "Gets branding configuration")]
2017-12-03 22:15:21 +00:00
public class GetQueryFiltersLegacy : IReturn < QueryFiltersLegacy >
{
/// <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 17:26:21 +00:00
public Guid UserId { get ; set ; }
2017-12-03 22:15:21 +00:00
[ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ParentId { get ; set ; }
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
[ApiMember(Name = "MediaTypes", Description = "Optional filter by MediaType. Allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string MediaTypes { get ; set ; }
public string [ ] GetMediaTypes ( )
{
return ( MediaTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
public string [ ] GetIncludeItemTypes ( )
{
return ( IncludeItemTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
}
[Route("/Items/Filters2", "GET", Summary = "Gets branding configuration")]
2014-11-09 18:24:57 +00:00
public class GetQueryFilters : IReturn < QueryFilters >
{
/// <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 17:26:21 +00:00
public Guid UserId { get ; set ; }
2014-11-09 18:24:57 +00:00
[ApiMember(Name = "ParentId", Description = "Specify this to localize the search to a specific item or folder. Omit to use the root", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ParentId { get ; set ; }
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
[ApiMember(Name = "MediaTypes", Description = "Optional filter by MediaType. Allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string MediaTypes { get ; set ; }
public string [ ] GetMediaTypes ( )
{
return ( MediaTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
public string [ ] GetIncludeItemTypes ( )
{
return ( IncludeItemTypes ? ? string . Empty ) . Split ( new [ ] { ',' } , StringSplitOptions . RemoveEmptyEntries ) ;
}
2017-12-03 22:15:21 +00:00
public bool? IsAiring { get ; set ; }
public bool? IsMovie { get ; set ; }
public bool? IsSports { get ; set ; }
public bool? IsKids { get ; set ; }
public bool? IsNews { get ; set ; }
public bool? IsSeries { get ; set ; }
2018-09-12 17:26:21 +00:00
public bool? Recursive { get ; set ; }
2014-11-09 18:24:57 +00:00
}
[Authenticated]
public class FilterService : BaseApiService
{
private readonly ILibraryManager _libraryManager ;
private readonly IUserManager _userManager ;
2019-11-17 22:05:39 +00:00
public FilterService (
ILogger < FilterService > logger ,
IServerConfigurationManager serverConfigurationManager ,
IHttpResultFactory httpResultFactory ,
ILibraryManager libraryManager ,
IUserManager userManager )
: base ( logger , serverConfigurationManager , httpResultFactory )
2014-11-09 18:24:57 +00:00
{
_libraryManager = libraryManager ;
_userManager = userManager ;
}
2017-05-26 06:48:54 +00:00
public object Get ( GetQueryFilters request )
2014-11-09 18:24:57 +00:00
{
var parentItem = string . IsNullOrEmpty ( request . ParentId ) ? null : _libraryManager . GetItemById ( request . ParentId ) ;
2018-09-12 17:26:21 +00:00
var user = ! request . UserId . Equals ( Guid . Empty ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-11-09 18:24:57 +00:00
2017-12-03 22:15:21 +00:00
if ( string . Equals ( request . IncludeItemTypes , "BoxSet" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "Playlist" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , typeof ( Trailer ) . Name , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "Program" , StringComparison . OrdinalIgnoreCase ) )
{
parentItem = null ;
}
var filters = new QueryFilters ( ) ;
var genreQuery = new InternalItemsQuery ( user )
{
IncludeItemTypes = request . GetIncludeItemTypes ( ) ,
DtoOptions = new Controller . Dto . DtoOptions
{
Fields = new ItemFields [ ] { } ,
EnableImages = false ,
EnableUserData = false
} ,
IsAiring = request . IsAiring ,
IsMovie = request . IsMovie ,
IsSports = request . IsSports ,
IsKids = request . IsKids ,
IsNews = request . IsNews ,
IsSeries = request . IsSeries
} ;
2018-09-12 17:26:21 +00:00
// Non recursive not yet supported for library folders
if ( ( request . Recursive ? ? true ) | | parentItem is UserView | | parentItem is ICollectionFolder )
{
2020-04-05 17:00:35 +00:00
genreQuery . AncestorIds = parentItem = = null ? Array . Empty < Guid > ( ) : new [ ] { parentItem . Id } ;
2018-09-12 17:26:21 +00:00
}
else
{
genreQuery . Parent = parentItem ;
}
2017-12-03 22:15:21 +00:00
if ( string . Equals ( request . IncludeItemTypes , "MusicAlbum" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "MusicVideo" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "MusicArtist" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "Audio" , StringComparison . OrdinalIgnoreCase ) )
{
2018-09-12 17:26:21 +00:00
filters . Genres = _libraryManager . GetMusicGenres ( genreQuery ) . Items . Select ( i = > new NameGuidPair
2017-12-03 22:15:21 +00:00
{
Name = i . Item1 . Name ,
2018-09-12 17:26:21 +00:00
Id = i . Item1 . Id
2017-12-03 22:15:21 +00:00
} ) . ToArray ( ) ;
}
else
{
2018-09-12 17:26:21 +00:00
filters . Genres = _libraryManager . GetGenres ( genreQuery ) . Items . Select ( i = > new NameGuidPair
2017-12-03 22:15:21 +00:00
{
Name = i . Item1 . Name ,
2018-09-12 17:26:21 +00:00
Id = i . Item1 . Id
2017-12-03 22:15:21 +00:00
} ) . ToArray ( ) ;
}
return ToOptimizedResult ( filters ) ;
}
public object Get ( GetQueryFiltersLegacy request )
{
var parentItem = string . IsNullOrEmpty ( request . ParentId ) ? null : _libraryManager . GetItemById ( request . ParentId ) ;
2018-09-12 17:26:21 +00:00
var user = ! request . UserId . Equals ( Guid . Empty ) ? _userManager . GetUserById ( request . UserId ) : null ;
2017-12-03 22:15:21 +00:00
if ( string . Equals ( request . IncludeItemTypes , "BoxSet" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "Playlist" , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , typeof ( Trailer ) . Name , StringComparison . OrdinalIgnoreCase ) | |
string . Equals ( request . IncludeItemTypes , "Program" , StringComparison . OrdinalIgnoreCase ) )
{
parentItem = null ;
}
2014-11-09 18:24:57 +00:00
var item = string . IsNullOrEmpty ( request . ParentId ) ?
2018-09-12 17:26:21 +00:00
user = = null ? _libraryManager . RootFolder : _libraryManager . GetUserRootFolder ( ) :
2014-11-09 18:24:57 +00:00
parentItem ;
2017-08-01 16:45:57 +00:00
var result = ( ( Folder ) item ) . GetItemList ( GetItemsQuery ( request , user ) ) ;
2014-11-09 18:24:57 +00:00
2017-12-03 22:15:21 +00:00
var filters = GetFilters ( result ) ;
return ToOptimizedResult ( filters ) ;
2014-11-09 18:24:57 +00:00
}
2019-02-26 19:47:23 +00:00
private QueryFiltersLegacy GetFilters ( IReadOnlyCollection < BaseItem > items )
2014-11-09 18:24:57 +00:00
{
2017-12-03 22:15:21 +00:00
var result = new QueryFiltersLegacy ( ) ;
2014-11-09 18:24:57 +00:00
result . Years = items . Select ( i = > i . ProductionYear ? ? - 1 )
. Where ( i = > i > 0 )
. Distinct ( )
. OrderBy ( i = > i )
. ToArray ( ) ;
result . Genres = items . SelectMany ( i = > i . Genres )
2015-04-09 21:11:57 +00:00
. DistinctNames ( )
2014-11-09 18:24:57 +00:00
. OrderBy ( i = > i )
. ToArray ( ) ;
2016-06-02 17:43:29 +00:00
result . Tags = items
2014-11-09 18:24:57 +00:00
. SelectMany ( i = > i . Tags )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. OrderBy ( i = > i )
. ToArray ( ) ;
result . OfficialRatings = items
. Select ( i = > i . OfficialRating )
. Where ( i = > ! string . IsNullOrWhiteSpace ( i ) )
. Distinct ( StringComparer . OrdinalIgnoreCase )
. OrderBy ( i = > i )
. ToArray ( ) ;
return result ;
}
2017-12-03 22:15:21 +00:00
private InternalItemsQuery GetItemsQuery ( GetQueryFiltersLegacy request , User user )
2014-11-09 18:24:57 +00:00
{
var query = new InternalItemsQuery
{
User = user ,
MediaTypes = request . GetMediaTypes ( ) ,
IncludeItemTypes = request . GetIncludeItemTypes ( ) ,
2016-05-18 05:34:10 +00:00
Recursive = true ,
2016-10-08 05:57:38 +00:00
EnableTotalRecordCount = false ,
2016-10-10 18:18:28 +00:00
DtoOptions = new Controller . Dto . DtoOptions
{
2020-04-05 17:00:35 +00:00
Fields = new [ ] { ItemFields . Genres , ItemFields . Tags } ,
2016-10-10 18:18:28 +00:00
EnableImages = false ,
EnableUserData = false
}
2014-11-09 18:24:57 +00:00
} ;
return query ;
}
}
}