NotificationManager.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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).Select(i => _userManager.GetUserById(new Guid(i)));
  36. var title = GetTitle(request, options);
  37. var tasks = _services.Where(i => IsEnabled(i, notificationType))
  38. .Select(i => SendNotification(request, i, users, title, cancellationToken));
  39. return Task.WhenAll(tasks);
  40. }
  41. private Task SendNotification(NotificationRequest request,
  42. INotificationService service,
  43. IEnumerable<User> users,
  44. string title,
  45. CancellationToken cancellationToken)
  46. {
  47. users = users.Where(i => IsEnabledForUser(service, i))
  48. .ToList();
  49. var tasks = users.Select(i => SendNotification(request, service, title, i, cancellationToken));
  50. return Task.WhenAll(tasks);
  51. }
  52. private IEnumerable<string> GetUserIds(NotificationRequest request, NotificationOption options)
  53. {
  54. if (request.SendToUserMode.HasValue)
  55. {
  56. switch (request.SendToUserMode.Value)
  57. {
  58. case SendToUserType.Admins:
  59. return _userManager.Users.Where(i => i.Configuration.IsAdministrator)
  60. .Select(i => i.Id.ToString("N"));
  61. case SendToUserType.All:
  62. return _userManager.Users.Select(i => i.Id.ToString("N"));
  63. case SendToUserType.Custom:
  64. return request.UserIds;
  65. default:
  66. throw new ArgumentException("Unrecognized SendToUserMode: " + request.SendToUserMode.Value);
  67. }
  68. }
  69. if (options != null)
  70. {
  71. switch (options.SendToUserMode)
  72. {
  73. case SendToUserType.Admins:
  74. return _userManager.Users.Where(i => i.Configuration.IsAdministrator)
  75. .Select(i => i.Id.ToString("N"));
  76. case SendToUserType.All:
  77. return _userManager.Users.Select(i => i.Id.ToString("N"));
  78. case SendToUserType.Custom:
  79. return options.SendToUsers;
  80. default:
  81. throw new ArgumentException("Unrecognized SendToUserMode: " + options.SendToUserMode);
  82. }
  83. }
  84. return new List<string>();
  85. }
  86. private async Task SendNotification(NotificationRequest request,
  87. INotificationService service,
  88. string title,
  89. User user,
  90. CancellationToken cancellationToken)
  91. {
  92. var notification = new UserNotification
  93. {
  94. Date = request.Date,
  95. Description = request.Description,
  96. Level = request.Level,
  97. Name = title,
  98. Url = request.Url,
  99. User = user
  100. };
  101. _logger.Debug("Sending notification via {0} to user {1}", service.Name, user.Name);
  102. try
  103. {
  104. await service.SendNotification(notification, cancellationToken).ConfigureAwait(false);
  105. }
  106. catch (Exception ex)
  107. {
  108. _logger.ErrorException("Error sending notification to {0}", ex, service.Name);
  109. }
  110. }
  111. private string GetTitle(NotificationRequest request, NotificationOption options)
  112. {
  113. var title = request.Name;
  114. // If empty, grab from options
  115. if (string.IsNullOrEmpty(title))
  116. {
  117. if (!string.IsNullOrEmpty(request.NotificationType))
  118. {
  119. if (options != null)
  120. {
  121. title = options.Title;
  122. }
  123. }
  124. }
  125. // If still empty, grab default
  126. if (string.IsNullOrEmpty(title))
  127. {
  128. if (!string.IsNullOrEmpty(request.NotificationType))
  129. {
  130. var info = GetNotificationTypes().FirstOrDefault(i => string.Equals(i.Type, request.NotificationType, StringComparison.OrdinalIgnoreCase));
  131. if (info != null)
  132. {
  133. title = info.DefaultTitle;
  134. }
  135. }
  136. }
  137. title = title ?? string.Empty;
  138. foreach (var pair in request.Variables)
  139. {
  140. var token = "{" + pair.Key + "}";
  141. title = title.Replace(token, pair.Value, StringComparison.OrdinalIgnoreCase);
  142. }
  143. return title;
  144. }
  145. private bool IsEnabledForUser(INotificationService service, User user)
  146. {
  147. try
  148. {
  149. return service.IsEnabledForUser(user);
  150. }
  151. catch (Exception ex)
  152. {
  153. _logger.ErrorException("Error in IsEnabledForUser", ex);
  154. return false;
  155. }
  156. }
  157. private bool IsEnabled(INotificationService service, string notificationType)
  158. {
  159. return string.IsNullOrEmpty(notificationType) ||
  160. _config.Configuration.NotificationOptions.IsServiceEnabled(service.Name, notificationType);
  161. }
  162. public void AddParts(IEnumerable<INotificationService> services, IEnumerable<INotificationTypeFactory> notificationTypeFactories)
  163. {
  164. _services = services.ToArray();
  165. _typeFactories = notificationTypeFactories.ToArray();
  166. }
  167. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  168. {
  169. var list = _typeFactories.Select(i =>
  170. {
  171. try
  172. {
  173. return i.GetNotificationTypes().ToList();
  174. }
  175. catch (Exception ex)
  176. {
  177. _logger.ErrorException("Error in GetNotificationTypes", ex);
  178. return new List<NotificationTypeInfo>();
  179. }
  180. }).SelectMany(i => i).ToList();
  181. foreach (var i in list)
  182. {
  183. i.Enabled = _config.Configuration.NotificationOptions.IsEnabled(i.Type);
  184. }
  185. return list;
  186. }
  187. public IEnumerable<NotificationServiceInfo> GetNotificationServices()
  188. {
  189. return _services.Select(i => new NotificationServiceInfo
  190. {
  191. Name = i.Name,
  192. Id = i.Name.GetMD5().ToString("N")
  193. }).OrderBy(i => i.Name);
  194. }
  195. }
  196. }