using System.Collections.Generic; using System.Threading.Tasks; using Jellyfin.Data.Enums; using MediaBrowser.Controller.Authentication; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Session; using Microsoft.Extensions.Logging; namespace Jellyfin.Api.WebSocketListeners; /// /// Class SessionInfoWebSocketListener. /// public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener, WebSocketListenerState> { private readonly ISessionManager _sessionManager; /// /// Initializes a new instance of the class. /// /// Instance of the interface. /// Instance of the interface. public SessionInfoWebSocketListener(ILogger logger, ISessionManager sessionManager) : base(logger) { _sessionManager = sessionManager; _sessionManager.SessionStarted += OnSessionManagerSessionStarted; _sessionManager.SessionEnded += OnSessionManagerSessionEnded; _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart; _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped; _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress; _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged; _sessionManager.SessionActivity += OnSessionManagerSessionActivity; } /// protected override SessionMessageType Type => SessionMessageType.Sessions; /// protected override SessionMessageType StartType => SessionMessageType.SessionsStart; /// protected override SessionMessageType StopType => SessionMessageType.SessionsStop; /// /// Gets the data to send. /// /// Task{SystemInfo}. protected override Task> GetDataToSend() { return Task.FromResult(_sessionManager.Sessions); } /// protected override void Dispose(bool dispose) { if (dispose) { _sessionManager.SessionStarted -= OnSessionManagerSessionStarted; _sessionManager.SessionEnded -= OnSessionManagerSessionEnded; _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart; _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped; _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress; _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged; _sessionManager.SessionActivity -= OnSessionManagerSessionActivity; } base.Dispose(dispose); } /// /// Starts sending messages over a session info 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 subscribe to session information."); } base.Start(message); } private async void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e) { await SendData(false).ConfigureAwait(false); } private async void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e) { await SendData(true).ConfigureAwait(false); } private async void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e) { await SendData(!e.IsAutomated).ConfigureAwait(false); } private async void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e) { await SendData(true).ConfigureAwait(false); } private async void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e) { await SendData(true).ConfigureAwait(false); } private async void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e) { await SendData(true).ConfigureAwait(false); } private async void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e) { await SendData(true).ConfigureAwait(false); } }