NotificationManager.cs 9.1 KB

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