NotificationsController.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #nullable enable
  2. #pragma warning disable CA1801
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using Jellyfin.Api.Models.NotificationDtos;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Notifications;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Notifications;
  12. using Microsoft.AspNetCore.Http;
  13. using Microsoft.AspNetCore.Mvc;
  14. namespace Jellyfin.Api.Controllers
  15. {
  16. /// <summary>
  17. /// The notification controller.
  18. /// </summary>
  19. public class NotificationsController : BaseJellyfinApiController
  20. {
  21. private readonly INotificationManager _notificationManager;
  22. private readonly IUserManager _userManager;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="NotificationsController" /> class.
  25. /// </summary>
  26. /// <param name="notificationManager">The notification manager.</param>
  27. /// <param name="userManager">The user manager.</param>
  28. public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
  29. {
  30. _notificationManager = notificationManager;
  31. _userManager = userManager;
  32. }
  33. /// <summary>
  34. /// Endpoint for getting a user's notifications.
  35. /// </summary>
  36. /// <param name="userId">The user's ID.</param>
  37. /// <param name="isRead">An optional filter by IsRead.</param>
  38. /// <param name="startIndex">The optional index to start at. All notifications with a lower index will be dropped from the results.</param>
  39. /// <param name="limit">An optional limit on the number of notifications returned.</param>
  40. /// <returns>A read-only list of all of the user's notifications.</returns>
  41. [HttpGet("{UserID}")]
  42. [ProducesResponseType(typeof(IEnumerable<NotificationResultDto>), StatusCodes.Status200OK)]
  43. public IActionResult GetNotifications(
  44. [FromRoute] string userId,
  45. [FromQuery] bool? isRead,
  46. [FromQuery] int? startIndex,
  47. [FromQuery] int? limit)
  48. {
  49. return Ok(new NotificationResultDto());
  50. }
  51. /// <summary>
  52. /// Endpoint for getting a user's notification summary.
  53. /// </summary>
  54. /// <param name="userId">The user's ID.</param>
  55. /// <returns>Notifications summary for the user.</returns>
  56. [HttpGet("{UserID}/Summary")]
  57. [ProducesResponseType(typeof(NotificationsSummaryDto), StatusCodes.Status200OK)]
  58. public IActionResult GetNotificationsSummary(
  59. [FromRoute] string userId)
  60. {
  61. return Ok(new NotificationsSummaryDto());
  62. }
  63. /// <summary>
  64. /// Endpoint for getting notification types.
  65. /// </summary>
  66. /// <returns>All notification types.</returns>
  67. [HttpGet("Types")]
  68. [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
  69. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  70. public IActionResult GetNotificationTypes()
  71. {
  72. try
  73. {
  74. return Ok(_notificationManager.GetNotificationTypes());
  75. }
  76. catch (Exception e)
  77. {
  78. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  79. }
  80. }
  81. /// <summary>
  82. /// Endpoint for getting notification services.
  83. /// </summary>
  84. /// <returns>All notification services.</returns>
  85. [HttpGet("Services")]
  86. [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
  87. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  88. public IActionResult GetNotificationServices()
  89. {
  90. try
  91. {
  92. return Ok(_notificationManager.GetNotificationServices());
  93. }
  94. catch (Exception e)
  95. {
  96. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  97. }
  98. }
  99. /// <summary>
  100. /// Endpoint to send a notification to all admins.
  101. /// </summary>
  102. /// <param name="name">The name of the notification.</param>
  103. /// <param name="description">The description of the notification.</param>
  104. /// <param name="url">The URL of the notification.</param>
  105. /// <param name="level">The level of the notification.</param>
  106. /// <returns>Status.</returns>
  107. [HttpPost("Admin")]
  108. [ProducesResponseType(StatusCodes.Status200OK)]
  109. [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
  110. public IActionResult CreateAdminNotification(
  111. [FromQuery] string name,
  112. [FromQuery] string description,
  113. [FromQuery] string? url,
  114. [FromQuery] NotificationLevel? level)
  115. {
  116. try
  117. {
  118. var notification = new NotificationRequest
  119. {
  120. Name = name,
  121. Description = description,
  122. Url = url,
  123. Level = level ?? NotificationLevel.Normal,
  124. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  125. Date = DateTime.UtcNow,
  126. };
  127. _notificationManager.SendNotification(notification, CancellationToken.None);
  128. return Ok();
  129. }
  130. catch (Exception e)
  131. {
  132. return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
  133. }
  134. }
  135. /// <summary>
  136. /// Endpoint to set notifications as read.
  137. /// </summary>
  138. /// <param name="userId">The userID.</param>
  139. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  140. /// <returns>Status.</returns>
  141. [HttpPost("{UserID}/Read")]
  142. [ProducesResponseType(StatusCodes.Status200OK)]
  143. public IActionResult SetRead(
  144. [FromRoute] string userId,
  145. [FromQuery] string ids)
  146. {
  147. return Ok();
  148. }
  149. /// <summary>
  150. /// Endpoint to set notifications as unread.
  151. /// </summary>
  152. /// <param name="userId">The userID.</param>
  153. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
  154. /// <returns>Status.</returns>
  155. [HttpPost("{UserID}/Unread")]
  156. [ProducesResponseType(StatusCodes.Status200OK)]
  157. public IActionResult SetUnread(
  158. [FromRoute] string userId,
  159. [FromQuery] string ids)
  160. {
  161. return Ok();
  162. }
  163. }
  164. }