NotificationsService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using MediaBrowser.Controller.Notifications;
  2. using MediaBrowser.Model.Notifications;
  3. using ServiceStack.ServiceHost;
  4. using System;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Api
  9. {
  10. [Route("/Notifications/{UserId}", "GET")]
  11. [Api(Description = "Gets notifications")]
  12. public class GetNotifications : IReturn<NotificationResult>
  13. {
  14. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  15. public string UserId { get; set; }
  16. [ApiMember(Name = "IsRead", Description = "An optional filter by IsRead", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  17. public bool? IsRead { get; set; }
  18. [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")]
  19. public int? StartIndex { get; set; }
  20. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  21. public int? Limit { get; set; }
  22. }
  23. [Route("/Notifications/{UserId}/Summary", "GET")]
  24. [Api(Description = "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 Guid UserId { get; set; }
  29. }
  30. [Route("/Notifications/{UserId}", "POST")]
  31. [Api(Description = "Adds a notifications")]
  32. public class AddNotification : IReturn<Notification>
  33. {
  34. [ApiMember(Name = "Id", Description = "The Id of the new notification. If unspecified one will be provided.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  35. public Guid? Id { get; set; }
  36. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  37. public Guid UserId { get; set; }
  38. [ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  39. public string Name { get; set; }
  40. [ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  41. public string Description { get; set; }
  42. [ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  43. public string Url { get; set; }
  44. [ApiMember(Name = "Category", Description = "The notification's category", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  45. public string Category { get; set; }
  46. [ApiMember(Name = "RelatedId", Description = "The notification's related id (item)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  47. public string RelatedId { get; set; }
  48. [ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  49. public NotificationLevel Level { get; set; }
  50. }
  51. [Route("/Notifications/{UserId}/Read", "POST")]
  52. [Api(Description = "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 Guid 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")]
  61. [Api(Description = "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 Guid 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. public class NotificationsService : BaseApiService
  70. {
  71. private readonly INotificationsRepository _notificationsRepo;
  72. public NotificationsService(INotificationsRepository notificationsRepo)
  73. {
  74. _notificationsRepo = notificationsRepo;
  75. }
  76. public object Post(AddNotification request)
  77. {
  78. var task = AddNotification(request);
  79. return ToOptimizedResult(task.Result);
  80. }
  81. public object Get(GetNotificationsSummary request)
  82. {
  83. var result = _notificationsRepo.GetNotificationsSummary(request.UserId);
  84. return result;
  85. }
  86. private async Task<Notification> AddNotification(AddNotification request)
  87. {
  88. var notification = new Notification
  89. {
  90. Id = request.Id ?? Guid.NewGuid(),
  91. Date = DateTime.UtcNow,
  92. Description = request.Description,
  93. Level = request.Level,
  94. Name = request.Name,
  95. Url = request.Url,
  96. UserId = request.UserId,
  97. Category = request.Category,
  98. RelatedId = request.RelatedId
  99. };
  100. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  101. return notification;
  102. }
  103. public void Post(MarkRead request)
  104. {
  105. var task = MarkRead(request.Ids, request.UserId, true);
  106. Task.WaitAll(task);
  107. }
  108. public void Post(MarkUnread request)
  109. {
  110. var task = MarkRead(request.Ids, request.UserId, false);
  111. Task.WaitAll(task);
  112. }
  113. private Task MarkRead(string idList, Guid userId, bool read)
  114. {
  115. var ids = idList.Split(',').Select(i => new Guid(i));
  116. return _notificationsRepo.MarkRead(ids, userId, read, CancellationToken.None);
  117. }
  118. public object Get(GetNotifications request)
  119. {
  120. var result = _notificationsRepo.GetNotifications(new NotificationQuery
  121. {
  122. IsRead = request.IsRead,
  123. Limit = request.Limit,
  124. StartIndex = request.StartIndex,
  125. UserId = request.UserId
  126. });
  127. return ToOptimizedResult(result);
  128. }
  129. }
  130. }