2013-07-06 14:23:32 -07:00
|
|
|
|
using MediaBrowser.Common.Events;
|
|
|
|
|
using MediaBrowser.Common.Plugins;
|
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
|
|
|
|
using MediaBrowser.Common.Updates;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
using MediaBrowser.Controller;
|
2014-04-25 13:47:56 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Notifications;
|
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Notifications;
|
2013-09-17 19:43:34 -07:00
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates notifications for various system events
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Notifications : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private readonly IInstallationManager _installationManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
private readonly ITaskManager _taskManager;
|
2014-04-25 13:15:50 -07:00
|
|
|
|
private readonly INotificationManager _notificationManager;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-04-25 13:47:56 -07:00
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager, IServerConfigurationManager config, ILibraryManager libraryManager, ISessionManager sessionManager, IServerApplicationHost appHost)
|
2013-07-06 14:23:32 -07:00
|
|
|
|
{
|
|
|
|
|
_installationManager = installationManager;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_taskManager = taskManager;
|
2014-04-25 13:15:50 -07:00
|
|
|
|
_notificationManager = notificationManager;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
_config = config;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_sessionManager = sessionManager;
|
|
|
|
|
_appHost = appHost;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
|
|
|
|
|
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
|
|
|
|
|
_installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
|
|
|
|
|
|
|
|
|
|
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
|
|
|
|
|
_userManager.UserCreated += _userManager_UserCreated;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
_libraryManager.ItemAdded += _libraryManager_ItemAdded;
|
|
|
|
|
_sessionManager.PlaybackStart += _sessionManager_PlaybackStart;
|
|
|
|
|
_appHost.HasPendingRestartChanged += _appHost_HasPendingRestartChanged;
|
|
|
|
|
_appHost.HasUpdateAvailableChanged += _appHost_HasUpdateAvailableChanged;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
async void _appHost_HasUpdateAvailableChanged(object sender, EventArgs e)
|
2013-07-06 19:01:14 -07:00
|
|
|
|
{
|
2014-04-25 19:55:07 -07:00
|
|
|
|
// This notification is for users who can't auto-update (aka running as service)
|
|
|
|
|
if (!_appHost.HasUpdateAvailable || _appHost.CanSelfUpdate || !_config.Configuration.NotificationOptions.SendOnUpdates)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var userIds = _userManager
|
|
|
|
|
.Users
|
2014-04-25 19:55:07 -07:00
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
2014-04-25 13:15:50 -07:00
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
2013-07-06 19:01:14 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
UserIds = userIds,
|
2014-04-25 19:55:07 -07:00
|
|
|
|
Name = "A new version of Media Browser is available.",
|
|
|
|
|
Description = "Please see mediabrowser3.com for details."
|
2013-07-06 19:01:14 -07:00
|
|
|
|
};
|
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
await SendNotification(notification).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _appHost_HasPendingRestartChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (!_appHost.HasPendingRestart || !_config.Configuration.NotificationOptions.SendOnUpdates)
|
2013-07-06 19:01:14 -07:00
|
|
|
|
{
|
2014-04-25 19:55:07 -07:00
|
|
|
|
return;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
}
|
2014-04-25 19:55:07 -07:00
|
|
|
|
|
|
|
|
|
var userIds = _userManager
|
|
|
|
|
.Users
|
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
2013-07-06 19:01:14 -07:00
|
|
|
|
{
|
2014-04-25 19:55:07 -07:00
|
|
|
|
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;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
}
|
2014-04-25 19:55:07 -07:00
|
|
|
|
|
|
|
|
|
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);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
|
|
|
|
|
{
|
|
|
|
|
var result = e.Argument;
|
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
if (result.Status == TaskCompletionStatus.Failed &&
|
2014-04-25 13:47:56 -07:00
|
|
|
|
_config.Configuration.NotificationOptions.SendOnFailedTasks)
|
2013-07-06 14:23:32 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var userIds = _userManager
|
|
|
|
|
.Users
|
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
|
|
|
|
{
|
|
|
|
|
UserIds = userIds,
|
|
|
|
|
Name = result.Name + " failed",
|
|
|
|
|
Description = result.ErrorMessage,
|
|
|
|
|
Level = NotificationLevel.Error
|
|
|
|
|
};
|
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
await SendNotification(notification).ConfigureAwait(false);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
|
|
|
|
|
{
|
|
|
|
|
var plugin = e.Argument;
|
|
|
|
|
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var userIds = _userManager
|
|
|
|
|
.Users
|
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
2013-07-06 14:23:32 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
UserIds = userIds,
|
|
|
|
|
Name = plugin.Name + " has been uninstalled"
|
|
|
|
|
};
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
await SendNotification(notification).ConfigureAwait(false);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
|
|
|
|
|
{
|
2014-04-25 13:47:56 -07:00
|
|
|
|
if (!_config.Configuration.NotificationOptions.SendOnUpdates)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-06 14:23:32 -07:00
|
|
|
|
var installationInfo = e.InstallationInfo;
|
|
|
|
|
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var userIds = _userManager
|
|
|
|
|
.Users
|
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
2013-07-06 14:23:32 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
UserIds = userIds,
|
|
|
|
|
Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
|
|
|
|
|
Description = e.PackageVersionInfo.description
|
|
|
|
|
};
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
await SendNotification(notification).ConfigureAwait(false);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var installationInfo = e.InstallationInfo;
|
|
|
|
|
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var userIds = _userManager
|
2013-07-06 14:23:32 -07:00
|
|
|
|
.Users
|
|
|
|
|
.Where(i => i.Configuration.IsAdministrator)
|
2014-04-25 13:15:50 -07:00
|
|
|
|
.Select(i => i.Id.ToString("N"))
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
var notification = new NotificationRequest
|
2013-07-06 14:23:32 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
UserIds = userIds,
|
|
|
|
|
Level = NotificationLevel.Error,
|
|
|
|
|
Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
|
|
|
|
|
Description = e.Exception.Message
|
|
|
|
|
};
|
2013-07-06 14:23:32 -07:00
|
|
|
|
|
2014-04-25 19:55:07 -07:00
|
|
|
|
await SendNotification(notification).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task SendNotification(NotificationRequest notification)
|
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending notification", ex);
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
_installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
|
|
|
|
|
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
|
2013-07-06 19:01:14 -07:00
|
|
|
|
_installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
|
|
|
|
|
|
|
|
|
|
_taskManager.TaskCompleted -= _taskManager_TaskCompleted;
|
|
|
|
|
|
|
|
|
|
_userManager.UserCreated -= _userManager_UserCreated;
|
2014-04-25 19:55:07 -07:00
|
|
|
|
_libraryManager.ItemAdded -= _libraryManager_ItemAdded;
|
|
|
|
|
_sessionManager.PlaybackStart -= _sessionManager_PlaybackStart;
|
|
|
|
|
|
|
|
|
|
_appHost.HasPendingRestartChanged -= _appHost_HasPendingRestartChanged;
|
|
|
|
|
_appHost.HasUpdateAvailableChanged -= _appHost_HasUpdateAvailableChanged;
|
2013-07-06 14:23:32 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|