NotificationsController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using Jellyfin.Api.Models.NotificationDtos;
  6. using Jellyfin.Data.Enums;
  7. using MediaBrowser.Controller.Library;
  8. using MediaBrowser.Controller.Notifications;
  9. using MediaBrowser.Model.Dto;
  10. using MediaBrowser.Model.Notifications;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. namespace Jellyfin.Api.Controllers
  14. {
  15. /// <summary>
  16. /// The notification controller.
  17. /// </summary>
  18. public class NotificationsController : BaseJellyfinApiController
  19. {
  20. private readonly INotificationManager _notificationManager;
  21. private readonly IUserManager _userManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="NotificationsController" /> class.
  24. /// </summary>
  25. /// <param name="notificationManager">The notification manager.</param>
  26. /// <param name="userManager">The user manager.</param>
  27. public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
  28. {
  29. _notificationManager = notificationManager;
  30. _userManager = userManager;
  31. }
  32. /// <summary>
  33. /// Gets a user's notifications.
  34. /// </summary>
  35. /// <response code="200">Notifications returned.</response>
  36. /// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
  37. [HttpGet("{userId}")]
  38. [ProducesResponseType(StatusCodes.Status200OK)]
  39. public ActionResult<NotificationResultDto> GetNotifications()
  40. {
  41. return new NotificationResultDto();
  42. }
  43. /// <summary>
  44. /// Gets a user's notification summary.
  45. /// </summary>
  46. /// <response code="200">Summary of user's notifications returned.</response>
  47. /// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
  48. [HttpGet("{userId}/Summary")]
  49. [ProducesResponseType(StatusCodes.Status200OK)]
  50. public ActionResult<NotificationsSummaryDto> GetNotificationsSummary()
  51. {
  52. return new NotificationsSummaryDto();
  53. }
  54. /// <summary>
  55. /// Gets notification types.
  56. /// </summary>
  57. /// <response code="200">All notification types returned.</response>
  58. /// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
  59. [HttpGet("Types")]
  60. [ProducesResponseType(StatusCodes.Status200OK)]
  61. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  62. {
  63. return _notificationManager.GetNotificationTypes();
  64. }
  65. /// <summary>
  66. /// Gets notification services.
  67. /// </summary>
  68. /// <response code="200">All notification services returned.</response>
  69. /// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
  70. [HttpGet("Services")]
  71. [ProducesResponseType(StatusCodes.Status200OK)]
  72. public IEnumerable<NameIdPair> GetNotificationServices()
  73. {
  74. return _notificationManager.GetNotificationServices();
  75. }
  76. /// <summary>
  77. /// Sends a notification to all admins.
  78. /// </summary>
  79. /// <param name="name">The name of the notification.</param>
  80. /// <param name="description">The description of the notification.</param>
  81. /// <param name="url">The URL of the notification.</param>
  82. /// <param name="level">The level of the notification.</param>
  83. /// <response code="204">Notification sent.</response>
  84. /// <returns>A <cref see="NoContentResult"/>.</returns>
  85. [HttpPost("Admin")]
  86. [ProducesResponseType(StatusCodes.Status204NoContent)]
  87. public ActionResult CreateAdminNotification(
  88. [FromQuery] string? name,
  89. [FromQuery] string? description,
  90. [FromQuery] string? url,
  91. [FromQuery] NotificationLevel? level)
  92. {
  93. var notification = new NotificationRequest
  94. {
  95. Name = name,
  96. Description = description,
  97. Url = url,
  98. Level = level ?? NotificationLevel.Normal,
  99. UserIds = _userManager.Users
  100. .Where(user => user.HasPermission(PermissionKind.IsAdministrator))
  101. .Select(user => user.Id)
  102. .ToArray(),
  103. Date = DateTime.UtcNow,
  104. };
  105. _notificationManager.SendNotification(notification, CancellationToken.None);
  106. return NoContent();
  107. }
  108. /// <summary>
  109. /// Sets notifications as read.
  110. /// </summary>
  111. /// <response code="204">Notifications set as read.</response>
  112. /// <returns>A <cref see="NoContentResult"/>.</returns>
  113. [HttpPost("{userId}/Read")]
  114. [ProducesResponseType(StatusCodes.Status204NoContent)]
  115. public ActionResult SetRead()
  116. {
  117. return NoContent();
  118. }
  119. /// <summary>
  120. /// Sets notifications as unread.
  121. /// </summary>
  122. /// <response code="204">Notifications set as unread.</response>
  123. /// <returns>A <cref see="NoContentResult"/>.</returns>
  124. [HttpPost("{userId}/Unread")]
  125. [ProducesResponseType(StatusCodes.Status204NoContent)]
  126. public ActionResult SetUnread()
  127. {
  128. return NoContent();
  129. }
  130. }
  131. }