NotificationsController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /// <response code="200">Notifications returned.</response>
  37. /// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
  38. [HttpGet("{userId}")]
  39. [ProducesResponseType(StatusCodes.Status200OK)]
  40. public ActionResult<NotificationResultDto> GetNotifications()
  41. {
  42. return new NotificationResultDto();
  43. }
  44. /// <summary>
  45. /// Gets a user's notification summary.
  46. /// </summary>
  47. /// <response code="200">Summary of user's notifications returned.</response>
  48. /// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
  49. [HttpGet("{userId}/Summary")]
  50. [ProducesResponseType(StatusCodes.Status200OK)]
  51. public ActionResult<NotificationsSummaryDto> GetNotificationsSummary()
  52. {
  53. return new NotificationsSummaryDto();
  54. }
  55. /// <summary>
  56. /// Gets notification types.
  57. /// </summary>
  58. /// <response code="200">All notification types returned.</response>
  59. /// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
  60. [HttpGet("Types")]
  61. [ProducesResponseType(StatusCodes.Status200OK)]
  62. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  63. {
  64. return _notificationManager.GetNotificationTypes();
  65. }
  66. /// <summary>
  67. /// Gets notification services.
  68. /// </summary>
  69. /// <response code="200">All notification services returned.</response>
  70. /// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
  71. [HttpGet("Services")]
  72. [ProducesResponseType(StatusCodes.Status200OK)]
  73. public IEnumerable<NameIdPair> GetNotificationServices()
  74. {
  75. return _notificationManager.GetNotificationServices();
  76. }
  77. /// <summary>
  78. /// Sends a notification to all admins.
  79. /// </summary>
  80. /// <param name="name">The name of the notification.</param>
  81. /// <param name="description">The description of the notification.</param>
  82. /// <param name="url">The URL of the notification.</param>
  83. /// <param name="level">The level of the notification.</param>
  84. /// <response code="204">Notification sent.</response>
  85. /// <returns>A <cref see="NoContentResult"/>.</returns>
  86. [HttpPost("Admin")]
  87. [ProducesResponseType(StatusCodes.Status204NoContent)]
  88. public ActionResult CreateAdminNotification(
  89. [FromQuery] string name,
  90. [FromQuery] string description,
  91. [FromQuery] string? url,
  92. [FromQuery] NotificationLevel? level)
  93. {
  94. var notification = new NotificationRequest
  95. {
  96. Name = name,
  97. Description = description,
  98. Url = url,
  99. Level = level ?? NotificationLevel.Normal,
  100. UserIds = _userManager.Users
  101. .Where(user => user.HasPermission(PermissionKind.IsAdministrator))
  102. .Select(user => user.Id)
  103. .ToArray(),
  104. Date = DateTime.UtcNow,
  105. };
  106. _notificationManager.SendNotification(notification, CancellationToken.None);
  107. return NoContent();
  108. }
  109. /// <summary>
  110. /// Sets notifications as read.
  111. /// </summary>
  112. /// <response code="204">Notifications set as read.</response>
  113. /// <returns>A <cref see="NoContentResult"/>.</returns>
  114. [HttpPost("{userId}/Read")]
  115. [ProducesResponseType(StatusCodes.Status204NoContent)]
  116. public ActionResult SetRead()
  117. {
  118. return NoContent();
  119. }
  120. /// <summary>
  121. /// Sets notifications as unread.
  122. /// </summary>
  123. /// <response code="204">Notifications set as unread.</response>
  124. /// <returns>A <cref see="NoContentResult"/>.</returns>
  125. [HttpPost("{userId}/Unread")]
  126. [ProducesResponseType(StatusCodes.Status204NoContent)]
  127. public ActionResult SetUnread()
  128. {
  129. return NoContent();
  130. }
  131. }
  132. }