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 ;
2024-02-17 13:29:34 +00:00
using Jellyfin.Api.Helpers ;
2020-08-03 18:01:24 +00:00
using Jellyfin.Data.Entities ;
using Jellyfin.Data.Enums ;
2020-12-04 23:27:31 +00:00
using MediaBrowser.Common.Extensions ;
2020-08-03 18:01:24 +00:00
using MediaBrowser.Controller ;
2021-11-11 14:16:57 +00:00
using MediaBrowser.Model.Dto ;
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 ;
2020-12-10 14:41:00 +00:00
using Microsoft.Extensions.Logging ;
2020-04-19 18:06:18 +00:00
2023-01-31 11:18:10 +00:00
namespace Jellyfin.Api.Controllers ;
/// <summary>
/// Display Preferences Controller.
/// </summary>
2023-02-08 22:55:26 +00:00
[Authorize]
2023-01-31 11:18:10 +00:00
public class DisplayPreferencesController : BaseJellyfinApiController
2020-04-19 18:06:18 +00:00
{
2023-01-31 11:18:10 +00:00
private readonly IDisplayPreferencesManager _displayPreferencesManager ;
private readonly ILogger < DisplayPreferencesController > _logger ;
2020-04-19 18:06:18 +00:00
/// <summary>
2023-01-31 11:18:10 +00:00
/// Initializes a new instance of the <see cref="DisplayPreferencesController"/> class.
2020-04-19 18:06:18 +00:00
/// </summary>
2023-01-31 11:18:10 +00:00
/// <param name="displayPreferencesManager">Instance of <see cref="IDisplayPreferencesManager"/> interface.</param>
/// <param name="logger">Instance of <see cref="ILogger{DisplayPreferencesController}"/> interface.</param>
public DisplayPreferencesController ( IDisplayPreferencesManager displayPreferencesManager , ILogger < DisplayPreferencesController > logger )
2020-04-19 18:06:18 +00:00
{
2023-01-31 11:18:10 +00:00
_displayPreferencesManager = displayPreferencesManager ;
_logger = logger ;
}
/// <summary>
/// Get Display Preferences.
/// </summary>
/// <param name="displayPreferencesId">Display preferences id.</param>
/// <param name="userId">User id.</param>
/// <param name="client">Client.</param>
/// <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>
[HttpGet("{displayPreferencesId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
public ActionResult < DisplayPreferencesDto > GetDisplayPreferences (
[FromRoute, Required] string displayPreferencesId ,
2024-02-17 13:29:34 +00:00
[FromQuery] Guid ? userId ,
2023-01-31 11:18:10 +00:00
[FromQuery, Required] string client )
{
2024-02-17 13:29:34 +00:00
userId = RequestHelpers . GetUserId ( User , userId ) ;
2023-01-31 11:18:10 +00:00
if ( ! Guid . TryParse ( displayPreferencesId , out var itemId ) )
2020-04-19 18:26:38 +00:00
{
2023-01-31 11:18:10 +00:00
itemId = displayPreferencesId . GetMD5 ( ) ;
2020-04-19 18:26:38 +00:00
}
2024-02-17 13:29:34 +00:00
var displayPreferences = _displayPreferencesManager . GetDisplayPreferences ( userId . Value , itemId , client ) ;
2023-01-31 11:18:10 +00:00
var itemPreferences = _displayPreferencesManager . GetItemDisplayPreferences ( displayPreferences . UserId , itemId , displayPreferences . Client ) ;
itemPreferences . ItemId = itemId ;
2020-12-04 23:27:31 +00:00
2023-01-31 11:18:10 +00:00
var dto = new DisplayPreferencesDto
{
Client = displayPreferences . Client ,
Id = displayPreferences . ItemId . 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 ( ) ;
}
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +00:00
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 ;
dto . CustomPrefs [ "dashboardTheme" ] = displayPreferences . DashboardTheme ;
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +00:00
// Load all custom display preferences
var customDisplayPreferences = _displayPreferencesManager . ListCustomItemDisplayPreferences ( displayPreferences . UserId , itemId , displayPreferences . Client ) ;
foreach ( var ( key , value ) in customDisplayPreferences )
{
dto . CustomPrefs . TryAdd ( key , value ) ;
}
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +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-12-03 20:51:12 +00:00
2023-01-31 11:18:10 +00:00
return dto ;
}
2020-11-02 08:23:29 +00:00
2023-01-31 11:18:10 +00:00
/// <summary>
/// Update Display Preferences.
/// </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>
/// <response code="204">Display preferences updated.</response>
/// <returns>An <see cref="NoContentResult"/> on success.</returns>
[HttpPost("{displayPreferencesId}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
public ActionResult UpdateDisplayPreferences (
[FromRoute, Required] string displayPreferencesId ,
2024-02-17 13:29:34 +00:00
[FromQuery] Guid ? userId ,
2023-01-31 11:18:10 +00:00
[FromQuery, Required] string client ,
[FromBody, Required] DisplayPreferencesDto displayPreferences )
{
2024-02-17 13:29:34 +00:00
userId = RequestHelpers . GetUserId ( User , userId ) ;
2023-01-31 11:18:10 +00:00
HomeSectionType [ ] defaults =
{
HomeSectionType . SmallLibraryTiles ,
HomeSectionType . Resume ,
HomeSectionType . ResumeAudio ,
HomeSectionType . ResumeBook ,
HomeSectionType . LiveTv ,
HomeSectionType . NextUp ,
HomeSectionType . LatestMedia ,
HomeSectionType . None ,
} ;
if ( ! Guid . TryParse ( displayPreferencesId , out var itemId ) )
{
itemId = displayPreferencesId . GetMD5 ( ) ;
2020-04-19 18:26:38 +00:00
}
2024-02-17 13:29:34 +00:00
var existingDisplayPreferences = _displayPreferencesManager . GetDisplayPreferences ( userId . Value , itemId , client ) ;
2023-01-31 11:18:10 +00:00
existingDisplayPreferences . IndexBy = Enum . TryParse < IndexingKind > ( displayPreferences . IndexBy , true , out var indexBy ) ? indexBy : null ;
existingDisplayPreferences . ShowBackdrop = displayPreferences . ShowBackdrop ;
existingDisplayPreferences . ShowSidebar = displayPreferences . ShowSidebar ;
existingDisplayPreferences . ScrollDirection = displayPreferences . ScrollDirection ;
existingDisplayPreferences . ChromecastVersion = displayPreferences . CustomPrefs . TryGetValue ( "chromecastVersion" , out var chromecastVersion )
& & ! string . IsNullOrEmpty ( chromecastVersion )
? Enum . Parse < ChromecastVersion > ( chromecastVersion , true )
: ChromecastVersion . Stable ;
displayPreferences . CustomPrefs . Remove ( "chromecastVersion" ) ;
existingDisplayPreferences . EnableNextVideoInfoOverlay = ! displayPreferences . CustomPrefs . TryGetValue ( "enableNextVideoInfoOverlay" , out var enableNextVideoInfoOverlay )
| | string . IsNullOrEmpty ( enableNextVideoInfoOverlay )
| | bool . Parse ( enableNextVideoInfoOverlay ) ;
displayPreferences . CustomPrefs . Remove ( "enableNextVideoInfoOverlay" ) ;
existingDisplayPreferences . SkipBackwardLength = displayPreferences . CustomPrefs . TryGetValue ( "skipBackLength" , out var skipBackLength )
& & ! string . IsNullOrEmpty ( skipBackLength )
? int . Parse ( skipBackLength , CultureInfo . InvariantCulture )
: 10000 ;
displayPreferences . CustomPrefs . Remove ( "skipBackLength" ) ;
existingDisplayPreferences . SkipForwardLength = displayPreferences . CustomPrefs . TryGetValue ( "skipForwardLength" , out var skipForwardLength )
& & ! string . IsNullOrEmpty ( skipForwardLength )
? int . Parse ( skipForwardLength , CultureInfo . InvariantCulture )
: 30000 ;
displayPreferences . CustomPrefs . Remove ( "skipForwardLength" ) ;
existingDisplayPreferences . DashboardTheme = displayPreferences . CustomPrefs . TryGetValue ( "dashboardTheme" , out var theme )
? theme
: string . Empty ;
displayPreferences . CustomPrefs . Remove ( "dashboardTheme" ) ;
existingDisplayPreferences . TvHome = displayPreferences . CustomPrefs . TryGetValue ( "tvhome" , out var home )
? home
: string . Empty ;
displayPreferences . CustomPrefs . Remove ( "tvhome" ) ;
existingDisplayPreferences . HomeSections . Clear ( ) ;
foreach ( var key in displayPreferences . CustomPrefs . Keys . Where ( key = > key . StartsWith ( "homesection" , StringComparison . OrdinalIgnoreCase ) ) )
2020-04-19 18:26:38 +00:00
{
2023-01-31 11:18:10 +00:00
var order = int . Parse ( key . AsSpan ( ) . Slice ( "homesection" . Length ) , CultureInfo . InvariantCulture ) ;
if ( ! Enum . TryParse < HomeSectionType > ( displayPreferences . CustomPrefs [ key ] , true , out var type ) )
2020-12-04 23:27:31 +00:00
{
2023-01-31 11:18:10 +00:00
type = order < 8 ? defaults [ order ] : HomeSectionType . None ;
2020-12-04 23:27:31 +00:00
}
2023-01-31 11:18:10 +00:00
displayPreferences . CustomPrefs . Remove ( key ) ;
existingDisplayPreferences . HomeSections . Add ( new HomeSection { Order = order , Type = type } ) ;
}
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +00:00
foreach ( var key in displayPreferences . CustomPrefs . Keys . Where ( key = > key . StartsWith ( "landing-" , StringComparison . OrdinalIgnoreCase ) ) )
{
2024-04-14 14:18:36 +00:00
if ( ! Enum . TryParse < ViewType > ( displayPreferences . CustomPrefs [ key ] , true , out _ ) )
2020-08-03 18:01:24 +00:00
{
2023-01-31 11:18:10 +00:00
_logger . LogError ( "Invalid ViewType: {LandingScreenOption}" , displayPreferences . CustomPrefs [ key ] ) ;
displayPreferences . CustomPrefs . Remove ( key ) ;
2020-08-03 18:01:24 +00:00
}
2023-01-31 11:18:10 +00:00
}
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +00:00
var itemPrefs = _displayPreferencesManager . GetItemDisplayPreferences ( existingDisplayPreferences . UserId , itemId , existingDisplayPreferences . Client ) ;
itemPrefs . SortBy = displayPreferences . SortBy ? ? "SortName" ;
itemPrefs . SortOrder = displayPreferences . SortOrder ;
itemPrefs . RememberIndexing = displayPreferences . RememberIndexing ;
itemPrefs . RememberSorting = displayPreferences . RememberSorting ;
itemPrefs . ItemId = itemId ;
2020-08-03 18:01:24 +00:00
2023-01-31 11:18:10 +00:00
// Set all remaining custom preferences.
2024-02-17 13:29:34 +00:00
_displayPreferencesManager . SetCustomItemDisplayPreferences ( userId . Value , itemId , existingDisplayPreferences . Client , displayPreferences . CustomPrefs ) ;
2023-01-31 11:18:10 +00:00
_displayPreferencesManager . SaveChanges ( ) ;
2020-04-21 13:55:57 +00:00
2023-01-31 11:18:10 +00:00
return NoContent ( ) ;
2020-04-19 18:06:18 +00:00
}
}