Notifier.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 (e.Item !=null && e.Item.Parent == null)
  136. {
  137. // Don't report theme song or local trailer playback
  138. // TODO: This will also cause movie specials to not be reported
  139. return;
  140. }
  141. var notification = new NotificationRequest
  142. {
  143. NotificationType = GetPlaybackNotificationType(item.MediaType),
  144. ExcludeUserIds = e.Users.Select(i => i.Id.ToString("N")).ToList()
  145. };
  146. notification.Variables["ItemName"] = item.Name;
  147. notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name;
  148. notification.Variables["AppName"] = e.ClientName;
  149. notification.Variables["DeviceName"] = e.DeviceName;
  150. await SendNotification(notification).ConfigureAwait(false);
  151. }
  152. private string GetPlaybackNotificationType(string mediaType)
  153. {
  154. if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase))
  155. {
  156. return NotificationType.AudioPlayback.ToString();
  157. }
  158. if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase))
  159. {
  160. return NotificationType.GamePlayback.ToString();
  161. }
  162. if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase))
  163. {
  164. return NotificationType.VideoPlayback.ToString();
  165. }
  166. return null;
  167. }
  168. async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e)
  169. {
  170. if (e.Item.LocationType == LocationType.FileSystem)
  171. {
  172. var type = NotificationType.NewLibraryContent.ToString();
  173. var item = e.Item;
  174. var notification = new NotificationRequest
  175. {
  176. NotificationType = type
  177. };
  178. notification.Variables["Name"] = item.Name;
  179. await SendNotification(notification).ConfigureAwait(false);
  180. }
  181. }
  182. async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
  183. {
  184. var notification = new NotificationRequest
  185. {
  186. UserIds = new List<string> { e.Argument.Id.ToString("N") },
  187. Name = "Welcome to Media Browser!",
  188. Description = "Check back here for more notifications."
  189. };
  190. await SendNotification(notification).ConfigureAwait(false);
  191. }
  192. async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
  193. {
  194. var result = e.Argument;
  195. if (result.Status == TaskCompletionStatus.Failed)
  196. {
  197. var type = NotificationType.TaskFailed.ToString();
  198. var notification = new NotificationRequest
  199. {
  200. Description = result.ErrorMessage,
  201. Level = NotificationLevel.Error,
  202. NotificationType = type
  203. };
  204. notification.Variables["Name"] = e.Argument.Name;
  205. notification.Variables["ErrorMessage"] = e.Argument.ErrorMessage;
  206. await SendNotification(notification).ConfigureAwait(false);
  207. }
  208. }
  209. async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  210. {
  211. var type = NotificationType.PluginUninstalled.ToString();
  212. var plugin = e.Argument;
  213. var notification = new NotificationRequest
  214. {
  215. NotificationType = type
  216. };
  217. notification.Variables["Name"] = plugin.Name;
  218. notification.Variables["Version"] = plugin.Version.ToString();
  219. await SendNotification(notification).ConfigureAwait(false);
  220. }
  221. async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  222. {
  223. var installationInfo = e.InstallationInfo;
  224. var type = NotificationType.InstallationFailed.ToString();
  225. var notification = new NotificationRequest
  226. {
  227. Level = NotificationLevel.Error,
  228. Description = e.Exception.Message,
  229. NotificationType = type
  230. };
  231. notification.Variables["Name"] = installationInfo.Name;
  232. notification.Variables["Version"] = installationInfo.Version;
  233. await SendNotification(notification).ConfigureAwait(false);
  234. }
  235. private async Task SendNotification(NotificationRequest notification)
  236. {
  237. try
  238. {
  239. await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
  240. }
  241. catch (Exception ex)
  242. {
  243. _logger.ErrorException("Error sending notification", ex);
  244. }
  245. }
  246. public void Dispose()
  247. {
  248. _installationManager.PluginInstalled -= _installationManager_PluginInstalled;
  249. _installationManager.PluginUpdated -= _installationManager_PluginUpdated;
  250. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  251. _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
  252. _taskManager.TaskCompleted -= _taskManager_TaskCompleted;
  253. _userManager.UserCreated -= _userManager_UserCreated;
  254. _libraryManager.ItemAdded -= _libraryManager_ItemAdded;
  255. _sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
  256. _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
  257. _appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
  258. _appHost.ApplicationUpdated -= _appHost_ApplicationUpdated;
  259. }
  260. }
  261. }