Notifier.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. _appHost.ApplicationUpdated += _appHost_ApplicationUpdated;
  64. }
  65. async void _appHost_ApplicationUpdated(object sender, GenericEventArgs<PackageVersionInfo> e)
  66. {
  67. var type = NotificationType.ApplicationUpdateInstalled.ToString();
  68. var notification = new NotificationRequest
  69. {
  70. NotificationType = type
  71. };
  72. notification.Variables["Version"] = e.Argument.versionStr;
  73. notification.Variables["ReleaseNotes"] = e.Argument.description;
  74. await SendNotification(notification).ConfigureAwait(false);
  75. }
  76. async void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e)
  77. {
  78. var type = NotificationType.PluginUpdateInstalled.ToString();
  79. var installationInfo = e.Argument.Item1;
  80. var notification = new NotificationRequest
  81. {
  82. Description = installationInfo.Description,
  83. NotificationType = type
  84. };
  85. notification.Variables["Name"] = installationInfo.Name;
  86. notification.Variables["Version"] = installationInfo.Version.ToString();
  87. notification.Variables["ReleaseNotes"] = e.Argument.Item2.description;
  88. await SendNotification(notification).ConfigureAwait(false);
  89. }
  90. async void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e)
  91. {
  92. var type = NotificationType.PluginInstalled.ToString();
  93. var installationInfo = e.Argument;
  94. var notification = new NotificationRequest
  95. {
  96. Description = installationInfo.description,
  97. NotificationType = type
  98. };
  99. notification.Variables["Name"] = installationInfo.name;
  100. notification.Variables["Version"] = installationInfo.versionStr;
  101. await SendNotification(notification).ConfigureAwait(false);
  102. }
  103. async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e)
  104. {
  105. // This notification is for users who can't auto-update (aka running as service)
  106. if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate)
  107. {
  108. return;
  109. }
  110. var type = NotificationType.ApplicationUpdateAvailable.ToString();
  111. var notification = new NotificationRequest
  112. {
  113. Description = "Please see mediabrowser3.com for details.",
  114. NotificationType = type
  115. };
  116. await SendNotification(notification).ConfigureAwait(false);
  117. }
  118. async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
  119. {
  120. if (!_appHost.HasPendingRestart)
  121. {
  122. return;
  123. }
  124. var type = NotificationType.ServerRestartRequired.ToString();
  125. var notification = new NotificationRequest
  126. {
  127. NotificationType = type
  128. };
  129. await SendNotification(notification).ConfigureAwait(false);
  130. }
  131. async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e)
  132. {
  133. var user = e.Users.FirstOrDefault();
  134. var item = e.MediaInfo;
  135. if (item == null)
  136. {
  137. _logger.Warn("PlaybackStart reported with null media info.");
  138. return;
  139. }
  140. if (e.Item != null && e.Item.Parent == null)
  141. {
  142. // Don't report theme song or local trailer playback
  143. // TODO: This will also cause movie specials to not be reported
  144. return;
  145. }
  146. var notification = new NotificationRequest
  147. {
  148. NotificationType = GetPlaybackNotificationType(item.MediaType),
  149. ExcludeUserIds = e.Users.Select(i => i.Id.ToString("N")).ToList()
  150. };
  151. notification.Variables["ItemName"] = item.Name;
  152. notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
  153. notification.Variables["AppName"] = e.ClientName;
  154. notification.Variables["DeviceName"] = e.DeviceName;
  155. await SendNotification(notification).ConfigureAwait(false);
  156. }
  157. private string GetPlaybackNotificationType(string mediaType)
  158. {
  159. if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  160. {
  161. return NotificationType.AudioPlayback.ToString();
  162. }
  163. if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
  164. {
  165. return NotificationType.GamePlayback.ToString();
  166. }
  167. if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  168. {
  169. return NotificationType.VideoPlayback.ToString();
  170. }
  171. return null;
  172. }
  173. async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
  174. {
  175. if (e.Item.LocationType == LocationType.FileSystem)
  176. {
  177. var type = NotificationType.NewLibraryContent.ToString();
  178. var item = e.Item;
  179. var notification = new NotificationRequest
  180. {
  181. NotificationType = type
  182. };
  183. notification.Variables["Name"] = item.Name;
  184. await SendNotification(notification).ConfigureAwait(false);
  185. }
  186. }
  187. async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
  188. {
  189. var notification = new NotificationRequest
  190. {
  191. UserIds = new List<string> { e.Argument.Id.ToString("N") },
  192. Name = "Welcome to Media Browser!",
  193. Description = "Check back here for more notifications."
  194. };
  195. await SendNotification(notification).ConfigureAwait(false);
  196. }
  197. async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
  198. {
  199. var result = e.Argument;
  200. if (result.Status == TaskCompletionStatus.Failed)
  201. {
  202. var type = NotificationType.TaskFailed.ToString();
  203. var notification = new NotificationRequest
  204. {
  205. Description = result.ErrorMessage,
  206. Level = NotificationLevel.Error,
  207. NotificationType = type
  208. };
  209. notification.Variables["Name"] = e.Argument.Name;
  210. notification.Variables["ErrorMessage"] = e.Argument.ErrorMessage;
  211. await SendNotification(notification).ConfigureAwait(false);
  212. }
  213. }
  214. async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  215. {
  216. var type = NotificationType.PluginUninstalled.ToString();
  217. var plugin = e.Argument;
  218. var notification = new NotificationRequest
  219. {
  220. NotificationType = type
  221. };
  222. notification.Variables["Name"] = plugin.Name;
  223. notification.Variables["Version"] = plugin.Version.ToString();
  224. await SendNotification(notification).ConfigureAwait(false);
  225. }
  226. async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  227. {
  228. var installationInfo = e.InstallationInfo;
  229. var type = NotificationType.InstallationFailed.ToString();
  230. var notification = new NotificationRequest
  231. {
  232. Level = NotificationLevel.Error,
  233. Description = e.Exception.Message,
  234. NotificationType = type
  235. };
  236. notification.Variables["Name"] = installationInfo.Name;
  237. notification.Variables["Version"] = installationInfo.Version;
  238. await SendNotification(notification).ConfigureAwait(false);
  239. }
  240. private async Task SendNotification(NotificationRequest notification)
  241. {
  242. try
  243. {
  244. await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
  245. }
  246. catch (Exception ex)
  247. {
  248. _logger.ErrorException("Error sending notification", ex);
  249. }
  250. }
  251. public void Dispose()
  252. {
  253. _installationManager.PluginInstalled -= _installationManager_PluginInstalled;
  254. _installationManager.PluginUpdated -= _installationManager_PluginUpdated;
  255. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  256. _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
  257. _taskManager.TaskCompleted -= _taskManager_TaskCompleted;
  258. _userManager.UserCreated -= _userManager_UserCreated;
  259. _libraryManager.ItemAdded -= _libraryManager_ItemAdded;
  260. _sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
  261. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  262. _appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
  263. _appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
  264. }
  265. }
  266. }