2
0

NotificationsService.cs 7.3 KB

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