jellyfin/MediaBrowser.Server.Implementations/EntryPoints/Notifications/Notifier.cs

207 lines
7.2 KiB
C#
Raw Normal View History

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 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;
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;
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 13:47:56 -07:00
private IServerConfigurationManager _config;
2014-04-25 13:15:50 -07:00
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager)
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;
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;
}
async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
{
2014-04-25 13:15:50 -07:00
var userIds = _userManager
.Users
.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,
2013-07-06 19:01:14 -07:00
Name = "Welcome to Media Browser!",
Description = "Check back here for more notifications."
};
try
{
2014-04-25 13:15:50 -07:00
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
2013-07-06 19:01:14 -07:00
}
catch (Exception ex)
{
2014-04-25 13:15:50 -07:00
_logger.ErrorException("Error sending notification", ex);
2013-07-06 19:01:14 -07:00
}
2013-07-06 14:23:32 -07:00
}
async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
{
var result = e.Argument;
2014-04-25 13:47:56 -07:00
if (result.Status == TaskCompletionStatus.Failed &&
_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
};
try
2013-07-06 14:23:32 -07:00
{
2014-04-25 13:15:50 -07:00
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
}
}
}
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 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
}
}
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 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
}
}
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 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;
2013-07-06 14:23:32 -07:00
}
}
}