2019-12-27 09:20:27 -05:00
|
|
|
using System;
|
2014-08-11 10:15:53 -04:00
|
|
|
using System.Threading.Tasks;
|
2023-06-09 11:11:22 -04:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-08-13 20:48:28 -04:00
|
|
|
using Jellyfin.Data.Events;
|
2023-06-09 11:11:22 -04:00
|
|
|
using MediaBrowser.Controller.Authentication;
|
2017-10-04 14:51:26 -04:00
|
|
|
using MediaBrowser.Controller.Net;
|
2019-01-13 14:24:58 -05:00
|
|
|
using MediaBrowser.Model.Activity;
|
2020-09-25 03:25:59 -04:00
|
|
|
using MediaBrowser.Model.Session;
|
2019-01-13 14:24:58 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-08-11 10:15:53 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
namespace Jellyfin.Api.WebSocketListeners;
|
|
|
|
|
|
|
|
/// <summary>
|
2023-06-09 11:11:22 -04:00
|
|
|
/// Class ActivityLogWebSocketListener.
|
2023-01-31 06:18:10 -05:00
|
|
|
/// </summary>
|
|
|
|
public class ActivityLogWebSocketListener : BasePeriodicWebSocketListener<ActivityLogEntry[], WebSocketListenerState>
|
2014-08-11 10:15:53 -04:00
|
|
|
{
|
|
|
|
/// <summary>
|
2023-01-31 06:18:10 -05:00
|
|
|
/// The _kernel.
|
2014-08-11 10:15:53 -04:00
|
|
|
/// </summary>
|
2023-01-31 06:18:10 -05:00
|
|
|
private readonly IActivityManager _activityManager;
|
2014-08-11 10:15:53 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private bool _disposed;
|
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="ActivityLogWebSocketListener"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger{ActivityLogWebSocketListener}"/> interface.</param>
|
|
|
|
/// <param name="activityManager">Instance of the <see cref="IActivityManager"/> interface.</param>
|
|
|
|
public ActivityLogWebSocketListener(ILogger<ActivityLogWebSocketListener> logger, IActivityManager activityManager)
|
|
|
|
: base(logger)
|
|
|
|
{
|
|
|
|
_activityManager = activityManager;
|
|
|
|
_activityManager.EntryCreated += OnEntryCreated;
|
|
|
|
}
|
2014-08-11 10:15:53 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType Type => SessionMessageType.ActivityLogEntry;
|
2020-09-25 03:25:59 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StartType => SessionMessageType.ActivityLogEntryStart;
|
2020-09-25 03:25:59 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
protected override SessionMessageType StopType => SessionMessageType.ActivityLogEntryStop;
|
2014-08-11 10:15:53 -04:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the data to send.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task{SystemInfo}.</returns>
|
|
|
|
protected override Task<ActivityLogEntry[]> GetDataToSend()
|
|
|
|
{
|
|
|
|
return Task.FromResult(Array.Empty<ActivityLogEntry>());
|
|
|
|
}
|
2019-01-07 18:27:46 -05:00
|
|
|
|
2023-01-31 06:18:10 -05:00
|
|
|
/// <inheritdoc />
|
2024-03-18 15:55:18 -04:00
|
|
|
protected override async ValueTask DisposeAsyncCore()
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
2024-03-18 15:55:18 -04:00
|
|
|
if (!_disposed)
|
2023-11-04 21:01:14 -04:00
|
|
|
{
|
|
|
|
_activityManager.EntryCreated -= OnEntryCreated;
|
2024-03-18 15:55:18 -04:00
|
|
|
_disposed = true;
|
2023-11-04 21:01:14 -04:00
|
|
|
}
|
2014-08-11 10:15:53 -04:00
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
await base.DisposeAsyncCore().ConfigureAwait(false);
|
2023-01-31 06:18:10 -05:00
|
|
|
}
|
2020-08-03 15:09:32 -04:00
|
|
|
|
2023-07-15 14:14:31 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Starts sending messages over an activity log web socket.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
protected override void Start(WebSocketMessageInfo message)
|
2023-06-09 11:11:22 -04:00
|
|
|
{
|
|
|
|
if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
|
|
|
|
{
|
|
|
|
throw new AuthenticationException("Only admin users can retrieve the activity log.");
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Start(message);
|
|
|
|
}
|
|
|
|
|
2024-03-18 15:55:18 -04:00
|
|
|
private void OnEntryCreated(object? sender, GenericEventArgs<ActivityLogEntry> e)
|
2023-01-31 06:18:10 -05:00
|
|
|
{
|
2024-03-18 15:55:18 -04:00
|
|
|
SendData(true);
|
2014-08-11 10:15:53 -04:00
|
|
|
}
|
|
|
|
}
|