NotificationsService.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1402
  3. #pragma warning disable SA1600
  4. #pragma warning disable SA1649
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics.CodeAnalysis;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using MediaBrowser.Controller.Library;
  12. using MediaBrowser.Controller.Net;
  13. using MediaBrowser.Controller.Notifications;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Notifications;
  16. using MediaBrowser.Model.Services;
  17. namespace Emby.Notifications.Api
  18. {
  19. [Route("/Notifications/{UserId}", "GET", Summary = "Gets notifications")]
  20. public class GetNotifications : IReturn<NotificationResult>
  21. {
  22. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  23. public string UserId { get; set; } = string.Empty;
  24. [ApiMember(Name = "IsRead", Description = "An optional filter by IsRead", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  25. public bool? IsRead { get; set; }
  26. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  27. public int? StartIndex { get; set; }
  28. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  29. public int? Limit { get; set; }
  30. }
  31. public class Notification
  32. {
  33. public string Id { get; set; } = string.Empty;
  34. public string UserId { get; set; } = string.Empty;
  35. public DateTime Date { get; set; }
  36. public bool IsRead { get; set; }
  37. public string Name { get; set; } = string.Empty;
  38. public string Description { get; set; } = string.Empty;
  39. public string Url { get; set; } = string.Empty;
  40. public NotificationLevel Level { get; set; }
  41. }
  42. public class NotificationResult
  43. {
  44. public IReadOnlyList<Notification> Notifications { get; set; } = Array.Empty<Notification>();
  45. public int TotalRecordCount { get; set; }
  46. }
  47. public class NotificationsSummary
  48. {
  49. public int UnreadCount { get; set; }
  50. public NotificationLevel MaxUnreadNotificationLevel { get; set; }
  51. }
  52. [Route("/Notifications/{UserId}/Summary", "GET", Summary = "Gets a notification summary for a user")]
  53. public class GetNotificationsSummary : IReturn<NotificationsSummary>
  54. {
  55. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  56. public string UserId { get; set; } = string.Empty;
  57. }
  58. [Route("/Notifications/Types", "GET", Summary = "Gets notification types")]
  59. public class GetNotificationTypes : IReturn<List<NotificationTypeInfo>>
  60. {
  61. }
  62. [Route("/Notifications/Services", "GET", Summary = "Gets notification types")]
  63. public class GetNotificationServices : IReturn<List<NameIdPair>>
  64. {
  65. }
  66. [Route("/Notifications/Admin", "POST", Summary = "Sends a notification to all admin users")]
  67. public class AddAdminNotification : IReturnVoid
  68. {
  69. [ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  70. public string Name { get; set; } = string.Empty;
  71. [ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  72. public string Description { get; set; } = string.Empty;
  73. [ApiMember(Name = "ImageUrl", Description = "The notification's image url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  74. public string? ImageUrl { get; set; }
  75. [ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  76. public string? Url { get; set; }
  77. [ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  78. public NotificationLevel Level { get; set; }
  79. }
  80. [Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
  81. public class MarkRead : IReturnVoid
  82. {
  83. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  84. public string UserId { get; set; } = string.Empty;
  85. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  86. public string Ids { get; set; } = string.Empty;
  87. }
  88. [Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
  89. public class MarkUnread : IReturnVoid
  90. {
  91. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  92. public string UserId { get; set; } = string.Empty;
  93. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  94. public string Ids { get; set; } = string.Empty;
  95. }
  96. [Authenticated]
  97. public class NotificationsService : IService
  98. {
  99. private readonly INotificationManager _notificationManager;
  100. private readonly IUserManager _userManager;
  101. public NotificationsService(INotificationManager notificationManager, IUserManager userManager)
  102. {
  103. _notificationManager = notificationManager;
  104. _userManager = userManager;
  105. }
  106. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  107. public object Get(GetNotificationTypes request)
  108. {
  109. return _notificationManager.GetNotificationTypes();
  110. }
  111. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  112. public object Get(GetNotificationServices request)
  113. {
  114. return _notificationManager.GetNotificationServices().ToList();
  115. }
  116. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  117. public object Get(GetNotificationsSummary request)
  118. {
  119. return new NotificationsSummary
  120. {
  121. };
  122. }
  123. public Task Post(AddAdminNotification request)
  124. {
  125. // This endpoint really just exists as post of a real with sickbeard
  126. var notification = new NotificationRequest
  127. {
  128. Date = DateTime.UtcNow,
  129. Description = request.Description,
  130. Level = request.Level,
  131. Name = request.Name,
  132. Url = request.Url,
  133. UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray()
  134. };
  135. return _notificationManager.SendNotification(notification, CancellationToken.None);
  136. }
  137. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  138. public void Post(MarkRead request)
  139. {
  140. }
  141. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  142. public void Post(MarkUnread request)
  143. {
  144. }
  145. [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request")]
  146. public object Get(GetNotifications request)
  147. {
  148. return new NotificationResult();
  149. }
  150. }
  151. }