NotificationsService.cs 8.1 KB

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