< Summary - Jellyfin

Information
Class: Jellyfin.Api.WebSocketListeners.SessionInfoWebSocketListener
Assembly: Jellyfin.Api
File(s): /srv/git/jellyfin/Jellyfin.Api/WebSocketListeners/SessionInfoWebSocketListener.cs
Line coverage
36%
Covered lines: 14
Uncovered lines: 24
Coverable lines: 38
Total lines: 142
Line coverage: 36.8%
Branch coverage
0%
Covered branches: 0
Total branches: 12
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.Linq;
 3using System.Threading.Tasks;
 4using Jellyfin.Data;
 5using Jellyfin.Database.Implementations.Enums;
 6using MediaBrowser.Controller.Authentication;
 7using MediaBrowser.Controller.Library;
 8using MediaBrowser.Controller.Net;
 9using MediaBrowser.Controller.Session;
 10using MediaBrowser.Model.Session;
 11using Microsoft.Extensions.Logging;
 12
 13namespace Jellyfin.Api.WebSocketListeners;
 14
 15/// <summary>
 16/// Class SessionInfoWebSocketListener.
 17/// </summary>
 18public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerSta
 19{
 20    private readonly ISessionManager _sessionManager;
 21    private bool _disposed;
 22
 23    /// <summary>
 24    /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
 25    /// </summary>
 26    /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
 27    /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
 28    public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
 1929        : base(logger)
 30    {
 1931        _sessionManager = sessionManager;
 32
 1933        _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
 1934        _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
 1935        _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
 1936        _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
 1937        _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
 1938        _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
 1939        _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
 1940    }
 41
 42    /// <inheritdoc />
 043    protected override SessionMessageType Type => SessionMessageType.Sessions;
 44
 45    /// <inheritdoc />
 046    protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
 47
 48    /// <inheritdoc />
 049    protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
 50
 51    /// <summary>
 52    /// Gets the data to send.
 53    /// </summary>
 54    /// <returns>Task{SystemInfo}.</returns>
 55    protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
 56    {
 057        return Task.FromResult(_sessionManager.Sessions);
 58    }
 59
 60    /// <inheritdoc />
 61    protected override Task<IEnumerable<SessionInfo>> GetDataToSendForConnection(IWebSocketConnection connection)
 62    {
 63        // For non-admin users, filter the sessions to only include their own sessions
 064        if (connection.AuthorizationInfo?.User is not null &&
 065            !connection.AuthorizationInfo.IsApiKey &&
 066            !connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
 67        {
 068            var userId = connection.AuthorizationInfo.User.Id;
 069            return Task.FromResult(_sessionManager.Sessions.Where(s => s.UserId.Equals(userId) || s.ContainsUser(userId)
 70        }
 71
 072        return Task.FromResult(_sessionManager.Sessions);
 73    }
 74
 75    /// <inheritdoc />
 76    protected override async ValueTask DisposeAsyncCore()
 77    {
 78        if (!_disposed)
 79        {
 80            _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
 81            _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
 82            _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
 83            _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
 84            _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
 85            _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
 86            _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
 87            _disposed = true;
 88        }
 89
 90        await base.DisposeAsyncCore().ConfigureAwait(false);
 91    }
 92
 93    /// <summary>
 94    /// Starts sending messages over a session info web socket.
 95    /// </summary>
 96    /// <param name="message">The message.</param>
 97    protected override void Start(WebSocketMessageInfo message)
 98    {
 99        // Allow all authenticated users to subscribe to session information
 0100        if (message.Connection.AuthorizationInfo.User is null && !message.Connection.AuthorizationInfo.IsApiKey)
 101        {
 0102            throw new AuthenticationException("User must be authenticated to subscribe to session Information.");
 103        }
 104
 0105        base.Start(message);
 0106    }
 107
 108    private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
 109    {
 15110        SendData(false);
 15111    }
 112
 113    private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
 114    {
 0115        SendData(true);
 0116    }
 117
 118    private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
 119    {
 0120        SendData(!e.IsAutomated);
 0121    }
 122
 123    private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
 124    {
 0125        SendData(true);
 0126    }
 127
 128    private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
 129    {
 0130        SendData(true);
 0131    }
 132
 133    private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
 134    {
 0135        SendData(true);
 0136    }
 137
 138    private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
 139    {
 15140        SendData(true);
 15141    }
 142}