Notifier.cs 11 KB

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