< Summary - Jellyfin

Information
Class: Jellyfin.Api.Controllers.DisplayPreferencesController
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/Controllers/DisplayPreferencesController.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 106
Coverable lines: 106
Total lines: 223
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 46
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/102) Branch coverage: 0% (0/44) Total lines: 2142/5/2026 - 12:13:49 AM Line coverage: 0% (0/106) Branch coverage: 0% (0/46) Total lines: 2224/27/2026 - 12:15:04 AM Line coverage: 0% (0/106) Branch coverage: 0% (0/46) Total lines: 223 1/23/2026 - 12:11:06 AM Line coverage: 0% (0/102) Branch coverage: 0% (0/44) Total lines: 2142/5/2026 - 12:13:49 AM Line coverage: 0% (0/106) Branch coverage: 0% (0/46) Total lines: 2224/27/2026 - 12:15:04 AM Line coverage: 0% (0/106) Branch coverage: 0% (0/46) Total lines: 223

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
GetDisplayPreferences(...)0%7280%
UpdateDisplayPreferences(...)0%1482380%

File(s)

/srv/git/jellyfin/Jellyfin.Api/Controllers/DisplayPreferencesController.cs

#LineLine coverage
 1using System;
 2using System.ComponentModel.DataAnnotations;
 3using System.Diagnostics.CodeAnalysis;
 4using System.Globalization;
 5using System.Linq;
 6using Jellyfin.Api.Helpers;
 7using Jellyfin.Database.Implementations.Entities;
 8using Jellyfin.Database.Implementations.Enums;
 9using MediaBrowser.Common.Extensions;
 10using MediaBrowser.Controller;
 11using MediaBrowser.Model.Dto;
 12using Microsoft.AspNetCore.Authorization;
 13using Microsoft.AspNetCore.Http;
 14using Microsoft.AspNetCore.Mvc;
 15using Microsoft.Extensions.Logging;
 16
 17namespace Jellyfin.Api.Controllers;
 18
 19/// <summary>
 20/// Display Preferences Controller.
 21/// </summary>
 22[Authorize]
 23[Tags("DisplayPreference")]
 24public class DisplayPreferencesController : BaseJellyfinApiController
 25{
 26    private readonly IDisplayPreferencesManager _displayPreferencesManager;
 27    private readonly ILogger<DisplayPreferencesController> _logger;
 28
 29    /// <summary>
 30    /// Initializes a new instance of the <see cref="DisplayPreferencesController"/> class.
 31    /// </summary>
 32    /// <param name="displayPreferencesManager">Instance of <see cref="IDisplayPreferencesManager"/> interface.</param>
 33    /// <param name="logger">Instance of <see cref="ILogger{DisplayPreferencesController}"/> interface.</param>
 034    public DisplayPreferencesController(IDisplayPreferencesManager displayPreferencesManager, ILogger<DisplayPreferences
 35    {
 036        _displayPreferencesManager = displayPreferencesManager;
 037        _logger = logger;
 038    }
 39
 40    /// <summary>
 41    /// Get Display Preferences.
 42    /// </summary>
 43    /// <param name="displayPreferencesId">Display preferences id.</param>
 44    /// <param name="userId">User id.</param>
 45    /// <param name="client">Client.</param>
 46    /// <response code="200">Display preferences retrieved.</response>
 47    /// <returns>An <see cref="OkResult"/> containing the display preferences on success, or a <see cref="NotFoundResult
 48    [HttpGet("{displayPreferencesId}")]
 49    [ProducesResponseType(StatusCodes.Status200OK)]
 50    [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justi
 51    public ActionResult<DisplayPreferencesDto> GetDisplayPreferences(
 52        [FromRoute, Required] string displayPreferencesId,
 53        [FromQuery] Guid? userId,
 54        [FromQuery, Required] string client)
 55    {
 056        userId = RequestHelpers.GetUserId(User, userId);
 57
 058        if (!Guid.TryParse(displayPreferencesId, out var itemId))
 59        {
 060            itemId = displayPreferencesId.GetMD5();
 61        }
 62
 063        var displayPreferences = _displayPreferencesManager.GetDisplayPreferences(userId.Value, itemId, client);
 064        var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(displayPreferences.UserId, itemId, di
 065        itemPreferences.ItemId = itemId;
 66
 067        var dto = new DisplayPreferencesDto
 068        {
 069            Client = displayPreferences.Client,
 070            Id = displayPreferences.ItemId.ToString(),
 071            SortBy = itemPreferences.SortBy,
 072            SortOrder = itemPreferences.SortOrder,
 073            IndexBy = displayPreferences.IndexBy?.ToString(),
 074            RememberIndexing = itemPreferences.RememberIndexing,
 075            RememberSorting = itemPreferences.RememberSorting,
 076            ScrollDirection = displayPreferences.ScrollDirection,
 077            ShowBackdrop = displayPreferences.ShowBackdrop,
 078            ShowSidebar = displayPreferences.ShowSidebar
 079        };
 80
 081        foreach (var homeSection in displayPreferences.HomeSections)
 82        {
 083            dto.CustomPrefs["homesection" + homeSection.Order] = homeSection.Type.ToString().ToLowerInvariant();
 84        }
 85
 086        dto.CustomPrefs["chromecastVersion"] = displayPreferences.ChromecastVersion.ToString().ToLowerInvariant();
 087        dto.CustomPrefs["skipForwardLength"] = displayPreferences.SkipForwardLength.ToString(CultureInfo.InvariantCultur
 088        dto.CustomPrefs["skipBackLength"] = displayPreferences.SkipBackwardLength.ToString(CultureInfo.InvariantCulture)
 089        dto.CustomPrefs["enableNextVideoInfoOverlay"] = displayPreferences.EnableNextVideoInfoOverlay.ToString(CultureIn
 090        dto.CustomPrefs["tvhome"] = displayPreferences.TvHome;
 091        dto.CustomPrefs["dashboardTheme"] = displayPreferences.DashboardTheme;
 92
 93        // Load all custom display preferences
 094        var customDisplayPreferences = _displayPreferencesManager.ListCustomItemDisplayPreferences(displayPreferences.Us
 095        foreach (var (key, value) in customDisplayPreferences)
 96        {
 097            dto.CustomPrefs.TryAdd(key, value);
 98        }
 99
 0100        return dto;
 101    }
 102
 103    /// <summary>
 104    /// Update Display Preferences.
 105    /// </summary>
 106    /// <param name="displayPreferencesId">Display preferences id.</param>
 107    /// <param name="userId">User Id.</param>
 108    /// <param name="client">Client.</param>
 109    /// <param name="displayPreferences">New Display Preferences object.</param>
 110    /// <response code="204">Display preferences updated.</response>
 111    /// <returns>An <see cref="NoContentResult"/> on success.</returns>
 112    [HttpPost("{displayPreferencesId}")]
 113    [ProducesResponseType(StatusCodes.Status204NoContent)]
 114    [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justi
 115    public ActionResult UpdateDisplayPreferences(
 116        [FromRoute, Required] string displayPreferencesId,
 117        [FromQuery] Guid? userId,
 118        [FromQuery, Required] string client,
 119        [FromBody, Required] DisplayPreferencesDto displayPreferences)
 120    {
 0121        userId = RequestHelpers.GetUserId(User, userId);
 122
 0123        HomeSectionType[] defaults =
 0124        {
 0125            HomeSectionType.SmallLibraryTiles,
 0126            HomeSectionType.Resume,
 0127            HomeSectionType.ResumeAudio,
 0128            HomeSectionType.ResumeBook,
 0129            HomeSectionType.LiveTv,
 0130            HomeSectionType.NextUp,
 0131            HomeSectionType.LatestMedia,
 0132            HomeSectionType.None,
 0133        };
 134
 0135        if (!Guid.TryParse(displayPreferencesId, out var itemId))
 136        {
 0137            itemId = displayPreferencesId.GetMD5();
 138        }
 139
 0140        var existingDisplayPreferences = _displayPreferencesManager.GetDisplayPreferences(userId.Value, itemId, client);
 0141        existingDisplayPreferences.IndexBy = Enum.TryParse<IndexingKind>(displayPreferences.IndexBy, true, out var index
 0142        existingDisplayPreferences.ShowBackdrop = displayPreferences.ShowBackdrop;
 0143        existingDisplayPreferences.ShowSidebar = displayPreferences.ShowSidebar;
 144
 0145        existingDisplayPreferences.ScrollDirection = displayPreferences.ScrollDirection;
 0146        existingDisplayPreferences.ChromecastVersion = displayPreferences.CustomPrefs.TryGetValue("chromecastVersion", o
 0147                                                       && !string.IsNullOrEmpty(chromecastVersion)
 0148            ? Enum.Parse<ChromecastVersion>(chromecastVersion, true)
 0149            : ChromecastVersion.Stable;
 0150        displayPreferences.CustomPrefs.Remove("chromecastVersion");
 151
 0152        existingDisplayPreferences.EnableNextVideoInfoOverlay = !displayPreferences.CustomPrefs.TryGetValue("enableNextV
 0153                                                                || string.IsNullOrEmpty(enableNextVideoInfoOverlay)
 0154                                                                || bool.Parse(enableNextVideoInfoOverlay);
 0155        displayPreferences.CustomPrefs.Remove("enableNextVideoInfoOverlay");
 156
 0157        existingDisplayPreferences.SkipBackwardLength = displayPreferences.CustomPrefs.TryGetValue("skipBackLength", out
 0158                                                        && !string.IsNullOrEmpty(skipBackLength)
 0159            ? int.Parse(skipBackLength, CultureInfo.InvariantCulture)
 0160            : 15000;
 0161        displayPreferences.CustomPrefs.Remove("skipBackLength");
 162
 0163        existingDisplayPreferences.SkipForwardLength = displayPreferences.CustomPrefs.TryGetValue("skipForwardLength", o
 0164                                                       && !string.IsNullOrEmpty(skipForwardLength)
 0165            ? int.Parse(skipForwardLength, CultureInfo.InvariantCulture)
 0166            : 15000;
 0167        displayPreferences.CustomPrefs.Remove("skipForwardLength");
 168
 0169        existingDisplayPreferences.DashboardTheme = displayPreferences.CustomPrefs.TryGetValue("dashboardTheme", out var
 0170            ? theme
 0171            : string.Empty;
 0172        displayPreferences.CustomPrefs.Remove("dashboardTheme");
 173
 0174        existingDisplayPreferences.TvHome = displayPreferences.CustomPrefs.TryGetValue("tvhome", out var home)
 0175            ? home
 0176            : string.Empty;
 0177        displayPreferences.CustomPrefs.Remove("tvhome");
 178
 0179        existingDisplayPreferences.HomeSections.Clear();
 180
 0181        foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("homesection", StringCompari
 182        {
 0183            var order = int.Parse(key.AsSpan().Slice("homesection".Length), CultureInfo.InvariantCulture);
 0184            if (!Enum.TryParse<HomeSectionType>(displayPreferences.CustomPrefs[key], true, out var type))
 185            {
 0186                type = order < 8 ? defaults[order] : HomeSectionType.None;
 187            }
 188
 0189            displayPreferences.CustomPrefs.Remove(key);
 0190            existingDisplayPreferences.HomeSections.Add(new HomeSection { Order = order, Type = type });
 191        }
 192
 0193        foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("landing-", StringComparison
 194        {
 0195            var viewType = displayPreferences.CustomPrefs[key];
 196
 0197            if (string.IsNullOrEmpty(viewType))
 198            {
 0199                displayPreferences.CustomPrefs.Remove(key);
 0200                continue;
 201            }
 202
 0203            if (!Enum.TryParse<ViewType>(viewType, true, out _))
 204            {
 0205                _logger.LogError("Invalid ViewType: {LandingScreenOption}", viewType);
 0206                displayPreferences.CustomPrefs.Remove(key);
 207            }
 208        }
 209
 0210        var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, itemId, 
 0211        itemPrefs.SortBy = displayPreferences.SortBy ?? "SortName";
 0212        itemPrefs.SortOrder = displayPreferences.SortOrder;
 0213        itemPrefs.RememberIndexing = displayPreferences.RememberIndexing;
 0214        itemPrefs.RememberSorting = displayPreferences.RememberSorting;
 0215        itemPrefs.ItemId = itemId;
 216
 217        // Set all remaining custom preferences.
 0218        _displayPreferencesManager.SetCustomItemDisplayPreferences(userId.Value, itemId, existingDisplayPreferences.Clie
 0219        _displayPreferencesManager.UpdateItemDisplayPreferences(itemPrefs);
 0220        _displayPreferencesManager.UpdateDisplayPreferences(existingDisplayPreferences);
 0221        return NoContent();
 222    }
 223}