NotificationManager.cs 9.7 KB

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