using System;
using System.Threading.Tasks;
using Jellyfin.Data.Enums;
using Jellyfin.Data.Events;
using MediaBrowser.Controller.Authentication;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Activity;
using MediaBrowser.Model.Session;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Api.WebSocketListeners;
///
/// Class ActivityLogWebSocketListener.
///
public class ActivityLogWebSocketListener : BasePeriodicWebSocketListener
{
///
/// The _kernel.
///
private readonly IActivityManager _activityManager;
private bool _disposed;
///
/// Initializes a new instance of the class.
///
/// Instance of the interface.
/// Instance of the interface.
public ActivityLogWebSocketListener(ILogger logger, IActivityManager activityManager)
: base(logger)
{
_activityManager = activityManager;
_activityManager.EntryCreated += OnEntryCreated;
}
///
protected override SessionMessageType Type => SessionMessageType.ActivityLogEntry;
///
protected override SessionMessageType StartType => SessionMessageType.ActivityLogEntryStart;
///
protected override SessionMessageType StopType => SessionMessageType.ActivityLogEntryStop;
///
/// Gets the data to send.
///
/// Task{SystemInfo}.
protected override Task GetDataToSend()
{
return Task.FromResult(Array.Empty());
}
///
protected override async ValueTask DisposeAsyncCore()
{
if (!_disposed)
{
_activityManager.EntryCreated -= OnEntryCreated;
_disposed = true;
}
await base.DisposeAsyncCore().ConfigureAwait(false);
}
///
/// Starts sending messages over an activity log web socket.
///
/// The message.
protected override void Start(WebSocketMessageInfo message)
{
if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
{
throw new AuthenticationException("Only admin users can retrieve the activity log.");
}
base.Start(message);
}
private void OnEntryCreated(object? sender, GenericEventArgs e)
{
SendData(true);
}
}