NotificationsController.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /// Gets a user's notifications.
  35. /// </summary>
  36. /// <param name="userId">The user's ID.</param>
  37. /// <param name="isRead">An optional filter by notification read state.</param>
  38. /// <param name="startIndex">The optional index to start at. All notifications with a lower index will be omitted from the results.</param>
  39. /// <param name="limit">An optional limit on the number of notifications returned.</param>
  40. /// <response code="200">Notifications returned.</response>
  41. /// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
  42. [HttpGet("{UserID}")]
  43. [ProducesResponseType(StatusCodes.Status200OK)]
  44. public ActionResult<NotificationResultDto> GetNotifications(
  45. [FromRoute] string userId,
  46. [FromQuery] bool? isRead,
  47. [FromQuery] int? startIndex,
  48. [FromQuery] int? limit)
  49. {
  50. return new NotificationResultDto();
  51. }
  52. /// <summary>
  53. /// Gets a user's notification summary.
  54. /// </summary>
  55. /// <param name="userId">The user's ID.</param>
  56. /// <response code="200">Summary of user's notifications returned.</response>
  57. /// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
  58. [HttpGet("{UserID}/Summary")]
  59. [ProducesResponseType(StatusCodes.Status200OK)]
  60. public ActionResult<NotificationsSummaryDto> GetNotificationsSummary(
  61. [FromRoute] string userId)
  62. {
  63. return new NotificationsSummaryDto();
  64. }
  65. /// <summary>
  66. /// Gets notification types.
  67. /// </summary>
  68. /// <response code="200">All notification types returned.</response>
  69. /// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
  70. [HttpGet("Types")]
  71. [ProducesResponseType(StatusCodes.Status200OK)]
  72. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  73. {
  74. return _notificationManager.GetNotificationTypes();
  75. }
  76. /// <summary>
  77. /// Gets notification services.
  78. /// </summary>
  79. /// <response code="200">All notification services returned.</response>
  80. /// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
  81. [HttpGet("Services")]
  82. [ProducesResponseType(StatusCodes.Status200OK)]
  83. public IEnumerable<NameIdPair> GetNotificationServices()
  84. {
  85. return _notificationManager.GetNotificationServices();
  86. }
  87. /// <summary>
  88. /// Sends a notification to all admins.
  89. /// </summary>
  90. /// <param name="name">The name of the notification.</param>
  91. /// <param name="description">The description of the notification.</param>
  92. /// <param name="url">The URL of the notification.</param>
  93. /// <param name="level">The level of the notification.</param>
  94. /// <response code="200">Notification sent.</response>
  95. /// <returns>An <cref see="OkResult"/>.</returns>
  96. [HttpPost("Admin")]
  97. [ProducesResponseType(StatusCodes.Status200OK)]
  98. public ActionResult CreateAdminNotification(
  99. [FromQuery] string name,
  100. [FromQuery] string description,
  101. [FromQuery] string? url,
  102. [FromQuery] NotificationLevel? level)
  103. {
  104. var notification = new NotificationRequest
  105. {
  106. Name = name,
  107. Description = description,
  108. Url = url,
  109. Level = level ?? NotificationLevel.Normal,
  110. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  111. Date = DateTime.UtcNow,
  112. };
  113. _notificationManager.SendNotification(notification, CancellationToken.None);
  114. return Ok();
  115. }
  116. /// <summary>
  117. /// Sets notifications as read.
  118. /// </summary>
  119. /// <param name="userId">The userID.</param>
  120. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  121. /// <response code="200">Notifications set as read.</response>
  122. /// <returns>An <cref see="OkResult"/>.</returns>
  123. [HttpPost("{UserID}/Read")]
  124. [ProducesResponseType(StatusCodes.Status200OK)]
  125. public ActionResult SetRead(
  126. [FromRoute] string userId,
  127. [FromQuery] string ids)
  128. {
  129. return Ok();
  130. }
  131. /// <summary>
  132. /// Sets notifications as unread.
  133. /// </summary>
  134. /// <param name="userId">The userID.</param>
  135. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
  136. /// <response code="200">Notifications set as unread.</response>
  137. /// <returns>An <cref see="OkResult"/>.</returns>
  138. [HttpPost("{UserID}/Unread")]
  139. [ProducesResponseType(StatusCodes.Status200OK)]
  140. public ActionResult SetUnread(
  141. [FromRoute] string userId,
  142. [FromQuery] string ids)
  143. {
  144. return Ok();
  145. }
  146. }
  147. }