SessionInfoWebSocketListener.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. namespace Jellyfin.Api.WebSocketListeners;
  13. /// <summary>
  14. /// Class SessionInfoWebSocketListener.
  15. /// </summary>
  16. public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
  17. {
  18. private readonly ISessionManager _sessionManager;
  19. private bool _disposed;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
  22. /// </summary>
  23. /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
  24. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  25. public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
  26. : base(logger)
  27. {
  28. _sessionManager = sessionManager;
  29. _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
  30. _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
  31. _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
  32. _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
  33. _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
  34. _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
  35. _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
  36. }
  37. /// <inheritdoc />
  38. protected override SessionMessageType Type => SessionMessageType.Sessions;
  39. /// <inheritdoc />
  40. protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
  41. /// <inheritdoc />
  42. protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
  43. /// <summary>
  44. /// Gets the data to send.
  45. /// </summary>
  46. /// <returns>Task{SystemInfo}.</returns>
  47. protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
  48. {
  49. return Task.FromResult(_sessionManager.Sessions);
  50. }
  51. /// <inheritdoc />
  52. protected override Task<IEnumerable<SessionInfo>> GetDataToSendForConnection(IWebSocketConnection connection)
  53. {
  54. // For non-admin users, filter the sessions to only include their own sessions
  55. if (connection.AuthorizationInfo?.User is not null &&
  56. !connection.AuthorizationInfo.IsApiKey &&
  57. !connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator))
  58. {
  59. var userId = connection.AuthorizationInfo.User.Id;
  60. return Task.FromResult(_sessionManager.Sessions.Where(s => s.UserId.Equals(userId) || s.ContainsUser(userId)));
  61. }
  62. return Task.FromResult(_sessionManager.Sessions);
  63. }
  64. /// <inheritdoc />
  65. protected override async ValueTask DisposeAsyncCore()
  66. {
  67. if (!_disposed)
  68. {
  69. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  70. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  71. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  72. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  73. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  74. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  75. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  76. _disposed = true;
  77. }
  78. await base.DisposeAsyncCore().ConfigureAwait(false);
  79. }
  80. /// <summary>
  81. /// Starts sending messages over a session info web socket.
  82. /// </summary>
  83. /// <param name="message">The message.</param>
  84. protected override void Start(WebSocketMessageInfo message)
  85. {
  86. // Allow all authenticated users to subscribe to session information
  87. if (message.Connection.AuthorizationInfo.User is null && !message.Connection.AuthorizationInfo.IsApiKey)
  88. {
  89. throw new AuthenticationException("User must be authenticated to subscribe to session Information.");
  90. }
  91. base.Start(message);
  92. }
  93. private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
  94. {
  95. SendData(false);
  96. }
  97. private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
  98. {
  99. SendData(true);
  100. }
  101. private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  102. {
  103. SendData(!e.IsAutomated);
  104. }
  105. private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
  106. {
  107. SendData(true);
  108. }
  109. private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
  110. {
  111. SendData(true);
  112. }
  113. private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
  114. {
  115. SendData(true);
  116. }
  117. private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
  118. {
  119. SendData(true);
  120. }
  121. }