2
0

NotificationsController.cs 5.5 KB

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