NotificationsController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #nullable enable
  2. #pragma warning disable CA1801
  3. #pragma warning disable SA1313
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using Jellyfin.Api.Models.NotificationDtos;
  9. using MediaBrowser.Controller.Library;
  10. using MediaBrowser.Controller.Notifications;
  11. using MediaBrowser.Model.Dto;
  12. using MediaBrowser.Model.Notifications;
  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 UserID.</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. public IReadOnlyList<NotificationDto> GetNotifications(
  43. [FromRoute] string UserID,
  44. [FromQuery] bool? IsRead,
  45. [FromQuery] int? StartIndex,
  46. [FromQuery] int? Limit)
  47. {
  48. return new List<NotificationDto>();
  49. }
  50. /// <summary>
  51. /// Endpoint for getting a user's notification summary.
  52. /// </summary>
  53. /// <param name="UserID">The UserID.</param>
  54. /// <returns>Notifications summary for the user.</returns>
  55. [HttpGet("{UserId}/Summary")]
  56. public NotificationsSummaryDto GetNotificationsSummary(
  57. [FromRoute] string UserID)
  58. {
  59. return new NotificationsSummaryDto();
  60. }
  61. /// <summary>
  62. /// Endpoint for getting notification types.
  63. /// </summary>
  64. /// <returns>All notification types.</returns>
  65. [HttpGet("Types")]
  66. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  67. {
  68. return _notificationManager.GetNotificationTypes();
  69. }
  70. /// <summary>
  71. /// Endpoint for getting notification services.
  72. /// </summary>
  73. /// <returns>All notification services.</returns>
  74. [HttpGet("Services")]
  75. public IEnumerable<NameIdPair> GetNotificationServices()
  76. {
  77. return _notificationManager.GetNotificationServices();
  78. }
  79. /// <summary>
  80. /// Endpoint to send a notification to all admins.
  81. /// </summary>
  82. /// <param name="Name">The name of the notification.</param>
  83. /// <param name="Description">The description of the notification.</param>
  84. /// <param name="URL">The URL of the notification.</param>
  85. /// <param name="Level">The level of the notification.</param>
  86. [HttpPost("Admin")]
  87. public void CreateAdminNotification(
  88. [FromForm] string Name,
  89. [FromForm] string Description,
  90. [FromForm] string? URL,
  91. [FromForm] NotificationLevel Level)
  92. {
  93. var notification = new NotificationRequest
  94. {
  95. Name = Name,
  96. Description = Description,
  97. Url = URL,
  98. Level = Level,
  99. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  100. Date = DateTime.UtcNow,
  101. };
  102. _notificationManager.SendNotification(notification, CancellationToken.None);
  103. }
  104. /// <summary>
  105. /// Endpoint to set notifications as read.
  106. /// </summary>
  107. /// <param name="UserID">The UserID.</param>
  108. /// <param name="IDs">The IDs of notifications which should be set as read.</param>
  109. [HttpPost("{UserID}/Read")]
  110. public void SetRead(
  111. [FromRoute] string UserID,
  112. [FromForm] List<string> IDs)
  113. {
  114. }
  115. /// <summary>
  116. /// Endpoint to set notifications as unread.
  117. /// </summary>
  118. /// <param name="UserID">The UserID.</param>
  119. /// <param name="IDs">The IDs of notifications which should be set as unread.</param>
  120. [HttpPost("{UserID}/Unread")]
  121. public void SetUnread(
  122. [FromRoute] string UserID,
  123. [FromForm] List<string> IDs)
  124. {
  125. }
  126. }
  127. }