NotificationsController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(NotificationResultDto), StatusCodes.Status200OK)]
  43. public NotificationResultDto GetNotifications(
  44. [FromRoute] string userId,
  45. [FromQuery] bool? isRead,
  46. [FromQuery] int? startIndex,
  47. [FromQuery] int? limit)
  48. {
  49. return 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 NotificationsSummaryDto GetNotificationsSummary(
  59. [FromRoute] string userId)
  60. {
  61. return 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<NotificationTypeInfo>), StatusCodes.Status200OK)]
  69. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  70. {
  71. return _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 IEnumerable<NameIdPair> GetNotificationServices()
  80. {
  81. return _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. [HttpPost("Admin")]
  91. [ProducesResponseType(StatusCodes.Status200OK)]
  92. public void CreateAdminNotification(
  93. [FromQuery] string name,
  94. [FromQuery] string description,
  95. [FromQuery] string? url,
  96. [FromQuery] NotificationLevel? level)
  97. {
  98. var notification = new NotificationRequest
  99. {
  100. Name = name,
  101. Description = description,
  102. Url = url,
  103. Level = level ?? NotificationLevel.Normal,
  104. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
  105. Date = DateTime.UtcNow,
  106. };
  107. _notificationManager.SendNotification(notification, CancellationToken.None);
  108. }
  109. /// <summary>
  110. /// Endpoint to set notifications as read.
  111. /// </summary>
  112. /// <param name="userId">The userID.</param>
  113. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
  114. [HttpPost("{UserID}/Read")]
  115. [ProducesResponseType(StatusCodes.Status200OK)]
  116. public void SetRead(
  117. [FromRoute] string userId,
  118. [FromQuery] string ids)
  119. {
  120. }
  121. /// <summary>
  122. /// Endpoint to set notifications as unread.
  123. /// </summary>
  124. /// <param name="userId">The userID.</param>
  125. /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
  126. [HttpPost("{UserID}/Unread")]
  127. [ProducesResponseType(StatusCodes.Status200OK)]
  128. public void SetUnread(
  129. [FromRoute] string userId,
  130. [FromQuery] string ids)
  131. {
  132. }
  133. }
  134. }