NotificationManager.cs 6.2 KB

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