2019-01-13 20:01:16 +00:00
using System ;
2019-02-28 22:22:57 +00:00
using System.Globalization ;
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.Dto ;
2015-04-15 21:59:20 +00:00
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Library ;
2019-01-13 19:24:58 +00:00
using MediaBrowser.Controller.Net ;
2015-04-15 21:59:20 +00:00
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Library ;
using MediaBrowser.Model.Querying ;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services ;
2019-10-20 19:12:03 +00:00
using Microsoft.Extensions.Logging ;
2015-04-15 21:59:20 +00:00
namespace MediaBrowser.Api.UserLibrary
{
[Route("/Users/{UserId}/Views", "GET")]
public class GetUserViews : IReturn < QueryResult < BaseItemDto > >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 17:26:21 +00:00
public Guid UserId { get ; set ; }
2015-04-15 21:59:20 +00:00
2018-09-12 17:26:21 +00:00
[ApiMember(Name = "IncludeExternalContent", Description = "Whether or not to include external views such as channels or live tv", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "GET")]
2015-04-15 21:59:20 +00:00
public bool? IncludeExternalContent { get ; set ; }
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
public bool IncludeHidden { get ; set ; }
2015-11-17 18:17:52 +00:00
public string PresetViews { get ; set ; }
2015-04-15 21:59:20 +00:00
}
2015-09-15 18:09:44 +00:00
[Route("/Users/{UserId}/GroupingOptions", "GET")]
2017-08-19 19:43:35 +00:00
public class GetGroupingOptions : IReturn < SpecialViewOption [ ] >
2015-09-15 18:09:44 +00:00
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2018-09-12 17:26:21 +00:00
public Guid UserId { get ; set ; }
2015-09-15 18:09:44 +00:00
}
2015-04-15 21:59:20 +00:00
public class UserViewsService : BaseApiService
{
private readonly IUserManager _userManager ;
private readonly IUserViewManager _userViewManager ;
private readonly IDtoService _dtoService ;
2016-11-10 14:41:24 +00:00
private readonly IAuthorizationContext _authContext ;
2018-09-12 17:26:21 +00:00
private readonly ILibraryManager _libraryManager ;
2015-04-15 21:59:20 +00:00
2019-10-20 19:12:03 +00:00
public UserViewsService (
2019-11-17 22:05:39 +00:00
ILogger < UserViewsService > logger ,
IServerConfigurationManager serverConfigurationManager ,
IHttpResultFactory httpResultFactory ,
2019-10-20 19:12:03 +00:00
IUserManager userManager ,
IUserViewManager userViewManager ,
IDtoService dtoService ,
IAuthorizationContext authContext ,
ILibraryManager libraryManager )
2019-11-17 22:05:39 +00:00
: base ( logger , serverConfigurationManager , httpResultFactory )
2015-04-15 21:59:20 +00:00
{
_userManager = userManager ;
_userViewManager = userViewManager ;
_dtoService = dtoService ;
2016-11-10 14:41:24 +00:00
_authContext = authContext ;
2018-09-12 17:26:21 +00:00
_libraryManager = libraryManager ;
2015-04-15 21:59:20 +00:00
}
2018-09-12 17:26:21 +00:00
public object Get ( GetUserViews request )
2015-04-15 21:59:20 +00:00
{
var query = new UserViewQuery
{
UserId = request . UserId
} ;
if ( request . IncludeExternalContent . HasValue )
{
query . IncludeExternalContent = request . IncludeExternalContent . Value ;
}
2020-06-15 21:43:52 +00:00
2018-09-12 17:26:21 +00:00
query . IncludeHidden = request . IncludeHidden ;
2015-04-15 21:59:20 +00:00
2015-11-17 18:17:52 +00:00
if ( ! string . IsNullOrWhiteSpace ( request . PresetViews ) )
{
query . PresetViews = request . PresetViews . Split ( ',' ) ;
}
2016-11-10 14:41:24 +00:00
var app = _authContext . GetAuthorizationInfo ( Request ) . Client ? ? string . Empty ;
2015-11-17 18:17:52 +00:00
if ( app . IndexOf ( "emby rt" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
2018-09-12 17:26:21 +00:00
query . PresetViews = new [ ] { CollectionType . Movies , CollectionType . TvShows } ;
2015-11-17 18:17:52 +00:00
}
2018-09-12 17:26:21 +00:00
var folders = _userViewManager . GetUserViews ( query ) ;
2015-04-15 21:59:20 +00:00
2016-11-10 14:41:24 +00:00
var dtoOptions = GetDtoOptions ( _authContext , request ) ;
2017-08-19 19:43:35 +00:00
var fields = dtoOptions . Fields . ToList ( ) ;
fields . Add ( ItemFields . PrimaryImageAspectRatio ) ;
fields . Add ( ItemFields . DisplayPreferencesId ) ;
fields . Remove ( ItemFields . BasicSyncInfo ) ;
2018-12-28 15:48:26 +00:00
dtoOptions . Fields = fields . ToArray ( ) ;
2015-04-15 21:59:20 +00:00
var user = _userManager . GetUserById ( request . UserId ) ;
var dtos = folders . Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user ) )
. ToArray ( ) ;
var result = new QueryResult < BaseItemDto >
{
Items = dtos ,
TotalRecordCount = dtos . Length
} ;
return ToOptimizedResult ( result ) ;
}
2017-05-26 06:48:54 +00:00
public object Get ( GetGroupingOptions request )
2015-09-15 18:09:44 +00:00
{
var user = _userManager . GetUserById ( request . UserId ) ;
2018-09-12 17:26:21 +00:00
var list = _libraryManager . GetUserRootFolder ( )
2015-09-15 18:09:44 +00:00
. GetChildren ( user , true )
. OfType < Folder > ( )
2015-11-18 05:49:20 +00:00
. Where ( UserView . IsEligibleForGrouping )
2015-09-15 18:09:44 +00:00
. Select ( i = > new SpecialViewOption
{
Name = i . Name ,
2019-02-28 22:22:57 +00:00
Id = i . Id . ToString ( "N" , CultureInfo . InvariantCulture )
2015-09-15 18:09:44 +00:00
} )
. OrderBy ( i = > i . Name )
2017-08-19 19:43:35 +00:00
. ToArray ( ) ;
2015-09-15 18:09:44 +00:00
return ToOptimizedResult ( list ) ;
}
2015-04-15 21:59:20 +00:00
}
class SpecialViewOption
{
public string Name { get ; set ; }
2020-06-15 21:43:52 +00:00
2015-04-15 21:59:20 +00:00
public string Id { get ; set ; }
}
}