NotificationsController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Jellyfin.Data.Enums;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Notifications;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Notifications;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.AspNetCore.Mvc;
  15. namespace Jellyfin.Api.Controllers
  16. {
  17. /// <summary>
  18. /// The notification controller.
  19. /// </summary>
  20. public class NotificationsController : BaseJellyfinApiController
  21. {
  22. private readonly INotificationManager _notificationManager;
  23. private readonly IUserManager _userManager;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="NotificationsController" /> class.
  26. /// </summary>
  27. /// <param name="notificationManager">The notification manager.</param>
  28. /// <param name="userManager">The user manager.</param>
  29. public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
  30. {
  31. _notificationManager = notificationManager;
  32. _userManager = userManager;
  33. }
  34. /// <summary>
  35. /// Gets a user's notifications.
  36. /// </summary>
  37. /// <param name="userId">The user's ID.</param>
  38. /// <param name="isRead">An optional filter by notification read state.</param>
  39. /// <param name="startIndex">The optional index to start at. All notifications with a lower index will be omitted from the results.</param>
  40. /// <param name="limit">An optional limit on the number of notifications returned.</param>
  41. /// <response code="200">Notifications returned.</response>
  42. /// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
  43. [HttpGet("{UserID}")]
  44. [ProducesResponseType(StatusCodes.Status200OK)]
  45. public ActionResult<NotificationResultDto> GetNotifications(
  46. [FromRoute] string userId,
  47. [FromQuery] bool? isRead,
  48. [FromQuery] int? startIndex,
  49. [FromQuery] int? limit)
  50. {
  51. return new NotificationResultDto();
  52. }
  53. /// <summary>
  54. /// Gets a user's notification summary.
  55. /// </summary>
  56. /// <param name="userId">The user's ID.</param>
  57. /// <response code="200">Summary of user's notifications returned.</response>
  58. /// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
  59. [HttpGet("{UserID}/Summary")]
  60. [ProducesResponseType(StatusCodes.Status200OK)]
  61. public ActionResult<NotificationsSummaryDto> GetNotificationsSummary(
  62. [FromRoute] string userId)
  63. {
  64. return new NotificationsSummaryDto();
  65. }
  66. /// <summary>
  67. /// Gets notification types.
  68. /// </summary>
  69. /// <response code="200">All notification types returned.</response>
  70. /// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
  71. [HttpGet("Types")]
  72. [ProducesResponseType(StatusCodes.Status200OK)]
  73. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  74. {
  75. return _notificationManager.GetNotificationTypes();
  76. }
  77. /// <summary>
  78. /// Gets notification services.
  79. /// </summary>
  80. /// <response code="200">All notification services returned.</response>
  81. /// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
  82. [HttpGet("Services")]
  83. [ProducesResponseType(StatusCodes.Status200OK)]
  84. public IEnumerable<NameIdPair> GetNotificationServices()
  85. {
  86. return _notificationManager.GetNotificationServices();
  87. }
  88. /// <summary>
  89. /// Sends a notification to all admins.
  90. /// </summary>
  91. /// <param name="name">The name of the notification.</param>
  92. /// <param name="description">The description of the notification.</param>
  93. /// <param name="url">The URL of the notification.</param>
  94. /// <param name="level">The level of the notification.</param>
  95. /// <response code="204">Notification sent.</response>
  96. /// <returns>A <cref see="NoContentResult"/>.</returns>
  97. [HttpPost("Admin")]
  98. [ProducesResponseType(StatusCodes.Status204NoContent)]
  99. public ActionResult CreateAdminNotification(
  100. [FromQuery] string name,
  101. [FromQuery] string description,
  102. [FromQuery] string? url,
  103. [FromQuery] NotificationLevel? level)
  104. {
  105. var notification = new NotificationRequest
  106. {
  107. Name = name,
  108. Description = description,
  109. Url = url,
  110. Level = level ?? NotificationLevel.Normal,
  111. UserIds = _userManager.Users
  112. .Where(user => user.HasPermission(PermissionKind.IsAdministrator))
  113. .Select(user => user.Id)
  114. .ToArray(),
  115. Date = DateTime.UtcNow,
  116. };
  117. _notificationManager.SendNotification(notification, CancellationToken.None);
  118. return NoContent();
  119. }
  120. /// <summary>
  121. /// Sets notifications as read.
  122. /// </summary>
  123. /// <param name="userId">The userID.</param>
  124. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  125. /// <response code="204">Notifications set as read.</response>
  126. /// <returns>A <cref see="NoContentResult"/>.</returns>
  127. [HttpPost("{UserID}/Read")]
  128. [ProducesResponseType(StatusCodes.Status204NoContent)]
  129. public ActionResult SetRead(
  130. [FromRoute] string userId,
  131. [FromQuery] string ids)
  132. {
  133. return NoContent();
  134. }
  135. /// <summary>
  136. /// Sets notifications as unread.
  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 unread.</param>
  140. /// <response code="204">Notifications set as unread.</response>
  141. /// <returns>A <cref see="NoContentResult"/>.</returns>
  142. [HttpPost("{UserID}/Unread")]
  143. [ProducesResponseType(StatusCodes.Status204NoContent)]
  144. public ActionResult SetUnread(
  145. [FromRoute] string userId,
  146. [FromQuery] string ids)
  147. {
  148. return NoContent();
  149. }
  150. }
  151. }