2
0

NotificationManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using MediaBrowser.Common.Configuration;
  7. using MediaBrowser.Common.Extensions;
  8. using MediaBrowser.Controller.Configuration;
  9. using MediaBrowser.Controller.Entities;
  10. using MediaBrowser.Controller.Library;
  11. using MediaBrowser.Controller.Notifications;
  12. using MediaBrowser.Model.Dto;
  13. using MediaBrowser.Model.Notifications;
  14. using Microsoft.Extensions.Logging;
  15. namespace Emby.Notifications
  16. {
  17. public class NotificationManager : INotificationManager
  18. {
  19. private readonly ILogger _logger;
  20. private readonly IUserManager _userManager;
  21. private readonly IServerConfigurationManager _config;
  22. private INotificationService[] _services;
  23. private INotificationTypeFactory[] _typeFactories;
  24. public NotificationManager(ILoggerFactory loggerFactory, IUserManager userManager, IServerConfigurationManager config)
  25. {
  26. _userManager = userManager;
  27. _config = config;
  28. _logger = loggerFactory.CreateLogger(GetType().Name);
  29. }
  30. private NotificationOptions GetConfiguration()
  31. {
  32. return _config.GetConfiguration<NotificationOptions>("notifications");
  33. }
  34. public Task SendNotification(NotificationRequest request, CancellationToken cancellationToken)
  35. {
  36. return SendNotification(request, null, cancellationToken);
  37. }
  38. public Task SendNotification(NotificationRequest request, BaseItem relatedItem, CancellationToken cancellationToken)
  39. {
  40. var notificationType = request.NotificationType;
  41. var options = string.IsNullOrEmpty(notificationType) ?
  42. null :
  43. GetConfiguration().GetOptions(notificationType);
  44. var users = GetUserIds(request, options)
  45. .Select(i => _userManager.GetUserById(i))
  46. .Where(i => relatedItem == null || relatedItem.IsVisibleStandalone(i))
  47. .ToArray();
  48. var title = request.Name;
  49. var description = request.Description;
  50. var tasks = _services.Where(i => IsEnabled(i, notificationType))
  51. .Select(i => SendNotification(request, i, users, title, description, cancellationToken));
  52. return Task.WhenAll(tasks);
  53. }
  54. private Task SendNotification(NotificationRequest request,
  55. INotificationService service,
  56. IEnumerable<User> users,
  57. string title,
  58. string description,
  59. CancellationToken cancellationToken)
  60. {
  61. users = users.Where(i => IsEnabledForUser(service, i))
  62. .ToList();
  63. var tasks = users.Select(i => SendNotification(request, service, title, description, i, cancellationToken));
  64. return Task.WhenAll(tasks);
  65. }
  66. private IEnumerable<Guid> GetUserIds(NotificationRequest request, NotificationOption options)
  67. {
  68. if (request.SendToUserMode.HasValue)
  69. {
  70. switch (request.SendToUserMode.Value)
  71. {
  72. case SendToUserType.Admins:
  73. return _userManager.Users.Where(i => i.Policy.IsAdministrator)
  74. .Select(i => i.Id);
  75. case SendToUserType.All:
  76. return _userManager.Users.Select(i => i.Id);
  77. case SendToUserType.Custom:
  78. return request.UserIds;
  79. default:
  80. throw new ArgumentException("Unrecognized SendToUserMode: " + request.SendToUserMode.Value);
  81. }
  82. }
  83. if (options != null && !string.IsNullOrEmpty(request.NotificationType))
  84. {
  85. var config = GetConfiguration();
  86. return _userManager.Users
  87. .Where(i => config.IsEnabledToSendToUser(request.NotificationType, i.Id.ToString("N"), i.Policy))
  88. .Select(i => i.Id);
  89. }
  90. return request.UserIds;
  91. }
  92. private async Task SendNotification(NotificationRequest request,
  93. INotificationService service,
  94. string title,
  95. string description,
  96. User user,
  97. CancellationToken cancellationToken)
  98. {
  99. var notification = new UserNotification
  100. {
  101. Date = request.Date,
  102. Description = description,
  103. Level = request.Level,
  104. Name = title,
  105. Url = request.Url,
  106. User = user
  107. };
  108. _logger.LogDebug("Sending notification via {0} to user {1}", service.Name, user.Name);
  109. try
  110. {
  111. await service.SendNotification(notification, cancellationToken).ConfigureAwait(false);
  112. }
  113. catch (Exception ex)
  114. {
  115. _logger.LogError(ex, "Error sending notification to {0}", service.Name);
  116. }
  117. }
  118. private bool IsEnabledForUser(INotificationService service, User user)
  119. {
  120. try
  121. {
  122. return service.IsEnabledForUser(user);
  123. }
  124. catch (Exception ex)
  125. {
  126. _logger.LogError(ex, "Error in IsEnabledForUser");
  127. return false;
  128. }
  129. }
  130. private bool IsEnabled(INotificationService service, string notificationType)
  131. {
  132. if (string.IsNullOrEmpty(notificationType))
  133. {
  134. return true;
  135. }
  136. return GetConfiguration().IsServiceEnabled(service.Name, notificationType);
  137. }
  138. public void AddParts(IEnumerable<INotificationService> services, IEnumerable<INotificationTypeFactory> notificationTypeFactories)
  139. {
  140. _services = services.ToArray();
  141. _typeFactories = notificationTypeFactories.ToArray();
  142. }
  143. public List<NotificationTypeInfo> GetNotificationTypes()
  144. {
  145. var list = _typeFactories.Select(i =>
  146. {
  147. try
  148. {
  149. return i.GetNotificationTypes().ToList();
  150. }
  151. catch (Exception ex)
  152. {
  153. _logger.LogError(ex, "Error in GetNotificationTypes");
  154. return new List<NotificationTypeInfo>();
  155. }
  156. }).SelectMany(i => i).ToList();
  157. var config = GetConfiguration();
  158. foreach (var i in list)
  159. {
  160. i.Enabled = config.IsEnabled(i.Type);
  161. }
  162. return list;
  163. }
  164. public IEnumerable<NameIdPair> GetNotificationServices()
  165. {
  166. return _services.Select(i => new NameIdPair
  167. {
  168. Name = i.Name,
  169. Id = i.Name.GetMD5().ToString("N")
  170. }).OrderBy(i => i.Name);
  171. }
  172. }
  173. }