DisplayPreferencesController.cs 11 KB

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