| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using Jellyfin.Data; |
| | | 4 | | using Jellyfin.Data.Events; |
| | | 5 | | using Jellyfin.Database.Implementations.Enums; |
| | | 6 | | using MediaBrowser.Controller.Authentication; |
| | | 7 | | using MediaBrowser.Controller.Net; |
| | | 8 | | using MediaBrowser.Model.Activity; |
| | | 9 | | using MediaBrowser.Model.Session; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Api.WebSocketListeners; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Class ActivityLogWebSocketListener. |
| | | 16 | | /// </summary> |
| | | 17 | | public class ActivityLogWebSocketListener : BasePeriodicWebSocketListener<ActivityLogEntry[], WebSocketListenerState> |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// The _kernel. |
| | | 21 | | /// </summary> |
| | | 22 | | private readonly IActivityManager _activityManager; |
| | | 23 | | |
| | | 24 | | private bool _disposed; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="ActivityLogWebSocketListener"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="logger">Instance of the <see cref="ILogger{ActivityLogWebSocketListener}"/> interface.</param> |
| | | 30 | | /// <param name="activityManager">Instance of the <see cref="IActivityManager"/> interface.</param> |
| | | 31 | | public ActivityLogWebSocketListener(ILogger<ActivityLogWebSocketListener> logger, IActivityManager activityManager) |
| | 19 | 32 | | : base(logger) |
| | | 33 | | { |
| | 19 | 34 | | _activityManager = activityManager; |
| | 19 | 35 | | _activityManager.EntryCreated += OnEntryCreated; |
| | 19 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 0 | 39 | | protected override SessionMessageType Type => SessionMessageType.ActivityLogEntry; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 0 | 42 | | protected override SessionMessageType StartType => SessionMessageType.ActivityLogEntryStart; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 0 | 45 | | protected override SessionMessageType StopType => SessionMessageType.ActivityLogEntryStop; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the data to send. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <returns>Task{SystemInfo}.</returns> |
| | | 51 | | protected override Task<ActivityLogEntry[]> GetDataToSend() |
| | | 52 | | { |
| | 0 | 53 | | return Task.FromResult(Array.Empty<ActivityLogEntry>()); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | protected override async ValueTask DisposeAsyncCore() |
| | | 58 | | { |
| | | 59 | | if (!_disposed) |
| | | 60 | | { |
| | | 61 | | _activityManager.EntryCreated -= OnEntryCreated; |
| | | 62 | | _disposed = true; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | await base.DisposeAsyncCore().ConfigureAwait(false); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Starts sending messages over an activity log web socket. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="message">The message.</param> |
| | | 72 | | protected override void Start(WebSocketMessageInfo message) |
| | | 73 | | { |
| | 0 | 74 | | if (!message.Connection.AuthorizationInfo.IsApiKey |
| | 0 | 75 | | && (message.Connection.AuthorizationInfo.User is null |
| | 0 | 76 | | || !message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))) |
| | | 77 | | { |
| | 0 | 78 | | throw new AuthenticationException("Only admin users can retrieve the activity log."); |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | base.Start(message); |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | private void OnEntryCreated(object? sender, GenericEventArgs<ActivityLogEntry> e) |
| | | 85 | | { |
| | 34 | 86 | | SendData(true); |
| | 34 | 87 | | } |
| | | 88 | | } |