NotificationsController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Linq;
  5. using System.Threading;
  6. using Jellyfin.Api.Models.NotificationDtos;
  7. using Jellyfin.Data.Enums;
  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. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Imported from ServiceStack")]
  45. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isRead", Justification = "Imported from ServiceStack")]
  46. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "startIndex", Justification = "Imported from ServiceStack")]
  47. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "limit", Justification = "Imported from ServiceStack")]
  48. public ActionResult<NotificationResultDto> GetNotifications(
  49. [FromRoute] string userId,
  50. [FromQuery] bool? isRead,
  51. [FromQuery] int? startIndex,
  52. [FromQuery] int? limit)
  53. {
  54. return new NotificationResultDto();
  55. }
  56. /// <summary>
  57. /// Gets a user's notification summary.
  58. /// </summary>
  59. /// <param name="userId">The user's ID.</param>
  60. /// <response code="200">Summary of user's notifications returned.</response>
  61. /// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
  62. [HttpGet("{userId}/Summary")]
  63. [ProducesResponseType(StatusCodes.Status200OK)]
  64. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Imported from ServiceStack")]
  65. public ActionResult<NotificationsSummaryDto> GetNotificationsSummary(
  66. [FromRoute] string userId)
  67. {
  68. return new NotificationsSummaryDto();
  69. }
  70. /// <summary>
  71. /// Gets notification types.
  72. /// </summary>
  73. /// <response code="200">All notification types returned.</response>
  74. /// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
  75. [HttpGet("Types")]
  76. [ProducesResponseType(StatusCodes.Status200OK)]
  77. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  78. {
  79. return _notificationManager.GetNotificationTypes();
  80. }
  81. /// <summary>
  82. /// Gets notification services.
  83. /// </summary>
  84. /// <response code="200">All notification services returned.</response>
  85. /// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
  86. [HttpGet("Services")]
  87. [ProducesResponseType(StatusCodes.Status200OK)]
  88. public IEnumerable<NameIdPair> GetNotificationServices()
  89. {
  90. return _notificationManager.GetNotificationServices();
  91. }
  92. /// <summary>
  93. /// Sends a notification to all admins.
  94. /// </summary>
  95. /// <param name="name">The name of the notification.</param>
  96. /// <param name="description">The description of the notification.</param>
  97. /// <param name="url">The URL of the notification.</param>
  98. /// <param name="level">The level of the notification.</param>
  99. /// <response code="204">Notification sent.</response>
  100. /// <returns>A <cref see="NoContentResult"/>.</returns>
  101. [HttpPost("Admin")]
  102. [ProducesResponseType(StatusCodes.Status204NoContent)]
  103. public ActionResult CreateAdminNotification(
  104. [FromQuery] string name,
  105. [FromQuery] string description,
  106. [FromQuery] string? url,
  107. [FromQuery] NotificationLevel? level)
  108. {
  109. var notification = new NotificationRequest
  110. {
  111. Name = name,
  112. Description = description,
  113. Url = url,
  114. Level = level ?? NotificationLevel.Normal,
  115. UserIds = _userManager.Users
  116. .Where(user => user.HasPermission(PermissionKind.IsAdministrator))
  117. .Select(user => user.Id)
  118. .ToArray(),
  119. Date = DateTime.UtcNow,
  120. };
  121. _notificationManager.SendNotification(notification, CancellationToken.None);
  122. return NoContent();
  123. }
  124. /// <summary>
  125. /// Sets notifications as read.
  126. /// </summary>
  127. /// <param name="userId">The userID.</param>
  128. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  129. /// <response code="204">Notifications set as read.</response>
  130. /// <returns>A <cref see="NoContentResult"/>.</returns>
  131. [HttpPost("{userId}/Read")]
  132. [ProducesResponseType(StatusCodes.Status204NoContent)]
  133. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Imported from ServiceStack")]
  134. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "ids", Justification = "Imported from ServiceStack")]
  135. public ActionResult SetRead(
  136. [FromRoute] string userId,
  137. [FromQuery] string ids)
  138. {
  139. return NoContent();
  140. }
  141. /// <summary>
  142. /// Sets notifications as unread.
  143. /// </summary>
  144. /// <param name="userId">The userID.</param>
  145. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
  146. /// <response code="204">Notifications set as unread.</response>
  147. /// <returns>A <cref see="NoContentResult"/>.</returns>
  148. [HttpPost("{userId}/Unread")]
  149. [ProducesResponseType(StatusCodes.Status204NoContent)]
  150. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "userId", Justification = "Imported from ServiceStack")]
  151. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "ids", Justification = "Imported from ServiceStack")]
  152. public ActionResult SetUnread(
  153. [FromRoute] string userId,
  154. [FromQuery] string ids)
  155. {
  156. return NoContent();
  157. }
  158. }
  159. }