2019-01-13 19:54:44 +00:00
|
|
|
using System;
|
2019-01-13 19:20:41 +00:00
|
|
|
using System.Collections.Generic;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:20:41 +00:00
|
|
|
using System.Threading;
|
2019-01-27 14:40:37 +00:00
|
|
|
using System.Threading.Tasks;
|
2020-05-13 02:10:35 +00:00
|
|
|
using Jellyfin.Data.Entities;
|
2020-08-14 00:48:28 +00:00
|
|
|
using Jellyfin.Data.Events;
|
2019-01-13 19:20:41 +00:00
|
|
|
using MediaBrowser.Common.Plugins;
|
2013-07-01 02:27:50 +00:00
|
|
|
using MediaBrowser.Common.Updates;
|
2013-03-07 05:34:00 +00:00
|
|
|
using MediaBrowser.Controller;
|
2013-03-01 21:22:34 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2013-03-05 04:25:27 +00:00
|
|
|
using MediaBrowser.Controller.Plugins;
|
2013-10-03 01:22:50 +00:00
|
|
|
using MediaBrowser.Controller.Session;
|
2016-10-23 19:47:34 +00:00
|
|
|
using MediaBrowser.Model.Tasks;
|
2020-05-24 06:04:10 +00:00
|
|
|
using MediaBrowser.Model.Updates;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
2016-11-03 22:06:00 +00:00
|
|
|
namespace Emby.Server.Implementations.EntryPoints
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-11-01 17:38:54 +00:00
|
|
|
/// Class WebSocketEvents.
|
2013-03-01 21:22:34 +00:00
|
|
|
/// </summary>
|
2013-10-03 18:02:23 +00:00
|
|
|
public class ServerEventNotifier : IServerEntryPoint
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
/// The user manager.
|
2013-03-01 21:22:34 +00:00
|
|
|
/// </summary>
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
/// The installation manager.
|
2013-03-01 21:22:34 +00:00
|
|
|
/// </summary>
|
2013-03-05 04:25:27 +00:00
|
|
|
private readonly IInstallationManager _installationManager;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
/// The kernel.
|
2013-03-01 21:22:34 +00:00
|
|
|
/// </summary>
|
2013-03-07 05:34:00 +00:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
2013-04-15 18:45:58 +00:00
|
|
|
/// <summary>
|
2019-12-10 15:22:03 +00:00
|
|
|
/// The task manager.
|
2013-04-15 18:45:58 +00:00
|
|
|
/// </summary>
|
2013-03-07 05:34:00 +00:00
|
|
|
private readonly ITaskManager _taskManager;
|
2013-04-15 18:45:58 +00:00
|
|
|
|
2013-10-03 18:02:23 +00:00
|
|
|
private readonly ISessionManager _sessionManager;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ServerEventNotifier"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="appHost">The application host.</param>
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
|
|
|
/// <param name="installationManager">The installation manager.</param>
|
|
|
|
/// <param name="taskManager">The task manager.</param>
|
|
|
|
/// <param name="sessionManager">The session manager.</param>
|
|
|
|
public ServerEventNotifier(
|
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IUserManager userManager,
|
|
|
|
IInstallationManager installationManager,
|
|
|
|
ITaskManager taskManager,
|
|
|
|
ISessionManager sessionManager)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
|
|
|
_userManager = userManager;
|
|
|
|
_installationManager = installationManager;
|
2013-03-07 05:34:00 +00:00
|
|
|
_appHost = appHost;
|
|
|
|
_taskManager = taskManager;
|
2013-10-03 01:22:50 +00:00
|
|
|
_sessionManager = sessionManager;
|
2013-03-05 04:25:27 +00:00
|
|
|
}
|
2013-03-01 21:22:34 +00:00
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
/// <inheritdoc />
|
2019-01-27 14:40:37 +00:00
|
|
|
public Task RunAsync()
|
2013-03-05 04:25:27 +00:00
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
_userManager.OnUserDeleted += OnUserDeleted;
|
|
|
|
_userManager.OnUserUpdated += OnUserUpdated;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
_appHost.HasPendingRestartChanged += OnHasPendingRestartChanged;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
_installationManager.PluginUninstalled += OnPluginUninstalled;
|
|
|
|
_installationManager.PackageInstalling += OnPackageInstalling;
|
|
|
|
_installationManager.PackageInstallationCancelled += OnPackageInstallationCancelled;
|
|
|
|
_installationManager.PackageInstallationCompleted += OnPackageInstallationCompleted;
|
|
|
|
_installationManager.PackageInstallationFailed += OnPackageInstallationFailed;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
_taskManager.TaskCompleted += OnTaskCompleted;
|
2019-01-27 14:40:37 +00:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2013-03-07 05:34:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:20:48 +00:00
|
|
|
private async void OnPackageInstalling(object sender, InstallationInfo e)
|
2013-03-07 05:34:00 +00:00
|
|
|
{
|
2020-06-03 19:54:55 +00:00
|
|
|
await SendMessageToAdminSessions("PackageInstalling", e).ConfigureAwait(false);
|
2013-03-07 05:34:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:20:48 +00:00
|
|
|
private async void OnPackageInstallationCancelled(object sender, InstallationInfo e)
|
2013-03-07 05:34:00 +00:00
|
|
|
{
|
2020-06-03 19:54:55 +00:00
|
|
|
await SendMessageToAdminSessions("PackageInstallationCancelled", e).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:20:48 +00:00
|
|
|
private async void OnPackageInstallationCompleted(object sender, InstallationInfo e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-06-03 19:54:55 +00:00
|
|
|
await SendMessageToAdminSessions("PackageInstallationCompleted", e).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendMessageToAdminSessions("PackageInstallationFailed", e.InstallationInfo).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnTaskCompleted(object sender, TaskCompletionEventArgs e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendMessageToAdminSessions("ScheduledTaskEnded", e.Result).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Installations the manager_ plugin uninstalled.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
/// <param name="e">The e.</param>
|
2020-06-03 18:20:48 +00:00
|
|
|
private async void OnPluginUninstalled(object sender, IPlugin e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-06-03 19:54:55 +00:00
|
|
|
await SendMessageToAdminSessions("PluginUninstalled", e).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Handles the HasPendingRestartChanged event of the kernel control.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnHasPendingRestartChanged(object sender, EventArgs e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await _sessionManager.SendRestartRequiredNotification(CancellationToken.None).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Users the manager_ user updated.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
/// <param name="e">The e.</param>
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnUserUpdated(object sender, GenericEventArgs<User> e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2014-08-15 16:35:41 +00:00
|
|
|
var dto = _userManager.GetUserDto(e.Argument);
|
2015-01-13 03:46:44 +00:00
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendMessageToUserSession(e.Argument, "UserUpdated", dto).ConfigureAwait(false);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Users the manager_ user deleted.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
/// <param name="e">The e.</param>
|
2020-05-25 21:52:51 +00:00
|
|
|
private async void OnUserDeleted(object sender, GenericEventArgs<User> e)
|
2013-03-01 21:22:34 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await SendMessageToUserSession(e.Argument, "UserDeleted", e.Argument.Id.ToString("N", CultureInfo.InvariantCulture)).ConfigureAwait(false);
|
2015-01-13 03:46:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async Task SendMessageToAdminSessions<T>(string name, T data)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-10-25 10:47:20 +00:00
|
|
|
await _sessionManager.SendMessageToAdminSessions(name, data, CancellationToken.None).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async Task SendMessageToUserSession<T>(User user, string name, T data)
|
2015-01-13 03:46:44 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
try
|
|
|
|
{
|
2019-11-01 17:38:54 +00:00
|
|
|
await _sessionManager.SendMessageToUserSessions(
|
|
|
|
new List<Guid> { user.Id },
|
|
|
|
name,
|
|
|
|
data,
|
|
|
|
CancellationToken.None).ConfigureAwait(false);
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
}
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
/// <inheritdoc />
|
2013-03-01 21:22:34 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2019-11-01 17:38:54 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
if (dispose)
|
|
|
|
{
|
2020-05-13 02:10:35 +00:00
|
|
|
_userManager.OnUserDeleted -= OnUserDeleted;
|
|
|
|
_userManager.OnUserUpdated -= OnUserUpdated;
|
|
|
|
|
2019-11-01 17:38:54 +00:00
|
|
|
_installationManager.PluginUninstalled -= OnPluginUninstalled;
|
|
|
|
_installationManager.PackageInstalling -= OnPackageInstalling;
|
|
|
|
_installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled;
|
|
|
|
_installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted;
|
|
|
|
_installationManager.PackageInstallationFailed -= OnPackageInstallationFailed;
|
|
|
|
|
|
|
|
_appHost.HasPendingRestartChanged -= OnHasPendingRestartChanged;
|
|
|
|
|
|
|
|
_taskManager.TaskCompleted -= OnTaskCompleted;
|
2013-03-01 21:22:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|