NotificationsService.cs 6.9 KB

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