2
0

NotificationManager.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Notifications;
  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. private NotificationOptions GetConfiguration()
  30. {
  31. return _config.GetConfiguration<NotificationOptions>("notifications");
  32. }
  33. public Task SendNotification(NotificationRequest request, CancellationToken cancellationToken)
  34. {
  35. var notificationType = request.NotificationType;
  36. var options = string.IsNullOrWhiteSpace(notificationType) ?
  37. null :
  38. GetConfiguration().GetOptions(notificationType);
  39. var users = GetUserIds(request, options)
  40. .Select(i => _userManager.GetUserById(i));
  41. var title = GetTitle(request, options);
  42. var description = GetDescription(request, options);
  43. var tasks = _services.Where(i => IsEnabled(i, notificationType))
  44. .Select(i => SendNotification(request, i, users, title, description, cancellationToken));
  45. return Task.WhenAll(tasks);
  46. }
  47. private Task SendNotification(NotificationRequest request,
  48. INotificationService service,
  49. IEnumerable<User> users,
  50. string title,
  51. string description,
  52. CancellationToken cancellationToken)
  53. {
  54. users = users.Where(i => IsEnabledForUser(service, i))
  55. .ToList();
  56. var tasks = users.Select(i => SendNotification(request, service, title, description, i, cancellationToken));
  57. return Task.WhenAll(tasks);
  58. }
  59. private IEnumerable<string> GetUserIds(NotificationRequest request, NotificationOption options)
  60. {
  61. if (request.SendToUserMode.HasValue)
  62. {
  63. switch (request.SendToUserMode.Value)
  64. {
  65. case SendToUserType.Admins:
  66. return _userManager.Users.Where(i => i.Configuration.IsAdministrator)
  67. .Select(i => i.Id.ToString("N"));
  68. case SendToUserType.All:
  69. return _userManager.Users.Select(i => i.Id.ToString("N"));
  70. case SendToUserType.Custom:
  71. return request.UserIds;
  72. default:
  73. throw new ArgumentException("Unrecognized SendToUserMode: " + request.SendToUserMode.Value);
  74. }
  75. }
  76. if (options != null && !string.IsNullOrWhiteSpace(request.NotificationType))
  77. {
  78. var config = GetConfiguration();
  79. return _userManager.Users
  80. .Where(i => config.IsEnabledToSendToUser(request.NotificationType, i.Id.ToString("N"), i.Configuration))
  81. .Select(i => i.Id.ToString("N"));
  82. }
  83. return request.UserIds;
  84. }
  85. private async Task SendNotification(NotificationRequest request,
  86. INotificationService service,
  87. string title,
  88. string description,
  89. User user,
  90. CancellationToken cancellationToken)
  91. {
  92. var notification = new UserNotification
  93. {
  94. Date = request.Date,
  95. Description = 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 string GetDescription(NotificationRequest request, NotificationOption options)
  146. {
  147. var text = request.Description;
  148. // If empty, grab from options
  149. if (string.IsNullOrEmpty(text))
  150. {
  151. if (!string.IsNullOrEmpty(request.NotificationType))
  152. {
  153. if (options != null)
  154. {
  155. text = options.Description;
  156. }
  157. }
  158. }
  159. // If still empty, grab default
  160. if (string.IsNullOrEmpty(text))
  161. {
  162. if (!string.IsNullOrEmpty(request.NotificationType))
  163. {
  164. var info = GetNotificationTypes().FirstOrDefault(i => string.Equals(i.Type, request.NotificationType, StringComparison.OrdinalIgnoreCase));
  165. if (info != null)
  166. {
  167. text = info.DefaultDescription;
  168. }
  169. }
  170. }
  171. text = text ?? string.Empty;
  172. foreach (var pair in request.Variables)
  173. {
  174. var token = "{" + pair.Key + "}";
  175. text = text.Replace(token, pair.Value, StringComparison.OrdinalIgnoreCase);
  176. }
  177. return text;
  178. }
  179. private bool IsEnabledForUser(INotificationService service, User user)
  180. {
  181. try
  182. {
  183. return service.IsEnabledForUser(user);
  184. }
  185. catch (Exception ex)
  186. {
  187. _logger.ErrorException("Error in IsEnabledForUser", ex);
  188. return false;
  189. }
  190. }
  191. private bool IsEnabled(INotificationService service, string notificationType)
  192. {
  193. return string.IsNullOrEmpty(notificationType) ||
  194. GetConfiguration().IsServiceEnabled(service.Name, notificationType);
  195. }
  196. public void AddParts(IEnumerable<INotificationService> services, IEnumerable<INotificationTypeFactory> notificationTypeFactories)
  197. {
  198. _services = services.ToArray();
  199. _typeFactories = notificationTypeFactories.ToArray();
  200. }
  201. public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
  202. {
  203. var list = _typeFactories.Select(i =>
  204. {
  205. try
  206. {
  207. return i.GetNotificationTypes().ToList();
  208. }
  209. catch (Exception ex)
  210. {
  211. _logger.ErrorException("Error in GetNotificationTypes", ex);
  212. return new List<NotificationTypeInfo>();
  213. }
  214. }).SelectMany(i => i).ToList();
  215. var config = GetConfiguration();
  216. foreach (var i in list)
  217. {
  218. i.Enabled = config.IsEnabled(i.Type);
  219. }
  220. return list;
  221. }
  222. public IEnumerable<NotificationServiceInfo> GetNotificationServices()
  223. {
  224. return _services.Select(i => new NotificationServiceInfo
  225. {
  226. Name = i.Name,
  227. Id = i.Name.GetMD5().ToString("N")
  228. }).OrderBy(i => i.Name);
  229. }
  230. }
  231. }