NotificationsController.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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="notificationDto">The notification request.</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([FromBody, Required] AdminNotificationDto notificationDto)
  89. {
  90. var notification = new NotificationRequest
  91. {
  92. Name = notificationDto.Name,
  93. Description = notificationDto.Description,
  94. Url = notificationDto.Url,
  95. Level = notificationDto.NotificationLevel ?? NotificationLevel.Normal,
  96. UserIds = _userManager.Users
  97. .Where(user => user.HasPermission(PermissionKind.IsAdministrator))
  98. .Select(user => user.Id)
  99. .ToArray(),
  100. Date = DateTime.UtcNow,
  101. };
  102. _notificationManager.SendNotification(notification, CancellationToken.None);
  103. return NoContent();
  104. }
  105. /// <summary>
  106. /// Sets notifications as read.
  107. /// </summary>
  108. /// <response code="204">Notifications set as read.</response>
  109. /// <returns>A <cref see="NoContentResult"/>.</returns>
  110. [HttpPost("{userId}/Read")]
  111. [ProducesResponseType(StatusCodes.Status204NoContent)]
  112. public ActionResult SetRead()
  113. {
  114. return NoContent();
  115. }
  116. /// <summary>
  117. /// Sets notifications as unread.
  118. /// </summary>
  119. /// <response code="204">Notifications set as unread.</response>
  120. /// <returns>A <cref see="NoContentResult"/>.</returns>
  121. [HttpPost("{userId}/Unread")]
  122. [ProducesResponseType(StatusCodes.Status204NoContent)]
  123. public ActionResult SetUnread()
  124. {
  125. return NoContent();
  126. }
  127. }
  128. }