diff options
| author | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-04-25 22:55:07 -0400 |
|---|---|---|
| committer | Luke Pulverenti <luke.pulverenti@gmail.com> | 2014-04-25 22:55:07 -0400 |
| commit | c40d3f349463c11c69fc2dcf929dc9f768f8fc86 (patch) | |
| tree | d86a3dc958c03b437af7b8fdc2b7e14d81b6d3fe /MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs | |
| parent | 28f5a9ac756391bbdb56867dc376ff4829e6f312 (diff) | |
add more notifications
Diffstat (limited to 'MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs')
| -rw-r--r-- | MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs | 185 |
1 files changed, 151 insertions, 34 deletions
diff --git a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs index bb03c09e6..fa02e5f76 100644 --- a/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs +++ b/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs @@ -2,17 +2,21 @@ using MediaBrowser.Common.Plugins; using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Common.Updates; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Notifications; using MediaBrowser.Controller.Plugins; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Notifications; using MediaBrowser.Model.Tasks; using System; using System.Linq; using System.Threading; +using System.Threading.Tasks; namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications { @@ -28,15 +32,22 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications private readonly ITaskManager _taskManager; private readonly INotificationManager _notificationManager; - private IServerConfigurationManager _config; + private readonly IServerConfigurationManager _config; + private readonly ILibraryManager _libraryManager; + private readonly ISessionManager _sessionManager; + private readonly IServerApplicationHost _appHost; - public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager) + public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost) { _installationManager = installationManager; _userManager = userManager; _logger = logger; _taskManager = taskManager; _notificationManager = notificationManager; + _config = config; + _libraryManager = libraryManager; + _sessionManager = sessionManager; + _appHost = appHost; } public void Run() @@ -48,37 +59,154 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications _taskManager.TaskCompleted += _taskManager_TaskCompleted; _userManager.UserCreated += _userManager_UserCreated; + _libraryManager.ItemAdded += _libraryManager_ItemAdded; + _sessionManager.PlaybackStart += _sessionManager_PlaybackStart; + _appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged; + _appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged; } - async void _userManager_UserCreated(object sender, GenericEventArgs<User> e) + async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e) { + // This notification is for users who can't auto-update (aka running as service) + if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate || !_config.Configuration.NotificationOptions.SendOnUpdates) + { + return; + } + var userIds = _userManager .Users + .Where(i => i.Configuration.IsAdministrator) .Select(i => i.Id.ToString("N")) .ToList(); var notification = new NotificationRequest { UserIds = userIds, - Name = "Welcome to Media Browser!", - Description = "Check back here for more notifications." + Name = "A new version of Media Browser is available.", + Description = "Please see mediabrowser3.com for details." }; - try + await SendNotification(notification).ConfigureAwait(false); + } + + async void _appHost_HasPendingRestartChanged(object sender, EventArgs e) + { + if (!_appHost.HasPendingRestart || !_config.Configuration.NotificationOptions.SendOnUpdates) { - await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false); + return; } - catch (Exception ex) + + var userIds = _userManager + .Users + .Where(i => i.Configuration.IsAdministrator) + .Select(i => i.Id.ToString("N")) + .ToList(); + + var notification = new NotificationRequest { - _logger.ErrorException("Error sending notification", ex); + UserIds = userIds, + Name = "Please restart Media Browser to finish updating" + }; + + await SendNotification(notification).ConfigureAwait(false); + } + + async void _sessionManager_PlaybackStart(object sender, PlaybackProgressEventArgs e) + { + if (!NotifyOnPlayback(e.MediaInfo.MediaType)) + { + return; + } + + var userIds = _userManager + .Users + .Where(i => i.Configuration.IsAdministrator) + .Select(i => i.Id.ToString("N")) + .ToList(); + + var item = e.MediaInfo; + + var msgName = "playing " + item.Name; + + var user = e.Users.FirstOrDefault(); + + if (user != null) + { + msgName = user.Name + " " + msgName; } + + var notification = new NotificationRequest + { + UserIds = userIds, + Name = msgName + }; + + await SendNotification(notification).ConfigureAwait(false); + } + + private bool NotifyOnPlayback(string mediaType) + { + if (string.Equals(mediaType, MediaType.Audio, StringComparison.OrdinalIgnoreCase)) + { + return _config.Configuration.NotificationOptions.SendOnAudioPlayback; + } + if (string.Equals(mediaType, MediaType.Game, StringComparison.OrdinalIgnoreCase)) + { + return _config.Configuration.NotificationOptions.SendOnGamePlayback; + } + if (string.Equals(mediaType, MediaType.Video, StringComparison.OrdinalIgnoreCase)) + { + return _config.Configuration.NotificationOptions.SendOnVideoPlayback; + } + + return false; + } + + async void _libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) + { + if (_config.Configuration.NotificationOptions.SendOnNewLibraryContent && + e.Item.LocationType == LocationType.FileSystem) + { + var userIds = _userManager + .Users + .Where(i => i.Configuration.IsAdministrator) + .Select(i => i.Id.ToString("N")) + .ToList(); + + var item = e.Item; + + var notification = new NotificationRequest + { + UserIds = userIds, + Name = item.Name + " added to library." + }; + + 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, + Name = "Welcome to Media Browser!", + Description = "Check back here for more notifications." + }; + + await SendNotification(notification).ConfigureAwait(false); } async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e) { var result = e.Argument; - if (result.Status == TaskCompletionStatus.Failed && + if (result.Status == TaskCompletionStatus.Failed && _config.Configuration.NotificationOptions.SendOnFailedTasks) { var userIds = _userManager @@ -95,14 +223,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications Level = NotificationLevel.Error }; - try - { - await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error sending notification", ex); - } + await SendNotification(notification).ConfigureAwait(false); } } @@ -122,14 +243,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications Name = plugin.Name + " has been uninstalled" }; - try - { - await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error sending notification", ex); - } + await SendNotification(notification).ConfigureAwait(false); } async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e) @@ -154,14 +268,7 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications Description = e.PackageVersionInfo.description }; - try - { - await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error sending notification", ex); - } + await SendNotification(notification).ConfigureAwait(false); } async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e) @@ -182,6 +289,11 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications Description = e.Exception.Message }; + await SendNotification(notification).ConfigureAwait(false); + } + + private async Task SendNotification(NotificationRequest notification) + { try { await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false); @@ -201,6 +313,11 @@ namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications _taskManager.TaskCompleted -= _taskManager_TaskCompleted; _userManager.UserCreated -= _userManager_UserCreated; + _libraryManager.ItemAdded -= _libraryManager_ItemAdded; + _sessionManager.PlaybackStart -= _sessionManager_PlaybackStart; + + _appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged; + _appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged; } } } |
