mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-20 00:55:13 +01:00
added a notifications service
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
using MediaBrowser.Common.Events;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using MediaBrowser.Common.ScheduledTasks;
|
||||
using MediaBrowser.Common.Updates;
|
||||
using MediaBrowser.Controller.Library;
|
||||
using MediaBrowser.Controller.Notifications;
|
||||
using MediaBrowser.Controller.Plugins;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Notifications;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates notifications for various system events
|
||||
/// </summary>
|
||||
public class Notifications : IServerEntryPoint
|
||||
{
|
||||
private readonly INotificationsRepository _notificationsRepo;
|
||||
private readonly IInstallationManager _installationManager;
|
||||
private readonly IUserManager _userManager;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly ITaskManager _taskManager;
|
||||
|
||||
public Notifications(IInstallationManager installationManager, INotificationsRepository notificationsRepo, IUserManager userManager, ILogger logger, ITaskManager taskManager)
|
||||
{
|
||||
_installationManager = installationManager;
|
||||
_notificationsRepo = notificationsRepo;
|
||||
_userManager = userManager;
|
||||
_logger = logger;
|
||||
_taskManager = taskManager;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
_installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
|
||||
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
|
||||
_installationManager.PluginUninstalled += _installationManager_PluginUninstalled;
|
||||
|
||||
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
|
||||
}
|
||||
|
||||
async void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
|
||||
{
|
||||
var result = e.Argument;
|
||||
|
||||
if (result.Status == TaskCompletionStatus.Failed)
|
||||
{
|
||||
foreach (var user in _userManager
|
||||
.Users
|
||||
.Where(i => i.Configuration.IsAdministrator)
|
||||
.ToList())
|
||||
{
|
||||
var notification = new Notification
|
||||
{
|
||||
UserId = user.Id,
|
||||
Category = "ScheduledTaskFailed",
|
||||
Name = result.Name + " failed",
|
||||
RelatedId = result.Name,
|
||||
Description = result.ErrorMessage,
|
||||
Level = NotificationLevel.Error
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error adding notification", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async void _installationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
|
||||
{
|
||||
var plugin = e.Argument;
|
||||
|
||||
foreach (var user in _userManager
|
||||
.Users
|
||||
.Where(i => i.Configuration.IsAdministrator)
|
||||
.ToList())
|
||||
{
|
||||
var notification = new Notification
|
||||
{
|
||||
UserId = user.Id,
|
||||
Category = "PluginUninstalled",
|
||||
Name = plugin.Name + " has been uninstalled",
|
||||
RelatedId = plugin.Id.ToString()
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error adding notification", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
|
||||
{
|
||||
var installationInfo = e.InstallationInfo;
|
||||
|
||||
foreach (var user in _userManager
|
||||
.Users
|
||||
.Where(i => i.Configuration.IsAdministrator)
|
||||
.ToList())
|
||||
{
|
||||
var notification = new Notification
|
||||
{
|
||||
UserId = user.Id,
|
||||
Category = "PackageInstallationCompleted",
|
||||
Name = installationInfo.Name + " " + installationInfo.Version + " was installed",
|
||||
RelatedId = installationInfo.Name,
|
||||
Description = e.PackageVersionInfo.description
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error adding notification", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
|
||||
{
|
||||
var installationInfo = e.InstallationInfo;
|
||||
|
||||
foreach (var user in _userManager
|
||||
.Users
|
||||
.Where(i => i.Configuration.IsAdministrator)
|
||||
.ToList())
|
||||
{
|
||||
var notification = new Notification
|
||||
{
|
||||
UserId = user.Id,
|
||||
Category = "PackageInstallationFailed",
|
||||
Level = NotificationLevel.Error,
|
||||
Name = installationInfo.Name + " " + installationInfo.Version + " installation failed",
|
||||
RelatedId = installationInfo.Name,
|
||||
Description = e.Exception.Message
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _notificationsRepo.AddNotification(notification, CancellationToken.None).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.ErrorException("Error adding notification", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
|
||||
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Notifications;
|
||||
using MediaBrowser.Controller.Plugins;
|
||||
using System.Linq;
|
||||
|
||||
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// Sends out messages anytime a notification is added or udpated
|
||||
/// </summary>
|
||||
public class WebSocketNotifier : IServerEntryPoint
|
||||
{
|
||||
private readonly INotificationsRepository _notificationsRepo;
|
||||
|
||||
private readonly IServerManager _serverManager;
|
||||
|
||||
public WebSocketNotifier(INotificationsRepository notificationsRepo, IServerManager serverManager)
|
||||
{
|
||||
_notificationsRepo = notificationsRepo;
|
||||
_serverManager = serverManager;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
_notificationsRepo.NotificationAdded += _notificationsRepo_NotificationAdded;
|
||||
_notificationsRepo.NotificationUpdated += _notificationsRepo_NotificationUpdated;
|
||||
|
||||
_notificationsRepo.NotificationsMarkedRead += _notificationsRepo_NotificationsMarkedRead;
|
||||
}
|
||||
|
||||
void _notificationsRepo_NotificationsMarkedRead(object sender, NotificationReadEventArgs e)
|
||||
{
|
||||
var list = e.IdList.Select(i => i.ToString("N")).ToList();
|
||||
|
||||
list.Add(e.UserId.ToString("N"));
|
||||
list.Add(e.IsRead.ToString().ToLower());
|
||||
|
||||
var msg = string.Join("|", list.ToArray());
|
||||
|
||||
_serverManager.SendWebSocketMessage("NotificationsMarkedRead", msg);
|
||||
}
|
||||
|
||||
void _notificationsRepo_NotificationUpdated(object sender, NotificationUpdateEventArgs e)
|
||||
{
|
||||
var msg = e.Notification.UserId + "|" + e.Notification.Id;
|
||||
|
||||
_serverManager.SendWebSocketMessage("NotificationUpdated", msg);
|
||||
}
|
||||
|
||||
void _notificationsRepo_NotificationAdded(object sender, NotificationUpdateEventArgs e)
|
||||
{
|
||||
var msg = e.Notification.UserId + "|" + e.Notification.Id;
|
||||
|
||||
_serverManager.SendWebSocketMessage("NotificationAdded", msg);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_notificationsRepo.NotificationAdded -= _notificationsRepo_NotificationAdded;
|
||||
_notificationsRepo.NotificationUpdated -= _notificationsRepo_NotificationUpdated;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user