| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using Jellyfin.Data; |
| | 5 | | using Jellyfin.Database.Implementations.Enums; |
| | 6 | | using MediaBrowser.Controller.Authentication; |
| | 7 | | using MediaBrowser.Controller.Library; |
| | 8 | | using MediaBrowser.Controller.Net; |
| | 9 | | using MediaBrowser.Controller.Session; |
| | 10 | | using MediaBrowser.Model.Session; |
| | 11 | | using Microsoft.Extensions.Logging; |
| | 12 | |
|
| | 13 | | namespace Jellyfin.Api.WebSocketListeners; |
| | 14 | |
|
| | 15 | | /// <summary> |
| | 16 | | /// Class SessionInfoWebSocketListener. |
| | 17 | | /// </summary> |
| | 18 | | public 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) |
| 19 | 29 | | : base(logger) |
| | 30 | | { |
| 19 | 31 | | _sessionManager = sessionManager; |
| | 32 | |
|
| 19 | 33 | | _sessionManager.SessionStarted += OnSessionManagerSessionStarted; |
| 19 | 34 | | _sessionManager.SessionEnded += OnSessionManagerSessionEnded; |
| 19 | 35 | | _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart; |
| 19 | 36 | | _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped; |
| 19 | 37 | | _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress; |
| 19 | 38 | | _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged; |
| 19 | 39 | | _sessionManager.SessionActivity += OnSessionManagerSessionActivity; |
| 19 | 40 | | } |
| | 41 | |
|
| | 42 | | /// <inheritdoc /> |
| 0 | 43 | | protected override SessionMessageType Type => SessionMessageType.Sessions; |
| | 44 | |
|
| | 45 | | /// <inheritdoc /> |
| 0 | 46 | | protected override SessionMessageType StartType => SessionMessageType.SessionsStart; |
| | 47 | |
|
| | 48 | | /// <inheritdoc /> |
| 0 | 49 | | 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 | | { |
| 0 | 57 | | 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 |
| 0 | 64 | | if (connection.AuthorizationInfo?.User is not null && |
| 0 | 65 | | !connection.AuthorizationInfo.IsApiKey && |
| 0 | 66 | | !connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator)) |
| | 67 | | { |
| 0 | 68 | | var userId = connection.AuthorizationInfo.User.Id; |
| 0 | 69 | | return Task.FromResult(_sessionManager.Sessions.Where(s => s.UserId.Equals(userId) || s.ContainsUser(userId) |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | 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 |
| 0 | 100 | | if (message.Connection.AuthorizationInfo.User is null && !message.Connection.AuthorizationInfo.IsApiKey) |
| | 101 | | { |
| 0 | 102 | | throw new AuthenticationException("User must be authenticated to subscribe to session Information."); |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | base.Start(message); |
| 0 | 106 | | } |
| | 107 | |
|
| | 108 | | private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e) |
| | 109 | | { |
| 15 | 110 | | SendData(false); |
| 15 | 111 | | } |
| | 112 | |
|
| | 113 | | private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e) |
| | 114 | | { |
| 0 | 115 | | SendData(true); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e) |
| | 119 | | { |
| 0 | 120 | | SendData(!e.IsAutomated); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e) |
| | 124 | | { |
| 0 | 125 | | SendData(true); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e) |
| | 129 | | { |
| 0 | 130 | | SendData(true); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e) |
| | 134 | | { |
| 0 | 135 | | SendData(true); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e) |
| | 139 | | { |
| 15 | 140 | | SendData(true); |
| 15 | 141 | | } |
| | 142 | | } |