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

198 lines
6.9 KiB
C#
Raw Normal View History

2013-07-06 21:23:32 +00:00
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Common.Updates;
2013-07-07 02:01:14 +00:00
using MediaBrowser.Controller.Entities;
2013-07-06 21:23:32 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
2013-09-18 02:43:34 +00:00
using MediaBrowser.Model.Tasks;
2013-07-06 21:23:32 +00: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 20:15:50 +00:00
private readonly INotificationManager _notificationManager;
2013-07-06 21:23:32 +00:00
2014-04-25 20:15:50 +00:00
public Notifications(IInstallationManager installationManager, IUserManager userManager, ILogger logger, ITaskManager taskManager, INotificationManager notificationManager)
2013-07-06 21:23:32 +00:00
{
_installationManager = installationManager;
_userManager = userManager;
_logger = logger;
_taskManager = taskManager;
2014-04-25 20:15:50 +00:00
_notificationManager = notificationManager;
2013-07-06 21:23:32 +00:00
}
public void Run()
{
_installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
_installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
2013-07-07 02:01:14 +00:00
_userManager.UserCreated += _userManager_UserCreated;
}
async void _userManager_UserCreated(object sender, GenericEventArgs<User> e)
{
2014-04-25 20:15:50 +00:00
var userIds = _userManager
.Users
.Select(i => i.Id.ToString("N"))
.ToList();
var notification = new NotificationRequest
2013-07-07 02:01:14 +00:00
{
2014-04-25 20:15:50 +00:00
UserIds = userIds,
2013-07-07 02:01:14 +00:00
Name = "Welcome to Media Browser!",
Description = "Check back here for more notifications."
};
try
{
2014-04-25 20:15:50 +00:00
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
2013-07-07 02:01:14 +00:00
}
catch (Exception ex)
{
2014-04-25 20:15:50 +00:00
_logger.ErrorException("Error sending notification", ex);
2013-07-07 02:01:14 +00:00
}
2013-07-06 21:23:32 +00:00
}
async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
{
var result = e.Argument;
if (result.Status == TaskCompletionStatus.Failed)
{
2014-04-25 20:15:50 +00: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 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
2013-07-06 21:23:32 +00:00
}
}
}
async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
{
var plugin = e.Argument;
2014-04-25 20:15:50 +00:00
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.Select(i => i.Id.ToString("N"))
.ToList();
var notification = new NotificationRequest
2013-07-06 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
UserIds = userIds,
Name = plugin.Name + " has been uninstalled"
};
2013-07-06 21:23:32 +00:00
2014-04-25 20:15:50 +00:00
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
2013-07-06 21:23:32 +00:00
}
}
async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
{
var installationInfo = e.InstallationInfo;
2014-04-25 20:15:50 +00:00
var userIds = _userManager
.Users
.Where(i => i.Configuration.IsAdministrator)
.Select(i => i.Id.ToString("N"))
.ToList();
var notification = new NotificationRequest
2013-07-06 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
UserIds = userIds,
Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
Description = e.PackageVersionInfo.description
};
2013-07-06 21:23:32 +00:00
2014-04-25 20:15:50 +00:00
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
2013-07-06 21:23:32 +00:00
}
}
async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
{
var installationInfo = e.InstallationInfo;
2014-04-25 20:15:50 +00:00
var userIds = _userManager
2013-07-06 21:23:32 +00:00
.Users
.Where(i => i.Configuration.IsAdministrator)
2014-04-25 20:15:50 +00:00
.Select(i => i.Id.ToString("N"))
.ToList();
var notification = new NotificationRequest
2013-07-06 21:23:32 +00:00
{
2014-04-25 20:15:50 +00:00
UserIds = userIds,
Level = NotificationLevel.Error,
Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
Description = e.Exception.Message
};
2013-07-06 21:23:32 +00:00
2014-04-25 20:15:50 +00:00
try
{
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error sending notification", ex);
2013-07-06 21:23:32 +00:00
}
}
public void Dispose()
{
_installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
2013-07-07 02:01:14 +00:00
_installationManager.PluginUninstalled -= _installationManager_PluginUninstalled;
_taskManager.TaskCompleted -= _taskManager_TaskCompleted;
_userManager.UserCreated -= _userManager_UserCreated;
2013-07-06 21:23:32 +00:00
}
}
}