DisplayPreferencesController.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Linq;
  6. using Jellyfin.Api.Constants;
  7. using Jellyfin.Data.Entities;
  8. using Jellyfin.Data.Enums;
  9. using MediaBrowser.Controller;
  10. using MediaBrowser.Model.Entities;
  11. using Microsoft.AspNetCore.Authorization;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Mvc.ModelBinding;
  15. namespace Jellyfin.Api.Controllers
  16. {
  17. /// <summary>
  18. /// Display Preferences Controller.
  19. /// </summary>
  20. [Authorize(Policy = Policies.DefaultAuthorization)]
  21. public class DisplayPreferencesController : BaseJellyfinApiController
  22. {
  23. private readonly IDisplayPreferencesManager _displayPreferencesManager;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="DisplayPreferencesController"/> class.
  26. /// </summary>
  27. /// <param name="displayPreferencesManager">Instance of <see cref="IDisplayPreferencesManager"/> interface.</param>
  28. public DisplayPreferencesController(IDisplayPreferencesManager displayPreferencesManager)
  29. {
  30. _displayPreferencesManager = displayPreferencesManager;
  31. }
  32. /// <summary>
  33. /// Get Display Preferences.
  34. /// </summary>
  35. /// <param name="displayPreferencesId">Display preferences id.</param>
  36. /// <param name="userId">User id.</param>
  37. /// <param name="client">Client.</param>
  38. /// <response code="200">Display preferences retrieved.</response>
  39. /// <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>
  40. [HttpGet("{displayPreferencesId}")]
  41. [ProducesResponseType(StatusCodes.Status200OK)]
  42. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
  43. public ActionResult<DisplayPreferencesDto> GetDisplayPreferences(
  44. [FromRoute] string? displayPreferencesId,
  45. [FromQuery] [Required] Guid userId,
  46. [FromQuery] [Required] string? client)
  47. {
  48. var displayPreferences = _displayPreferencesManager.GetDisplayPreferences(userId, client);
  49. var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(displayPreferences.UserId, Guid.Empty, displayPreferences.Client);
  50. var dto = new DisplayPreferencesDto
  51. {
  52. Client = displayPreferences.Client,
  53. Id = displayPreferences.UserId.ToString(),
  54. ViewType = itemPreferences.ViewType.ToString(),
  55. SortBy = itemPreferences.SortBy,
  56. SortOrder = itemPreferences.SortOrder,
  57. IndexBy = displayPreferences.IndexBy?.ToString(),
  58. RememberIndexing = itemPreferences.RememberIndexing,
  59. RememberSorting = itemPreferences.RememberSorting,
  60. ScrollDirection = displayPreferences.ScrollDirection,
  61. ShowBackdrop = displayPreferences.ShowBackdrop,
  62. ShowSidebar = displayPreferences.ShowSidebar
  63. };
  64. foreach (var homeSection in displayPreferences.HomeSections)
  65. {
  66. dto.CustomPrefs["homesection" + homeSection.Order] = homeSection.Type.ToString().ToLowerInvariant();
  67. }
  68. foreach (var itemDisplayPreferences in _displayPreferencesManager.ListItemDisplayPreferences(displayPreferences.UserId, displayPreferences.Client))
  69. {
  70. dto.CustomPrefs["landing-" + itemDisplayPreferences.ItemId] = itemDisplayPreferences.ViewType.ToString().ToLowerInvariant();
  71. }
  72. dto.CustomPrefs["chromecastVersion"] = displayPreferences.ChromecastVersion.ToString().ToLowerInvariant();
  73. dto.CustomPrefs["skipForwardLength"] = displayPreferences.SkipForwardLength.ToString(CultureInfo.InvariantCulture);
  74. dto.CustomPrefs["skipBackLength"] = displayPreferences.SkipBackwardLength.ToString(CultureInfo.InvariantCulture);
  75. dto.CustomPrefs["enableNextVideoInfoOverlay"] = displayPreferences.EnableNextVideoInfoOverlay.ToString(CultureInfo.InvariantCulture);
  76. dto.CustomPrefs["tvhome"] = displayPreferences.TvHome;
  77. return dto;
  78. }
  79. /// <summary>
  80. /// Update Display Preferences.
  81. /// </summary>
  82. /// <param name="displayPreferencesId">Display preferences id.</param>
  83. /// <param name="userId">User Id.</param>
  84. /// <param name="client">Client.</param>
  85. /// <param name="displayPreferences">New Display Preferences object.</param>
  86. /// <response code="204">Display preferences updated.</response>
  87. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  88. [HttpPost("{displayPreferencesId}")]
  89. [ProducesResponseType(StatusCodes.Status204NoContent)]
  90. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
  91. public ActionResult UpdateDisplayPreferences(
  92. [FromRoute] string? displayPreferencesId,
  93. [FromQuery, BindRequired] Guid userId,
  94. [FromQuery, BindRequired] string? client,
  95. [FromBody, BindRequired] DisplayPreferencesDto displayPreferences)
  96. {
  97. HomeSectionType[] defaults =
  98. {
  99. HomeSectionType.SmallLibraryTiles,
  100. HomeSectionType.Resume,
  101. HomeSectionType.ResumeAudio,
  102. HomeSectionType.LiveTv,
  103. HomeSectionType.NextUp,
  104. HomeSectionType.LatestMedia, HomeSectionType.None,
  105. };
  106. var existingDisplayPreferences = _displayPreferencesManager.GetDisplayPreferences(userId, client);
  107. existingDisplayPreferences.IndexBy = Enum.TryParse<IndexingKind>(displayPreferences.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null;
  108. existingDisplayPreferences.ShowBackdrop = displayPreferences.ShowBackdrop;
  109. existingDisplayPreferences.ShowSidebar = displayPreferences.ShowSidebar;
  110. existingDisplayPreferences.ScrollDirection = displayPreferences.ScrollDirection;
  111. existingDisplayPreferences.ChromecastVersion = displayPreferences.CustomPrefs.TryGetValue("chromecastVersion", out var chromecastVersion)
  112. ? Enum.Parse<ChromecastVersion>(chromecastVersion, true)
  113. : ChromecastVersion.Stable;
  114. existingDisplayPreferences.EnableNextVideoInfoOverlay = displayPreferences.CustomPrefs.TryGetValue("enableNextVideoInfoOverlay", out var enableNextVideoInfoOverlay)
  115. ? bool.Parse(enableNextVideoInfoOverlay)
  116. : true;
  117. existingDisplayPreferences.SkipBackwardLength = displayPreferences.CustomPrefs.TryGetValue("skipBackLength", out var skipBackLength)
  118. ? int.Parse(skipBackLength, CultureInfo.InvariantCulture)
  119. : 10000;
  120. existingDisplayPreferences.SkipForwardLength = displayPreferences.CustomPrefs.TryGetValue("skipForwardLength", out var skipForwardLength)
  121. ? int.Parse(skipForwardLength, CultureInfo.InvariantCulture)
  122. : 30000;
  123. existingDisplayPreferences.DashboardTheme = displayPreferences.CustomPrefs.TryGetValue("dashboardTheme", out var theme)
  124. ? theme
  125. : string.Empty;
  126. existingDisplayPreferences.TvHome = displayPreferences.CustomPrefs.TryGetValue("tvhome", out var home)
  127. ? home
  128. : string.Empty;
  129. existingDisplayPreferences.HomeSections.Clear();
  130. foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("homesection", StringComparison.OrdinalIgnoreCase)))
  131. {
  132. var order = int.Parse(key.AsSpan().Slice("homesection".Length));
  133. if (!Enum.TryParse<HomeSectionType>(displayPreferences.CustomPrefs[key], true, out var type))
  134. {
  135. type = order < 7 ? defaults[order] : HomeSectionType.None;
  136. }
  137. existingDisplayPreferences.HomeSections.Add(new HomeSection { Order = order, Type = type });
  138. }
  139. foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("landing-", StringComparison.OrdinalIgnoreCase)))
  140. {
  141. var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Parse(key.Substring("landing-".Length)), existingDisplayPreferences.Client);
  142. itemPreferences.ViewType = Enum.Parse<ViewType>(displayPreferences.ViewType);
  143. _displayPreferencesManager.SaveChanges(itemPreferences);
  144. }
  145. var itemPrefs = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Empty, existingDisplayPreferences.Client);
  146. itemPrefs.SortBy = displayPreferences.SortBy;
  147. itemPrefs.SortOrder = displayPreferences.SortOrder;
  148. itemPrefs.RememberIndexing = displayPreferences.RememberIndexing;
  149. itemPrefs.RememberSorting = displayPreferences.RememberSorting;
  150. if (Enum.TryParse<ViewType>(displayPreferences.ViewType, true, out var viewType))
  151. {
  152. itemPrefs.ViewType = viewType;
  153. }
  154. _displayPreferencesManager.SaveChanges(existingDisplayPreferences);
  155. _displayPreferencesManager.SaveChanges(itemPrefs);
  156. return NoContent();
  157. }
  158. }
  159. }