SessionInfoWebSocketListener.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Jellyfin.Data;
  4. using Jellyfin.Database.Implementations.Enums;
  5. using MediaBrowser.Controller.Authentication;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Net;
  8. using MediaBrowser.Controller.Session;
  9. using MediaBrowser.Model.Session;
  10. using Microsoft.Extensions.Logging;
  11. namespace Jellyfin.Api.WebSocketListeners;
  12. /// <summary>
  13. /// Class SessionInfoWebSocketListener.
  14. /// </summary>
  15. public class SessionInfoWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<SessionInfo>, WebSocketListenerState>
  16. {
  17. private readonly ISessionManager _sessionManager;
  18. private bool _disposed;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="SessionInfoWebSocketListener"/> class.
  21. /// </summary>
  22. /// <param name="logger">Instance of the <see cref="ILogger{SessionInfoWebSocketListener}"/> interface.</param>
  23. /// <param name="sessionManager">Instance of the <see cref="ISessionManager"/> interface.</param>
  24. public SessionInfoWebSocketListener(ILogger<SessionInfoWebSocketListener> logger, ISessionManager sessionManager)
  25. : base(logger)
  26. {
  27. _sessionManager = sessionManager;
  28. _sessionManager.SessionStarted += OnSessionManagerSessionStarted;
  29. _sessionManager.SessionEnded += OnSessionManagerSessionEnded;
  30. _sessionManager.PlaybackStart += OnSessionManagerPlaybackStart;
  31. _sessionManager.PlaybackStopped += OnSessionManagerPlaybackStopped;
  32. _sessionManager.PlaybackProgress += OnSessionManagerPlaybackProgress;
  33. _sessionManager.CapabilitiesChanged += OnSessionManagerCapabilitiesChanged;
  34. _sessionManager.SessionActivity += OnSessionManagerSessionActivity;
  35. }
  36. /// <inheritdoc />
  37. protected override SessionMessageType Type => SessionMessageType.Sessions;
  38. /// <inheritdoc />
  39. protected override SessionMessageType StartType => SessionMessageType.SessionsStart;
  40. /// <inheritdoc />
  41. protected override SessionMessageType StopType => SessionMessageType.SessionsStop;
  42. /// <summary>
  43. /// Gets the data to send.
  44. /// </summary>
  45. /// <returns>Task{SystemInfo}.</returns>
  46. protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
  47. {
  48. return Task.FromResult(_sessionManager.Sessions);
  49. }
  50. /// <inheritdoc />
  51. protected override async ValueTask DisposeAsyncCore()
  52. {
  53. if (!_disposed)
  54. {
  55. _sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
  56. _sessionManager.SessionEnded -= OnSessionManagerSessionEnded;
  57. _sessionManager.PlaybackStart -= OnSessionManagerPlaybackStart;
  58. _sessionManager.PlaybackStopped -= OnSessionManagerPlaybackStopped;
  59. _sessionManager.PlaybackProgress -= OnSessionManagerPlaybackProgress;
  60. _sessionManager.CapabilitiesChanged -= OnSessionManagerCapabilitiesChanged;
  61. _sessionManager.SessionActivity -= OnSessionManagerSessionActivity;
  62. _disposed = true;
  63. }
  64. await base.DisposeAsyncCore().ConfigureAwait(false);
  65. }
  66. /// <summary>
  67. /// Starts sending messages over a session info web socket.
  68. /// </summary>
  69. /// <param name="message">The message.</param>
  70. protected override void Start(WebSocketMessageInfo message)
  71. {
  72. if (!message.Connection.AuthorizationInfo.IsApiKey
  73. && (message.Connection.AuthorizationInfo.User is null
  74. || !message.Connection.AuthorizationInfo.User.HasPermission(PermissionKind.IsAdministrator)))
  75. {
  76. throw new AuthenticationException("Only admin users can subscribe to session information.");
  77. }
  78. base.Start(message);
  79. }
  80. private void OnSessionManagerSessionActivity(object? sender, SessionEventArgs e)
  81. {
  82. SendData(false);
  83. }
  84. private void OnSessionManagerCapabilitiesChanged(object? sender, SessionEventArgs e)
  85. {
  86. SendData(true);
  87. }
  88. private void OnSessionManagerPlaybackProgress(object? sender, PlaybackProgressEventArgs e)
  89. {
  90. SendData(!e.IsAutomated);
  91. }
  92. private void OnSessionManagerPlaybackStopped(object? sender, PlaybackStopEventArgs e)
  93. {
  94. SendData(true);
  95. }
  96. private void OnSessionManagerPlaybackStart(object? sender, PlaybackProgressEventArgs e)
  97. {
  98. SendData(true);
  99. }
  100. private void OnSessionManagerSessionEnded(object? sender, SessionEventArgs e)
  101. {
  102. SendData(true);
  103. }
  104. private void OnSessionManagerSessionStarted(object? sender, SessionEventArgs e)
  105. {
  106. SendData(true);
  107. }
  108. }