2020-08-03 18:01:24 +00:00
using System ;
2020-04-19 18:26:38 +00:00
using System.ComponentModel.DataAnnotations ;
2020-06-21 00:02:07 +00:00
using System.Diagnostics.CodeAnalysis ;
2020-08-03 18:01:24 +00:00
using System.Globalization ;
using System.Linq ;
2020-06-22 13:44:11 +00:00
using Jellyfin.Api.Constants ;
2020-08-03 18:01:24 +00:00
using Jellyfin.Data.Entities ;
using Jellyfin.Data.Enums ;
using MediaBrowser.Controller ;
2020-04-19 18:26:38 +00:00
using MediaBrowser.Model.Entities ;
2020-04-23 16:07:21 +00:00
using Microsoft.AspNetCore.Authorization ;
2020-04-19 18:26:38 +00:00
using Microsoft.AspNetCore.Http ;
2020-04-19 18:06:18 +00:00
using Microsoft.AspNetCore.Mvc ;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// Display Preferences Controller.
/// </summary>
2020-06-22 13:44:11 +00:00
[Authorize(Policy = Policies.DefaultAuthorization)]
2020-04-19 18:06:18 +00:00
public class DisplayPreferencesController : BaseJellyfinApiController
{
2020-08-03 18:01:24 +00:00
private readonly IDisplayPreferencesManager _displayPreferencesManager ;
2020-04-19 18:26:38 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="DisplayPreferencesController"/> class.
/// </summary>
2020-08-03 18:01:24 +00:00
/// <param name="displayPreferencesManager">Instance of <see cref="IDisplayPreferencesManager"/> interface.</param>
public DisplayPreferencesController ( IDisplayPreferencesManager displayPreferencesManager )
2020-04-19 18:26:38 +00:00
{
2020-08-03 18:01:24 +00:00
_displayPreferencesManager = displayPreferencesManager ;
2020-04-19 18:26:38 +00:00
}
/// <summary>
2020-04-19 18:30:10 +00:00
/// Get Display Preferences.
2020-04-19 18:26:38 +00:00
/// </summary>
/// <param name="displayPreferencesId">Display preferences id.</param>
/// <param name="userId">User id.</param>
/// <param name="client">Client.</param>
2020-05-02 23:06:29 +00:00
/// <response code="200">Display preferences retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the display preferences on success, or a <see cref="NotFoundResult"/> if the display preferences could not be found.</returns>
2020-06-21 00:02:07 +00:00
[HttpGet("{displayPreferencesId}")]
2020-04-21 20:01:47 +00:00
[ProducesResponseType(StatusCodes.Status200OK)]
2020-08-03 18:01:24 +00:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
public ActionResult < DisplayPreferencesDto > GetDisplayPreferences (
2020-09-08 00:45:06 +00:00
[FromRoute, Required] string displayPreferencesId ,
2020-09-08 00:46:14 +00:00
[FromQuery, Required] Guid userId ,
2020-09-09 20:28:30 +00:00
[FromQuery, Required] string client )
2020-04-19 18:26:38 +00:00
{
2020-12-04 16:16:38 +00:00
_ = Guid . TryParse ( displayPreferencesId , out var itemId ) ;
2020-08-03 18:01:24 +00:00
var displayPreferences = _displayPreferencesManager . GetDisplayPreferences ( userId , client ) ;
2020-12-04 16:16:38 +00:00
var itemPreferences = _displayPreferencesManager . GetItemDisplayPreferences ( displayPreferences . UserId , itemId , displayPreferences . Client ) ;
itemPreferences . ItemId = itemId ;
2020-08-03 18:01:24 +00:00
var dto = new DisplayPreferencesDto
{
Client = displayPreferences . Client ,
Id = displayPreferences . UserId . ToString ( ) ,
ViewType = itemPreferences . ViewType . ToString ( ) ,
SortBy = itemPreferences . SortBy ,
SortOrder = itemPreferences . SortOrder ,
IndexBy = displayPreferences . IndexBy ? . ToString ( ) ,
RememberIndexing = itemPreferences . RememberIndexing ,
RememberSorting = itemPreferences . RememberSorting ,
ScrollDirection = displayPreferences . ScrollDirection ,
ShowBackdrop = displayPreferences . ShowBackdrop ,
ShowSidebar = displayPreferences . ShowSidebar
} ;
foreach ( var homeSection in displayPreferences . HomeSections )
{
dto . CustomPrefs [ "homesection" + homeSection . Order ] = homeSection . Type . ToString ( ) . ToLowerInvariant ( ) ;
}
foreach ( var itemDisplayPreferences in _displayPreferencesManager . ListItemDisplayPreferences ( displayPreferences . UserId , displayPreferences . Client ) )
{
dto . CustomPrefs [ "landing-" + itemDisplayPreferences . ItemId ] = itemDisplayPreferences . ViewType . ToString ( ) . ToLowerInvariant ( ) ;
}
dto . CustomPrefs [ "chromecastVersion" ] = displayPreferences . ChromecastVersion . ToString ( ) . ToLowerInvariant ( ) ;
dto . CustomPrefs [ "skipForwardLength" ] = displayPreferences . SkipForwardLength . ToString ( CultureInfo . InvariantCulture ) ;
dto . CustomPrefs [ "skipBackLength" ] = displayPreferences . SkipBackwardLength . ToString ( CultureInfo . InvariantCulture ) ;
dto . CustomPrefs [ "enableNextVideoInfoOverlay" ] = displayPreferences . EnableNextVideoInfoOverlay . ToString ( CultureInfo . InvariantCulture ) ;
dto . CustomPrefs [ "tvhome" ] = displayPreferences . TvHome ;
2020-12-03 20:51:12 +00:00
// Load all custom display preferences
var customDisplayPreferences = _displayPreferencesManager . ListCustomItemDisplayPreferences ( displayPreferences . UserId , displayPreferences . Client ) ;
if ( customDisplayPreferences ! = null )
{
foreach ( var ( key , value ) in customDisplayPreferences )
{
2020-12-03 21:35:18 +00:00
dto . CustomPrefs . TryAdd ( key , value ) ;
2020-12-03 20:51:12 +00:00
}
}
2020-11-02 08:23:29 +00:00
// This will essentially be a noop if no changes have been made, but new prefs must be saved at least.
_displayPreferencesManager . SaveChanges ( ) ;
2020-08-03 18:01:24 +00:00
return dto ;
2020-04-19 18:26:38 +00:00
}
/// <summary>
2020-04-19 18:30:10 +00:00
/// Update Display Preferences.
2020-04-19 18:26:38 +00:00
/// </summary>
/// <param name="displayPreferencesId">Display preferences id.</param>
/// <param name="userId">User Id.</param>
/// <param name="client">Client.</param>
/// <param name="displayPreferences">New Display Preferences object.</param>
2020-06-21 00:02:07 +00:00
/// <response code="204">Display preferences updated.</response>
2020-06-21 15:33:34 +00:00
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
2020-06-21 00:02:07 +00:00
[HttpPost("{displayPreferencesId}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
2020-04-21 20:01:47 +00:00
public ActionResult UpdateDisplayPreferences (
2020-09-08 00:45:06 +00:00
[FromRoute, Required] string displayPreferencesId ,
2020-08-06 14:17:45 +00:00
[FromQuery, Required] Guid userId ,
2020-09-09 20:28:30 +00:00
[FromQuery, Required] string client ,
2020-08-06 14:17:45 +00:00
[FromBody, Required] DisplayPreferencesDto displayPreferences )
2020-04-19 18:26:38 +00:00
{
2020-08-03 18:01:24 +00:00
HomeSectionType [ ] defaults =
{
HomeSectionType . SmallLibraryTiles ,
HomeSectionType . Resume ,
HomeSectionType . ResumeAudio ,
HomeSectionType . LiveTv ,
HomeSectionType . NextUp ,
HomeSectionType . LatestMedia , HomeSectionType . None ,
} ;
2020-12-04 16:16:38 +00:00
_ = Guid . TryParse ( displayPreferencesId , out var itemId ) ;
2020-08-03 18:01:24 +00:00
var existingDisplayPreferences = _displayPreferencesManager . GetDisplayPreferences ( userId , client ) ;
existingDisplayPreferences . IndexBy = Enum . TryParse < IndexingKind > ( displayPreferences . IndexBy , true , out var indexBy ) ? indexBy : ( IndexingKind ? ) null ;
existingDisplayPreferences . ShowBackdrop = displayPreferences . ShowBackdrop ;
existingDisplayPreferences . ShowSidebar = displayPreferences . ShowSidebar ;
existingDisplayPreferences . ScrollDirection = displayPreferences . ScrollDirection ;
existingDisplayPreferences . ChromecastVersion = displayPreferences . CustomPrefs . TryGetValue ( "chromecastVersion" , out var chromecastVersion )
? Enum . Parse < ChromecastVersion > ( chromecastVersion , true )
: ChromecastVersion . Stable ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "chromecastVersion" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . EnableNextVideoInfoOverlay = displayPreferences . CustomPrefs . TryGetValue ( "enableNextVideoInfoOverlay" , out var enableNextVideoInfoOverlay )
? bool . Parse ( enableNextVideoInfoOverlay )
: true ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "enableNextVideoInfoOverlay" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . SkipBackwardLength = displayPreferences . CustomPrefs . TryGetValue ( "skipBackLength" , out var skipBackLength )
? int . Parse ( skipBackLength , CultureInfo . InvariantCulture )
: 10000 ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "skipBackLength" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . SkipForwardLength = displayPreferences . CustomPrefs . TryGetValue ( "skipForwardLength" , out var skipForwardLength )
? int . Parse ( skipForwardLength , CultureInfo . InvariantCulture )
: 30000 ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "skipForwardLength" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . DashboardTheme = displayPreferences . CustomPrefs . TryGetValue ( "dashboardTheme" , out var theme )
? theme
: string . Empty ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "dashboardTheme" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . TvHome = displayPreferences . CustomPrefs . TryGetValue ( "tvhome" , out var home )
? home
: string . Empty ;
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( "tvhome" ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . HomeSections . Clear ( ) ;
foreach ( var key in displayPreferences . CustomPrefs . Keys . Where ( key = > key . StartsWith ( "homesection" , StringComparison . OrdinalIgnoreCase ) ) )
{
var order = int . Parse ( key . AsSpan ( ) . Slice ( "homesection" . Length ) ) ;
if ( ! Enum . TryParse < HomeSectionType > ( displayPreferences . CustomPrefs [ key ] , true , out var type ) )
{
type = order < 7 ? defaults [ order ] : HomeSectionType . None ;
}
2020-12-03 20:51:12 +00:00
displayPreferences . CustomPrefs . Remove ( key ) ;
2020-08-03 18:01:24 +00:00
existingDisplayPreferences . HomeSections . Add ( new HomeSection { Order = order , Type = type } ) ;
}
foreach ( var key in displayPreferences . CustomPrefs . Keys . Where ( key = > key . StartsWith ( "landing-" , StringComparison . OrdinalIgnoreCase ) ) )
{
2020-12-03 22:00:26 +00:00
if ( Guid . TryParse ( key . AsSpan ( ) . Slice ( "landing-" . Length ) , out var preferenceId ) )
2020-12-03 20:51:12 +00:00
{
var itemPreferences = _displayPreferencesManager . GetItemDisplayPreferences ( existingDisplayPreferences . UserId , preferenceId , existingDisplayPreferences . Client ) ;
itemPreferences . ViewType = Enum . Parse < ViewType > ( displayPreferences . ViewType ) ;
displayPreferences . CustomPrefs . Remove ( key ) ;
}
2020-08-03 18:01:24 +00:00
}
2020-12-04 16:16:38 +00:00
var itemPrefs = _displayPreferencesManager . GetItemDisplayPreferences ( existingDisplayPreferences . UserId , itemId , existingDisplayPreferences . Client ) ;
2020-08-03 18:01:24 +00:00
itemPrefs . SortBy = displayPreferences . SortBy ;
itemPrefs . SortOrder = displayPreferences . SortOrder ;
itemPrefs . RememberIndexing = displayPreferences . RememberIndexing ;
itemPrefs . RememberSorting = displayPreferences . RememberSorting ;
2020-12-04 16:16:38 +00:00
itemPrefs . ItemId = itemId ;
2020-08-03 18:01:24 +00:00
if ( Enum . TryParse < ViewType > ( displayPreferences . ViewType , true , out var viewType ) )
{
itemPrefs . ViewType = viewType ;
}
2020-12-03 20:51:12 +00:00
// Set all remaining custom preferences.
_displayPreferencesManager . SetCustomItemDisplayPreferences ( userId , existingDisplayPreferences . Client , displayPreferences . CustomPrefs ) ;
2020-08-08 17:39:49 +00:00
_displayPreferencesManager . SaveChanges ( ) ;
2020-04-21 13:55:57 +00:00
2020-06-21 00:02:07 +00:00
return NoContent ( ) ;
2020-04-19 18:26:38 +00:00
}
2020-04-19 18:06:18 +00:00
}
}