Notifier.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Common.Plugins;
  3. using MediaBrowser.Common.ScheduledTasks;
  4. using MediaBrowser.Common.Updates;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Controller.Notifications;
  10. using MediaBrowser.Controller.Plugins;
  11. using MediaBrowser.Controller.Session;
  12. using MediaBrowser.Model.Configuration;
  13. using MediaBrowser.Model.Entities;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Model.Notifications;
  16. using MediaBrowser.Model.Tasks;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using MediaBrowser.Model.Updates;
  23. namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
  24. {
  25. /// <summary>
  26. /// Creates notifications for various system events
  27. /// </summary>
  28. public class Notifications : IServerEntryPoint
  29. {
  30. private readonly IInstallationManager _installationManager;
  31. private readonly IUserManager _userManager;
  32. private readonly ILogger _logger;
  33. private readonly ITaskManager _taskManager;
  34. private readonly INotificationManager _notificationManager;
  35. private readonly IServerConfigurationManager _config;
  36. private readonly ILibraryManager _libraryManager;
  37. private readonly ISessionManager _sessionManager;
  38. private readonly IServerApplicationHost _appHost;
  39. public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost)
  40. {
  41. _installationManager = installationManager;
  42. _userManager = userManager;
  43. _logger = logger;
  44. _taskManager = taskManager;
  45. _notificationManager = notificationManager;
  46. _config = config;
  47. _libraryManager = libraryManager;
  48. _sessionManager = sessionManager;
  49. _appHost = appHost;
  50. }
  51. public void Run()
  52. {
  53. _installationManager.PluginInstalled += _installationManager_PluginInstalled;
  54. _installationManager.PluginUpdated += _installationManager_PluginUpdated;
  55. _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
  56. _installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
  57. _taskManager.TaskCompleted += _taskManager_TaskCompleted;
  58. _userManager.UserCreated += _userManager_UserCreated;
  59. _libraryManager.ItemAdded += _libraryManager_ItemAdded;
  60. _sessionManager.PlaybackStart += _sessionManager_PlaybackStart;
  61. _appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
  62. _appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged;
  63. }
  64. async void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e)
  65. {
  66. var type = NotificationType.PluginUpdateInstalled.ToString();
  67. var installationInfo = e.Argument.Item1;
  68. var notification = new NotificationRequest
  69. {
  70. Description = installationInfo.Description,
  71. NotificationType = type
  72. };
  73. notification.Variables["Name"] = installationInfo.Name;
  74. notification.Variables["Version"] = installationInfo.Version.ToString();
  75. await SendNotification(notification).ConfigureAwait(false);
  76. }
  77. async void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e)
  78. {
  79. var type = NotificationType.PluginInstalled.ToString();
  80. var installationInfo = e.Argument;
  81. var notification = new NotificationRequest
  82. {
  83. Description = installationInfo.description,
  84. NotificationType = type
  85. };
  86. notification.Variables["Name"] = installationInfo.name;
  87. notification.Variables["Version"] = installationInfo.versionStr;
  88. await SendNotification(notification).ConfigureAwait(false);
  89. }
  90. async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e)
  91. {
  92. // This notification is for users who can't auto-update (aka running as service)
  93. if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate)
  94. {
  95. return;
  96. }
  97. var type = NotificationType.ApplicationUpdateAvailable.ToString();
  98. var notification = new NotificationRequest
  99. {
  100. Description = "Please see mediabrowser3.com for details.",
  101. NotificationType = type
  102. };
  103. await SendNotification(notification).ConfigureAwait(false);
  104. }
  105. async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
  106. {
  107. if (!_appHost.HasPendingRestart)
  108. {
  109. return;
  110. }
  111. var type = NotificationType.ServerRestartRequired.ToString();
  112. var notification = new NotificationRequest
  113. {
  114. NotificationType = type
  115. };
  116. await SendNotification(notification).ConfigureAwait(false);
  117. }
  118. async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
  119. {
  120. var user = e.Users.FirstOrDefault();
  121. var item = e.MediaInfo;
  122. var notification = new NotificationRequest
  123. {
  124. NotificationType = GetPlaybackNotificationType(item.MediaType)
  125. };
  126. notification.Variables["ItemName"] = item.Name;
  127. notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
  128. notification.Variables["AppName"] = e.ClientName;
  129. notification.Variables["DeviceName"] = e.DeviceName;
  130. await SendNotification(notification).ConfigureAwait(false);
  131. }
  132. private string GetPlaybackNotificationType(string mediaType)
  133. {
  134. if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  135. {
  136. return NotificationType.AudioPlayback.ToString();
  137. }
  138. if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
  139. {
  140. return NotificationType.GamePlayback.ToString();
  141. }
  142. if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  143. {
  144. return NotificationType.VideoPlayback.ToString();
  145. }
  146. return null;
  147. }
  148. async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
  149. {
  150. if (e.Item.LocationType == LocationType.FileSystem)
  151. {
  152. var type = NotificationType.NewLibraryContent.ToString();
  153. var item = e.Item;
  154. var notification = new NotificationRequest
  155. {
  156. NotificationType = type
  157. };
  158. notification.Variables["Name"] = item.Name;
  159. await SendNotification(notification).ConfigureAwait(false);
  160. }
  161. }
  162. async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
  163. {
  164. var notification = new NotificationRequest
  165. {
  166. UserIds = new List<string> { e.Argument.Id.ToString("N") },
  167. Name = "Welcome to Media Browser!",
  168. Description = "Check back here for more notifications."
  169. };
  170. await SendNotification(notification).ConfigureAwait(false);
  171. }
  172. async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
  173. {
  174. var result = e.Argument;
  175. if (result.Status == TaskCompletionStatus.Failed)
  176. {
  177. var type = NotificationType.TaskFailed.ToString();
  178. var notification = new NotificationRequest
  179. {
  180. Description = result.ErrorMessage,
  181. Level = NotificationLevel.Error,
  182. NotificationType = type
  183. };
  184. notification.Variables["Name"] = e.Argument.Name;
  185. await SendNotification(notification).ConfigureAwait(false);
  186. }
  187. }
  188. async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  189. {
  190. var type = NotificationType.PluginUninstalled.ToString();
  191. var plugin = e.Argument;
  192. var notification = new NotificationRequest
  193. {
  194. NotificationType = type
  195. };
  196. notification.Variables["Name"] = plugin.Name;
  197. notification.Variables["Version"] = plugin.Version.ToString();
  198. await SendNotification(notification).ConfigureAwait(false);
  199. }
  200. async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  201. {
  202. var installationInfo = e.InstallationInfo;
  203. var type = NotificationType.InstallationFailed.ToString();
  204. var notification = new NotificationRequest
  205. {
  206. Level = NotificationLevel.Error,
  207. Description = e.Exception.Message,
  208. NotificationType = type
  209. };
  210. notification.Variables["Name"] = installationInfo.Name;
  211. notification.Variables["Version"] = installationInfo.Version;
  212. await SendNotification(notification).ConfigureAwait(false);
  213. }
  214. private async Task SendNotification(NotificationRequest notification)
  215. {
  216. try
  217. {
  218. await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
  219. }
  220. catch (Exception ex)
  221. {
  222. _logger.ErrorException("Error sending notification", ex);
  223. }
  224. }
  225. public void Dispose()
  226. {
  227. _installationManager.PluginInstalled -= _installationManager_PluginInstalled;
  228. _installationManager.PluginUpdated -= _installationManager_PluginUpdated;
  229. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  230. _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
  231. _taskManager.TaskCompleted -= _taskManager_TaskCompleted;
  232. _userManager.UserCreated -= _userManager_UserCreated;
  233. _libraryManager.ItemAdded -= _libraryManager_ItemAdded;
  234. _sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
  235. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  236. _appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
  237. }
  238. }
  239. }