NotificationsController.cs 5.6 KB

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