diff options
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs | 233 |
1 files changed, 159 insertions, 74 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs index fa02e5f76..dd2c5bf85 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs @@ -9,14 +9,17 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Notifications; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Notifications; using MediaBrowser.Model.Tasks; using System; +using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using MediaBrowser.Model.Updates; namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications { @@ -52,7 +55,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications public void Run() { - _installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted; + _installationManager.PluginInstalled += _installationManager_PluginInstalled; + _installationManager.PluginUpdated += _installationManager_PluginUpdated; _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed; _installationManager.PluginUninstalled += _installationManager_PluginUninstalled; @@ -65,98 +69,192 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications _appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged; } - async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e) + async void _installationManager_PluginUpdated(object sender, GenericEventArgs<Tuple<IPlugin, PackageVersionInfo>> e) { - // This notification is for users who can't auto-update (aka running as service) - if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate || !_config.Configuration.NotificationOptions.SendOnUpdates) + var type = NotificationType.PluginUpdateInstalled.ToString(); + + var installationInfo = e.Argument.Item1; + + var userIds = _userManager + .Users + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) + .Select(i => i.Id.ToString("N")) + .ToList(); + + var notification = new NotificationRequest { - return; - } + UserIds = userIds, + Description = installationInfo.Description, + NotificationType = type + }; + + notification.Variables["Name"] = installationInfo.Name; + notification.Variables["Version"] = installationInfo.Version.ToString(); + + await SendNotification(notification).ConfigureAwait(false); + } + + async void _installationManager_PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> e) + { + var type = NotificationType.PluginInstalled.ToString(); + + var installationInfo = e.Argument; var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); var notification = new NotificationRequest { UserIds = userIds, - Name = "A new version of Media Browser is available.", - Description = "Please see mediabrowser3.com for details." + Description = installationInfo.description, + NotificationType = type }; + notification.Variables["Name"] = installationInfo.name; + notification.Variables["Version"] = installationInfo.versionStr; + await SendNotification(notification).ConfigureAwait(false); } - async void _appHost_HasPendingRestartChanged(object sender, EventArgs e) + async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e) { - if (!_appHost.HasPendingRestart || !_config.Configuration.NotificationOptions.SendOnUpdates) + // This notification is for users who can't auto-update (aka running as service) + if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate) { return; } + var type = NotificationType.ApplicationUpdateAvailable.ToString(); + var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); var notification = new NotificationRequest { UserIds = userIds, - Name = "Please restart Media Browser to finish updating" + Description = "Please see mediabrowser3.com for details.", + NotificationType = type }; await SendNotification(notification).ConfigureAwait(false); } - async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + async void _appHost_HasPendingRestartChanged(object sender, EventArgs e) { - if (!NotifyOnPlayback(e.MediaInfo.MediaType)) + if (!_appHost.HasPendingRestart) { return; } + var type = NotificationType.ServerRestartRequired.ToString(); + var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); - var item = e.MediaInfo; + var notification = new NotificationRequest + { + UserIds = userIds, + NotificationType = type + }; - var msgName = "playing " + item.Name; + await SendNotification(notification).ConfigureAwait(false); + } + async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + { var user = e.Users.FirstOrDefault(); - if (user != null) - { - msgName = user.Name + " " + msgName; - } + var userIds = _userManager + .Users + .Where(i => NotifyOnPlayback(e.MediaInfo.MediaType, user, i)) + .Select(i => i.Id.ToString("N")) + .ToList(); + + var item = e.MediaInfo; var notification = new NotificationRequest { - UserIds = userIds, - Name = msgName + UserIds = userIds }; + notification.Variables["ItemName"] = item.Name; + notification.Variables["UserName"] = user == null ? "Unknown user" : user.Name; + notification.Variables["AppName"] = e.ClientName; + notification.Variables["DeviceName"] = e.DeviceName; + await SendNotification(notification).ConfigureAwait(false); } - private bool NotifyOnPlayback(string mediaType) + private bool NotifyOnPlayback(string mediaType, User playingUser, User notifiedUser) { if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)) { - return _config.Configuration.NotificationOptions.SendOnAudioPlayback; + var type = NotificationType.AudioPlayback.ToString(); + + if (playingUser != null) + { + if (!_config.Configuration.NotificationOptions.IsEnabledToMonitorUser( + type, playingUser.Id.ToString("N"))) + { + return false; + } + + if (playingUser.Id == notifiedUser.Id) + { + return false; + } + } + + return _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, notifiedUser.Id.ToString("N")); } if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase)) { - return _config.Configuration.NotificationOptions.SendOnGamePlayback; + var type = NotificationType.GamePlayback.ToString(); + + if (playingUser != null) + { + if (!_config.Configuration.NotificationOptions.IsEnabledToMonitorUser( + type, playingUser.Id.ToString("N"))) + { + return false; + } + + if (playingUser.Id == notifiedUser.Id) + { + return false; + } + } + + return _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, notifiedUser.Id.ToString("N")); } if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase)) { - return _config.Configuration.NotificationOptions.SendOnVideoPlayback; + var type = NotificationType.VideoPlayback.ToString(); + + if (playingUser != null) + { + if (!_config.Configuration.NotificationOptions.IsEnabledToMonitorUser( + type, playingUser.Id.ToString("N"))) + { + return false; + } + + if (playingUser.Id == notifiedUser.Id) + { + return false; + } + } + + return _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, notifiedUser.Id.ToString("N")); } return false; @@ -164,12 +262,13 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) { - if (_config.Configuration.NotificationOptions.SendOnNewLibraryContent && - e.Item.LocationType == LocationType.FileSystem) + if (e.Item.LocationType == LocationType.FileSystem) { + var type = NotificationType.NewLibraryContent.ToString(); + var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); @@ -178,23 +277,20 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications var notification = new NotificationRequest { UserIds = userIds, - Name = item.Name + " added to library." + NotificationType = type }; + notification.Variables["Name"] = item.Name; + await SendNotification(notification).ConfigureAwait(false); } } async void _userManager_UserCreated(object sender, GenericEventArgs<User> e) { - var userIds = _userManager - .Users - .Select(i => i.Id.ToString("N")) - .ToList(); - var notification = new NotificationRequest { - UserIds = userIds, + UserIds = new List<string> { e.Argument.Id.ToString("N") }, Name = "Welcome to Media Browser!", Description = "Check back here for more notifications." }; @@ -206,68 +302,51 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications { var result = e.Argument; - if (result.Status == TaskCompletionStatus.Failed && - _config.Configuration.NotificationOptions.SendOnFailedTasks) + if (result.Status == TaskCompletionStatus.Failed) { + var type = NotificationType.TaskFailed.ToString(); + var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); var notification = new NotificationRequest { UserIds = userIds, - Name = result.Name + " failed", Description = result.ErrorMessage, - Level = NotificationLevel.Error + Level = NotificationLevel.Error, + NotificationType = type }; + notification.Variables["Name"] = e.Argument.Name; + await SendNotification(notification).ConfigureAwait(false); } } async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e) { + var type = NotificationType.PluginUninstalled.ToString(); + var plugin = e.Argument; var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) - .Select(i => i.Id.ToString("N")) - .ToList(); - - var notification = new NotificationRequest - { - UserIds = userIds, - Name = plugin.Name + " has been uninstalled" - }; - - await SendNotification(notification).ConfigureAwait(false); - } - - async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e) - { - if (!_config.Configuration.NotificationOptions.SendOnUpdates) - { - return; - } - - var installationInfo = e.InstallationInfo; - - var userIds = _userManager - .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); var notification = new NotificationRequest { UserIds = userIds, - Name = installationInfo.Name + " " + installationInfo.Version + " was installed", - Description = e.PackageVersionInfo.description + NotificationType = type }; + notification.Variables["Name"] = plugin.Name; + notification.Variables["Version"] = plugin.Version.ToString(); + await SendNotification(notification).ConfigureAwait(false); } @@ -275,9 +354,11 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications { var installationInfo = e.InstallationInfo; + var type = NotificationType.InstallationFailed.ToString(); + var userIds = _userManager .Users - .Where(i => i.Configuration.IsAdministrator) + .Where(i => i.Configuration.IsAdministrator && _config.Configuration.NotificationOptions.IsEnabledToSendToUser(type, i.Id.ToString("N"))) .Select(i => i.Id.ToString("N")) .ToList(); @@ -285,10 +366,13 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications { UserIds = userIds, Level = NotificationLevel.Error, - Name = installationInfo.Name + " " + installationInfo.Version + " installation failed", - Description = e.Exception.Message + Description = e.Exception.Message, + NotificationType = type }; + notification.Variables["Name"] = installationInfo.Name; + notification.Variables["Version"] = installationInfo.Version; + await SendNotification(notification).ConfigureAwait(false); } @@ -306,7 +390,8 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications public void Dispose() { - _installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted; + _installationManager.PluginInstalled -= _installationManager_PluginInstalled; + _installationManager.PluginUpdated -= _installationManager_PluginUpdated; _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed; _installationManager.PluginUninstalled -= _installationManager_PluginUninstalled; |
