DisplayPreferencesController.cs 9.5 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. 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. // This will essentially be a noop if no changes have been made, but new prefs must be saved at least.
  77. _displayPreferencesManager.SaveChanges();
  78. return dto;
  79. }
  80. /// <summary>
  81. /// Update Display Preferences.
  82. /// </summary>
  83. /// <param name="displayPreferencesId">Display preferences id.</param>
  84. /// <param name="userId">User Id.</param>
  85. /// <param name="client">Client.</param>
  86. /// <param name="displayPreferences">New Display Preferences object.</param>
  87. /// <response code="204">Display preferences updated.</response>
  88. /// <returns>An <see cref="NoContentResult"/> on success.</returns>
  89. [HttpPost("{displayPreferencesId}")]
  90. [ProducesResponseType(StatusCodes.Status204NoContent)]
  91. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "displayPreferencesId", Justification = "Imported from ServiceStack")]
  92. public ActionResult UpdateDisplayPreferences(
  93. [FromRoute, Required] string displayPreferencesId,
  94. [FromQuery, Required] Guid userId,
  95. [FromQuery, Required] string client,
  96. [FromBody, Required] DisplayPreferencesDto displayPreferences)
  97. {
  98. HomeSectionType[] defaults =
  99. {
  100. HomeSectionType.SmallLibraryTiles,
  101. HomeSectionType.Resume,
  102. HomeSectionType.ResumeAudio,
  103. HomeSectionType.LiveTv,
  104. HomeSectionType.NextUp,
  105. HomeSectionType.LatestMedia, HomeSectionType.None,
  106. };
  107. var existingDisplayPreferences = _displayPreferencesManager.GetDisplayPreferences(userId, client);
  108. existingDisplayPreferences.IndexBy = Enum.TryParse<IndexingKind>(displayPreferences.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null;
  109. existingDisplayPreferences.ShowBackdrop = displayPreferences.ShowBackdrop;
  110. existingDisplayPreferences.ShowSidebar = displayPreferences.ShowSidebar;
  111. existingDisplayPreferences.ScrollDirection = displayPreferences.ScrollDirection;
  112. existingDisplayPreferences.ChromecastVersion = displayPreferences.CustomPrefs.TryGetValue("chromecastVersion", out var chromecastVersion)
  113. ? Enum.Parse<ChromecastVersion>(chromecastVersion, true)
  114. : ChromecastVersion.Stable;
  115. existingDisplayPreferences.EnableNextVideoInfoOverlay = displayPreferences.CustomPrefs.TryGetValue("enableNextVideoInfoOverlay", out var enableNextVideoInfoOverlay)
  116. ? bool.Parse(enableNextVideoInfoOverlay)
  117. : true;
  118. existingDisplayPreferences.SkipBackwardLength = displayPreferences.CustomPrefs.TryGetValue("skipBackLength", out var skipBackLength)
  119. ? int.Parse(skipBackLength, CultureInfo.InvariantCulture)
  120. : 10000;
  121. existingDisplayPreferences.SkipForwardLength = displayPreferences.CustomPrefs.TryGetValue("skipForwardLength", out var skipForwardLength)
  122. ? int.Parse(skipForwardLength, CultureInfo.InvariantCulture)
  123. : 30000;
  124. existingDisplayPreferences.DashboardTheme = displayPreferences.CustomPrefs.TryGetValue("dashboardTheme", out var theme)
  125. ? theme
  126. : string.Empty;
  127. existingDisplayPreferences.TvHome = displayPreferences.CustomPrefs.TryGetValue("tvhome", out var home)
  128. ? home
  129. : string.Empty;
  130. existingDisplayPreferences.HomeSections.Clear();
  131. foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("homesection", StringComparison.OrdinalIgnoreCase)))
  132. {
  133. var order = int.Parse(key.AsSpan().Slice("homesection".Length));
  134. if (!Enum.TryParse<HomeSectionType>(displayPreferences.CustomPrefs[key], true, out var type))
  135. {
  136. type = order < 7 ? defaults[order] : HomeSectionType.None;
  137. }
  138. existingDisplayPreferences.HomeSections.Add(new HomeSection { Order = order, Type = type });
  139. }
  140. foreach (var key in displayPreferences.CustomPrefs.Keys.Where(key => key.StartsWith("landing-", StringComparison.OrdinalIgnoreCase)))
  141. {
  142. var itemPreferences = _displayPreferencesManager.GetItemDisplayPreferences(existingDisplayPreferences.UserId, Guid.Parse(key.Substring("landing-".Length)), existingDisplayPreferences.Client);
  143. itemPreferences.ViewType = Enum.Parse<ViewType>(displayPreferences.ViewType);
  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();
  155. return NoContent();
  156. }
  157. }
  158. }