2
0

NotificationsController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.Mvc;
  13. namespace Jellyfin.Api.Controllers
  14. {
  15. /// <summary>
  16. /// The notification controller.
  17. /// </summary>
  18. public class NotificationsController : BaseJellyfinApiController
  19. {
  20. private readonly INotificationManager _notificationManager;
  21. private readonly IUserManager _userManager;
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="NotificationsController" /> class.
  24. /// </summary>
  25. /// <param name="notificationManager">The notification manager.</param>
  26. /// <param name="userManager">The user manager.</param>
  27. public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
  28. {
  29. _notificationManager = notificationManager;
  30. _userManager = userManager;
  31. }
  32. /// <summary>
  33. /// Endpoint for getting a user's notifications.
  34. /// </summary>
  35. /// <param name="userID">The UserID.</param>
  36. /// <param name="isRead">An optional filter by IsRead.</param>
  37. /// <param name="startIndex">The optional index to start at. All notifications with a lower index will be dropped from the results.</param>
  38. /// <param name="limit">An optional limit on the number of notifications returned.</param>
  39. /// <returns>A read-only list of all of the user's notifications.</returns>
  40. [HttpGet("{UserID}")]
  41. public IReadOnlyList<NotificationDto> GetNotifications(
  42. [FromRoute] string userID,
  43. [FromQuery] bool? isRead,
  44. [FromQuery] int? startIndex,
  45. [FromQuery] int? limit)
  46. {
  47. return new List<NotificationDto>();
  48. }
  49. /// <summary>
  50. /// Endpoint for getting a user's notification summary.
  51. /// </summary>
  52. /// <param name="userID">The userID.</param>
  53. /// <returns>Notifications summary for the user.</returns>
  54. [HttpGet("{UserID}/Summary")]
  55. public NotificationsSummaryDto GetNotificationsSummary(
  56. [FromRoute] string userID)
  57. {
  58. return new NotificationsSummaryDto();
  59. }
  60. /// <summary>
  61. /// Endpoint for getting notification types.
  62. /// </summary>
  63. /// <returns>All notification types.</returns>
  64. [HttpGet("Types")]
  65. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  66. {
  67. return _notificationManager.GetNotificationTypes();
  68. }
  69. /// <summary>
  70. /// Endpoint for getting notification services.
  71. /// </summary>
  72. /// <returns>All notification services.</returns>
  73. [HttpGet("Services")]
  74. public IEnumerable<NameIdPair> GetNotificationServices()
  75. {
  76. return _notificationManager.GetNotificationServices();
  77. }
  78. /// <summary>
  79. /// Endpoint to send a notification to all admins.
  80. /// </summary>
  81. /// <param name="name">The name of the notification.</param>
  82. /// <param name="description">The description of the notification.</param>
  83. /// <param name="url">The URL of the notification.</param>
  84. /// <param name="level">The level of the notification.</param>
  85. [HttpPost("Admin")]
  86. public void CreateAdminNotification(
  87. [FromForm] string name,
  88. [FromForm] string description,
  89. [FromForm] string? url,
  90. [FromForm] NotificationLevel level)
  91. {
  92. var notification = new NotificationRequest
  93. {
  94. Name = name,
  95. Description = description,
  96. Url = url,
  97. Level = level,
  98. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  99. Date = DateTime.UtcNow,
  100. };
  101. _notificationManager.SendNotification(notification, CancellationToken.None);
  102. }
  103. /// <summary>
  104. /// Endpoint to set notifications as read.
  105. /// </summary>
  106. /// <param name="userID">The userID.</param>
  107. /// <param name="ids">The IDs of notifications which should be set as read.</param>
  108. [HttpPost("{UserID}/Read")]
  109. public void SetRead(
  110. [FromRoute] string userID,
  111. [FromForm] List<string> ids)
  112. {
  113. }
  114. /// <summary>
  115. /// Endpoint to set notifications as unread.
  116. /// </summary>
  117. /// <param name="userID">The userID.</param>
  118. /// <param name="ids">The IDs of notifications which should be set as unread.</param>
  119. [HttpPost("{UserID}/Unread")]
  120. public void SetUnread(
  121. [FromRoute] string userID,
  122. [FromForm] List<string> ids)
  123. {
  124. }
  125. }
  126. }