NotificationManager.cs 10.0 KB

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