Notifier.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.Plugins;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Notifications;
  8. using MediaBrowser.Controller.Plugins;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Notifications;
  11. using MediaBrowser.Model.Tasks;
  12. using System;
  13. using System.Linq;
  14. using System.Threading;
  15. namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
  16. {
  17. /// <summary>
  18. /// Creates notifications for various system events
  19. /// </summary>
  20. public class Notifications : IServerEntryPoint
  21. {
  22. private readonly INotificationsRepository _notificationsRepo;
  23. private readonly IInstallationManager _installationManager;
  24. private readonly IUserManager _userManager;
  25. private readonly ILogger _logger;
  26. private readonly ITaskManager _taskManager;
  27. public Notifications(IInstallationManager installationManager, INotificationsRepository notificationsRepo, IUserManager userManager, ILogger logger, ITaskManager taskManager)
  28. {
  29. _installationManager = installationManager;
  30. _notificationsRepo = notificationsRepo;
  31. _userManager = userManager;
  32. _logger = logger;
  33. _taskManager = taskManager;
  34. }
  35. public void Run()
  36. {
  37. _installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
  38. _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
  39. _installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
  40. _taskManager.TaskCompleted += _taskManager_TaskCompleted;
  41. _userManager.UserCreated += _userManager_UserCreated;
  42. }
  43. async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
  44. {
  45. var notification = new Notification
  46. {
  47. UserId = e.Argument.Id,
  48. Category = "UserCreated",
  49. Name = "Welcome to Media Browser!",
  50. Description = "Check back here for more notifications."
  51. };
  52. try
  53. {
  54. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  55. }
  56. catch (Exception ex)
  57. {
  58. _logger.ErrorException("Error adding notification", ex);
  59. }
  60. }
  61. async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
  62. {
  63. var result = e.Argument;
  64. if (result.Status == TaskCompletionStatus.Failed)
  65. {
  66. foreach (var user in _userManager
  67. .Users
  68. .Where(i => i.Configuration.IsAdministrator)
  69. .ToList())
  70. {
  71. var notification = new Notification
  72. {
  73. UserId = user.Id,
  74. Category = "ScheduledTaskFailed",
  75. Name = result.Name + " failed",
  76. RelatedId = result.Name,
  77. Description = result.ErrorMessage,
  78. Level = NotificationLevel.Error
  79. };
  80. try
  81. {
  82. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  83. }
  84. catch (Exception ex)
  85. {
  86. _logger.ErrorException("Error adding notification", ex);
  87. }
  88. }
  89. }
  90. }
  91. async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  92. {
  93. var plugin = e.Argument;
  94. foreach (var user in _userManager
  95. .Users
  96. .Where(i => i.Configuration.IsAdministrator)
  97. .ToList())
  98. {
  99. var notification = new Notification
  100. {
  101. UserId = user.Id,
  102. Category = "PluginUninstalled",
  103. Name = plugin.Name + " has been uninstalled",
  104. RelatedId = plugin.Id.ToString()
  105. };
  106. try
  107. {
  108. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  109. }
  110. catch (Exception ex)
  111. {
  112. _logger.ErrorException("Error adding notification", ex);
  113. }
  114. }
  115. }
  116. async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
  117. {
  118. var installationInfo = e.InstallationInfo;
  119. foreach (var user in _userManager
  120. .Users
  121. .Where(i => i.Configuration.IsAdministrator)
  122. .ToList())
  123. {
  124. var notification = new Notification
  125. {
  126. UserId = user.Id,
  127. Category = "PackageInstallationCompleted",
  128. Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
  129. RelatedId = installationInfo.Name,
  130. Description = e.PackageVersionInfo.description
  131. };
  132. try
  133. {
  134. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  135. }
  136. catch (Exception ex)
  137. {
  138. _logger.ErrorException("Error adding notification", ex);
  139. }
  140. }
  141. }
  142. async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  143. {
  144. var installationInfo = e.InstallationInfo;
  145. foreach (var user in _userManager
  146. .Users
  147. .Where(i => i.Configuration.IsAdministrator)
  148. .ToList())
  149. {
  150. var notification = new Notification
  151. {
  152. UserId = user.Id,
  153. Category = "PackageInstallationFailed",
  154. Level = NotificationLevel.Error,
  155. Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
  156. RelatedId = installationInfo.Name,
  157. Description = e.Exception.Message
  158. };
  159. try
  160. {
  161. await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
  162. }
  163. catch (Exception ex)
  164. {
  165. _logger.ErrorException("Error adding notification", ex);
  166. }
  167. }
  168. }
  169. public void Dispose()
  170. {
  171. _installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
  172. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  173. _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
  174. _taskManager.TaskCompleted -= _taskManager_TaskCompleted;
  175. _userManager.UserCreated -= _userManager_UserCreated;
  176. }
  177. }
  178. }