DisplayPreferencesController.cs 11 KB

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