NotificationsService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using MediaBrowser.Controller.Notifications;
  2. using MediaBrowser.Model.Notifications;
  3. using ServiceStack;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Api
  9. {
  10. [Route("/Notifications/{UserId}", "GET", Summary = "Gets notifications")]
  11. public class GetNotifications : IReturn<NotificationResult>
  12. {
  13. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  14. public string UserId { get; set; }
  15. [ApiMember(Name = "IsRead", Description = "An optional filter by IsRead", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  16. public bool? IsRead { get; set; }
  17. [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")]
  18. public int? StartIndex { get; set; }
  19. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  20. public int? Limit { get; set; }
  21. }
  22. [Route("/Notifications/{UserId}/Summary", "GET", Summary = "Gets a notification summary for a user")]
  23. public class GetNotificationsSummary : IReturn<NotificationsSummary>
  24. {
  25. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  26. public string UserId { get; set; }
  27. }
  28. [Route("/Notifications/{UserId}", "POST", Summary = "Adds a notifications")]
  29. public class AddUserNotification : IReturnVoid
  30. {
  31. [ApiMember(Name = "Id", Description = "The Id of the new notification. If unspecified one will be provided.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  32. public string Id { get; set; }
  33. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  34. public string UserId { get; set; }
  35. [ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  36. public string Name { get; set; }
  37. [ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  38. public string Description { get; set; }
  39. [ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  40. public string Url { get; set; }
  41. [ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  42. public NotificationLevel Level { get; set; }
  43. }
  44. [Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
  45. public class MarkRead : IReturnVoid
  46. {
  47. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  48. public string UserId { get; set; }
  49. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  50. public string Ids { get; set; }
  51. }
  52. [Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
  53. public class MarkUnread : 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. public class NotificationsService : BaseApiService
  61. {
  62. private readonly INotificationsRepository _notificationsRepo;
  63. private readonly INotificationManager _notificationManager;
  64. public NotificationsService(INotificationsRepository notificationsRepo, INotificationManager notificationManager)
  65. {
  66. _notificationsRepo = notificationsRepo;
  67. _notificationManager = notificationManager;
  68. }
  69. public void Post(AddUserNotification request)
  70. {
  71. var task = AddNotification(request);
  72. Task.WaitAll(task);
  73. }
  74. public object Get(GetNotificationsSummary request)
  75. {
  76. var result = _notificationsRepo.GetNotificationsSummary(request.UserId);
  77. return result;
  78. }
  79. private async Task AddNotification(AddUserNotification request)
  80. {
  81. var notification = new NotificationRequest
  82. {
  83. Date = DateTime.UtcNow,
  84. Description = request.Description,
  85. Level = request.Level,
  86. Name = request.Name,
  87. Url = request.Url,
  88. UserIds = new List<string> { request.UserId }
  89. };
  90. await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
  91. }
  92. public void Post(MarkRead request)
  93. {
  94. var task = MarkRead(request.Ids, request.UserId, true);
  95. Task.WaitAll(task);
  96. }
  97. public void Post(MarkUnread request)
  98. {
  99. var task = MarkRead(request.Ids, request.UserId, false);
  100. Task.WaitAll(task);
  101. }
  102. private Task MarkRead(string idList, string userId, bool read)
  103. {
  104. var ids = idList.Split(',');
  105. return _notificationsRepo.MarkRead(ids, userId, read, CancellationToken.None);
  106. }
  107. public object Get(GetNotifications request)
  108. {
  109. var result = _notificationsRepo.GetNotifications(new NotificationQuery
  110. {
  111. IsRead = request.IsRead,
  112. Limit = request.Limit,
  113. StartIndex = request.StartIndex,
  114. UserId = request.UserId
  115. });
  116. return ToOptimizedResult(result);
  117. }
  118. }
  119. }