NotificationsController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #nullable enable
  2. #pragma warning disable CA1801
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. using Jellyfin.Api.Models.NotificationDtos;
  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. /// Endpoint for getting a user's notifications.
  35. /// </summary>
  36. /// <param name="userId">The user's ID.</param>
  37. /// <param name="isRead">An optional filter by IsRead.</param>
  38. /// <param name="startIndex">The optional index to start at. All notifications with a lower index will be dropped from the results.</param>
  39. /// <param name="limit">An optional limit on the number of notifications returned.</param>
  40. /// <returns>A read-only list of all of the user's notifications.</returns>
  41. [HttpGet("{UserID}")]
  42. [ProducesResponseType(typeof(IEnumerable<NotificationResultDto>), StatusCodes.Status200OK)]
  43. public IActionResult GetNotifications(
  44. [FromRoute] string userId,
  45. [FromQuery] bool? isRead,
  46. [FromQuery] int? startIndex,
  47. [FromQuery] int? limit)
  48. {
  49. return Ok(new NotificationResultDto());
  50. }
  51. /// <summary>
  52. /// Endpoint for getting a user's notification summary.
  53. /// </summary>
  54. /// <param name="userId">The user's ID.</param>
  55. /// <returns>Notifications summary for the user.</returns>
  56. [HttpGet("{UserID}/Summary")]
  57. [ProducesResponseType(typeof(NotificationsSummaryDto), StatusCodes.Status200OK)]
  58. public IActionResult GetNotificationsSummary(
  59. [FromRoute] string userId)
  60. {
  61. return Ok(new NotificationsSummaryDto());
  62. }
  63. /// <summary>
  64. /// Endpoint for getting notification types.
  65. /// </summary>
  66. /// <returns>All notification types.</returns>
  67. [HttpGet("Types")]
  68. [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
  69. public IActionResult GetNotificationTypes()
  70. {
  71. return Ok(_notificationManager.GetNotificationTypes());
  72. }
  73. /// <summary>
  74. /// Endpoint for getting notification services.
  75. /// </summary>
  76. /// <returns>All notification services.</returns>
  77. [HttpGet("Services")]
  78. [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
  79. public IActionResult GetNotificationServices()
  80. {
  81. return Ok(_notificationManager.GetNotificationServices());
  82. }
  83. /// <summary>
  84. /// Endpoint to send a notification to all admins.
  85. /// </summary>
  86. /// <param name="name">The name of the notification.</param>
  87. /// <param name="description">The description of the notification.</param>
  88. /// <param name="url">The URL of the notification.</param>
  89. /// <param name="level">The level of the notification.</param>
  90. /// <returns>Status.</returns>
  91. [HttpPost("Admin")]
  92. [ProducesResponseType(StatusCodes.Status200OK)]
  93. public void CreateAdminNotification(
  94. [FromQuery] string name,
  95. [FromQuery] string description,
  96. [FromQuery] string? url,
  97. [FromQuery] NotificationLevel? level)
  98. {
  99. var notification = new NotificationRequest
  100. {
  101. Name = name,
  102. Description = description,
  103. Url = url,
  104. Level = level ?? NotificationLevel.Normal,
  105. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  106. Date = DateTime.UtcNow,
  107. };
  108. _notificationManager.SendNotification(notification, CancellationToken.None);
  109. }
  110. /// <summary>
  111. /// Endpoint to set notifications as read.
  112. /// </summary>
  113. /// <param name="userId">The userID.</param>
  114. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  115. [HttpPost("{UserID}/Read")]
  116. [ProducesResponseType(StatusCodes.Status200OK)]
  117. public void SetRead(
  118. [FromRoute] string userId,
  119. [FromQuery] string ids)
  120. {
  121. }
  122. /// <summary>
  123. /// Endpoint to set notifications as unread.
  124. /// </summary>
  125. /// <param name="userId">The userID.</param>
  126. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
  127. [HttpPost("{UserID}/Unread")]
  128. [ProducesResponseType(StatusCodes.Status200OK)]
  129. public void SetUnread(
  130. [FromRoute] string userId,
  131. [FromQuery] string ids)
  132. {
  133. }
  134. }
  135. }