DisplayPreferencesController.cs 11 KB

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