NotificationsService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = false, 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}/{Id}", "POST")]
  52. [Api(Description = "Updates a notifications")]
  53. public class UpdateNotification : IReturnVoid
  54. {
  55. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  56. public Guid UserId { get; set; }
  57. [ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  58. public Guid Id { get; set; }
  59. [ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  60. public string Name { get; set; }
  61. [ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  62. public string Description { get; set; }
  63. [ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  64. public string Url { get; set; }
  65. [ApiMember(Name = "Category", Description = "The notification's category", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  66. public string Category { get; set; }
  67. [ApiMember(Name = "RelatedId", Description = "The notification's related id (item)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  68. public string RelatedId { get; set; }
  69. [ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  70. public NotificationLevel Level { get; set; }
  71. }
  72. [Route("/Notifications/{UserId}/Read", "POST")]
  73. [Api(Description = "Marks notifications as read")]
  74. public class MarkRead : IReturnVoid
  75. {
  76. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  77. public Guid UserId { get; set; }
  78. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  79. public string Ids { get; set; }
  80. }
  81. [Route("/Notifications/{UserId}/Unread", "POST")]
  82. [Api(Description = "Marks notifications as unread")]
  83. public class MarkUnread : IReturnVoid
  84. {
  85. [ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  86. public Guid UserId { get; set; }
  87. [ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
  88. public string Ids { get; set; }
  89. }
  90. public class NotificationsService : BaseApiService
  91. {
  92. private readonly INotificationsRepository _notificationsRepo;
  93. public NotificationsService(INotificationsRepository notificationsRepo)
  94. {
  95. _notificationsRepo = notificationsRepo;
  96. }
  97. public object Post(AddNotification request)
  98. {
  99. var task = AddNotification(request);
  100. return ToOptimizedResult(task.Result);
  101. }
  102. public void Post(UpdateNotification request)
  103. {
  104. var task = UpdateNotification(request);
  105. Task.WaitAll(task);
  106. }
  107. public object Get(GetNotificationsSummary request)
  108. {
  109. var result = _notificationsRepo.GetNotificationsSummary(request.UserId);
  110. return result;
  111. }
  112. private async Task<Notification> AddNotification(AddNotification request)
  113. {
  114. var notification = new Notification
  115. {
  116. Id = request.Id ?? Guid.NewGuid(),
  117. Date = DateTime.UtcNow,
  118. Description = request.Description,
  119. Level = request.Level,
  120. Name = request.Name,
  121. Url = request.Url,
  122. UserId = request.UserId,
  123. Category = request.Category,
  124. RelatedId = request.RelatedId
  125. };
  126. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  127. return notification;
  128. }
  129. private Task UpdateNotification(UpdateNotification request)
  130. {
  131. var notification = _notificationsRepo.GetNotification(request.Id, request.UserId);
  132. notification.Description = request.Description;
  133. notification.Level = request.Level;
  134. notification.Url = request.Url;
  135. notification.Date = DateTime.UtcNow;
  136. notification.RelatedId = request.RelatedId;
  137. notification.Category = request.Category;
  138. notification.Name = request.Name;
  139. return _notificationsRepo.UpdateNotification(notification, CancellationToken.None);
  140. }
  141. public void Post(MarkRead request)
  142. {
  143. var task = MarkRead(request.Ids, request.UserId, true);
  144. Task.WaitAll(task);
  145. }
  146. public void Post(MarkUnread request)
  147. {
  148. var task = MarkRead(request.Ids, request.UserId, false);
  149. Task.WaitAll(task);
  150. }
  151. private Task MarkRead(string idList, Guid userId, bool read)
  152. {
  153. var ids = idList.Split(',').Select(i => new Guid(i));
  154. return _notificationsRepo.MarkRead(ids, userId, read, CancellationToken.None);
  155. }
  156. public object Get(GetNotifications request)
  157. {
  158. var result = _notificationsRepo.GetNotifications(new NotificationQuery
  159. {
  160. IsRead = request.IsRead,
  161. Limit = request.Limit,
  162. StartIndex = request.StartIndex,
  163. UserId = request.UserId
  164. });
  165. return ToOptimizedResult(result);
  166. }
  167. }
  168. }