< Summary - Jellyfin

Information
Class: Jellyfin.Api.WebSocketListeners.SessionInfoWebSocketListener
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs
Line coverage
43%
Covered lines: 14
Uncovered lines: 18
Coverable lines: 32
Total lines: 124
Line coverage: 43.7%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/srv/git/jellyfin/Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Threading.Tasks;
 3using Jellyfin.Data.Enums;
 4using MediaBrowser.Controller.Authentication;
 5using MediaBrowser.Controller.Library;
 6using MediaBrowser.Controller.Net;
 7using MediaBrowser.Controller.Session;
 8using MediaBrowser.Model.Session;
 9using Microsoft.Extensions.Logging;
 10
 11namespace Jellyfin.Api.WebSocketListeners;
 12
 13/// <summary>
 14/// Class SessionInfoWebSocketListener.
 15/// </summary>
 16public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerSta
 17{
 18    private readonly ISessionManager _sessionManager;
 19    private bool _disposed;
 20
 21    /// <summary>
 22    /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
 23    /// </summary>
 24    /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
 25    /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
 26    public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
 2027        : base(logger)
 28    {
 2029        _sessionManager = sessionManager;
 30
 2031        _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
 2032        _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
 2033        _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
 2034        _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
 2035        _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
 2036        _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
 2037        _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
 2038    }
 39
 40    /// <inheritdoc />
 041    protected override SessionMessageType Type => SessionMessageType.Sessions;
 42
 43    /// <inheritdoc />
 044    protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
 45
 46    /// <inheritdoc />
 047    protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
 48
 49    /// <summary>
 50    /// Gets the data to send.
 51    /// </summary>
 52    /// <returns>Task{SystemInfo}.</returns>
 53    protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
 54    {
 055        return Task.FromResult(_sessionManager.Sessions);
 56    }
 57
 58    /// <inheritdoc />
 59    protected override async ValueTask DisposeAsyncCore()
 60    {
 61        if (!_disposed)
 62        {
 63            _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
 64            _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
 65            _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
 66            _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
 67            _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
 68            _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
 69            _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
 70            _disposed = true;
 71        }
 72
 73        await base.DisposeAsyncCore().ConfigureAwait(false);
 74    }
 75
 76    /// <summary>
 77    /// Starts sending messages over a session info web socket.
 78    /// </summary>
 79    /// <param name="message">The message.</param>
 80    protected override void Start(WebSocketMessageInfo message)
 81    {
 082        if (!message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
 83        {
 084            throw new AuthenticationException("Only admin users can subscribe to session information.");
 85        }
 86
 087        base.Start(message);
 088    }
 89
 90    private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
 91    {
 1692        SendData(false);
 1693    }
 94
 95    private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
 96    {
 097        SendData(true);
 098    }
 99
 100    private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
 101    {
 0102        SendData(!e.IsAutomated);
 0103    }
 104
 105    private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
 106    {
 0107        SendData(true);
 0108    }
 109
 110    private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
 111    {
 0112        SendData(true);
 0113    }
 114
 115    private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
 116    {
 0117        SendData(true);
 0118    }
 119
 120    private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
 121    {
 16122        SendData(true);
 16123    }
 124}