NotificationsService.cs 7.5 KB

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