NotificationManager.cs 7.7 KB

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