DisplayPreferencesController.cs 9.4 KB

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