NotificationsService.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using MediaBrowser.Controller.Notifications;
  2. using MediaBrowser.Model.Notifications;
  3. using ServiceStack;
  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", 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 Guid UserId { get; set; }
  27. }
  28. [Route("/Notifications/{UserId}", "POST", Summary = "Adds a notifications")]
  29. public class AddUserNotification : IReturn<Notification>
  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 Guid? Id { get; set; }
  33. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  34. public Guid 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 = "Category", Description = "The notification's category", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  42. public string Category { get; set; }
  43. [ApiMember(Name = "RelatedId", Description = "The notification's related id (item)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  44. public string RelatedId { get; set; }
  45. [ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  46. public NotificationLevel Level { get; set; }
  47. }
  48. [Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
  49. public class MarkRead : IReturnVoid
  50. {
  51. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  52. public Guid UserId { get; set; }
  53. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  54. public string Ids { get; set; }
  55. }
  56. [Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
  57. public class MarkUnread : IReturnVoid
  58. {
  59. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  60. public Guid UserId { get; set; }
  61. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  62. public string Ids { get; set; }
  63. }
  64. public class NotificationsService : BaseApiService
  65. {
  66. private readonly INotificationsRepository _notificationsRepo;
  67. public NotificationsService(INotificationsRepository notificationsRepo)
  68. {
  69. _notificationsRepo = notificationsRepo;
  70. }
  71. public object Post(AddUserNotification request)
  72. {
  73. var task = AddNotification(request);
  74. return ToOptimizedResult(task.Result);
  75. }
  76. public object Get(GetNotificationsSummary request)
  77. {
  78. var result = _notificationsRepo.GetNotificationsSummary(request.UserId);
  79. return result;
  80. }
  81. private async Task<Notification> AddNotification(AddUserNotification request)
  82. {
  83. var notification = new Notification
  84. {
  85. Id = request.Id ?? Guid.NewGuid(),
  86. Date = DateTime.UtcNow,
  87. Description = request.Description,
  88. Level = request.Level,
  89. Name = request.Name,
  90. Url = request.Url,
  91. UserId = request.UserId,
  92. Category = request.Category,
  93. RelatedId = request.RelatedId
  94. };
  95. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  96. return notification;
  97. }
  98. public void Post(MarkRead request)
  99. {
  100. var task = MarkRead(request.Ids, request.UserId, true);
  101. Task.WaitAll(task);
  102. }
  103. public void Post(MarkUnread request)
  104. {
  105. var task = MarkRead(request.Ids, request.UserId, false);
  106. Task.WaitAll(task);
  107. }
  108. private Task MarkRead(string idList, Guid userId, bool read)
  109. {
  110. var ids = idList.Split(',').Select(i => new Guid(i));
  111. return _notificationsRepo.MarkRead(ids, userId, read, CancellationToken.None);
  112. }
  113. public object Get(GetNotifications request)
  114. {
  115. var result = _notificationsRepo.GetNotifications(new NotificationQuery
  116. {
  117. IsRead = request.IsRead,
  118. Limit = request.Limit,
  119. StartIndex = request.StartIndex,
  120. UserId = request.UserId
  121. });
  122. return ToOptimizedResult(result);
  123. }
  124. }
  125. }