Iceshrimp.NET/Iceshrimp.Backend/SignalR/StreamingHub.cs
2024-11-20 00:48:29 +01:00

38 lines
No EOL
1.6 KiB
C#

using Iceshrimp.Backend.Core.Middleware;
using Iceshrimp.Backend.Core.Services;
using Iceshrimp.Shared.Schemas.SignalR;
using Microsoft.AspNetCore.SignalR;
namespace Iceshrimp.Backend.SignalR;
[Microsoft.AspNetCore.Authorization.Authorize(Policy = "HubAuthorization")]
public class StreamingHub(StreamingService streamingService) : Hub<IStreamingHubClient>, IStreamingHubServer
{
public Task SubscribeAsync(StreamingTimeline timeline)
{
var userId = Context.UserIdentifier ?? throw new Exception("UserIdentifier must not be null at this stage");
return streamingService.SubscribeAsync(userId, Context.ConnectionId, timeline);
}
public Task UnsubscribeAsync(StreamingTimeline timeline)
{
var userId = Context.UserIdentifier ?? throw new Exception("UserIdentifier must not be null at this stage");
return streamingService.UnsubscribeAsync(userId, Context.ConnectionId, timeline);
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
var ctx = Context.GetHttpContext() ?? throw new Exception("HttpContext must not be null at this stage");
var user = ctx.GetUserOrFail();
var userId = Context.UserIdentifier ?? throw new Exception("UserIdentifier must not be null at this stage");
streamingService.Connect(userId, user, Context.ConnectionId);
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
var userId = Context.UserIdentifier ?? throw new Exception("UserIdentifier must not be null at this stage");
streamingService.Disconnect(userId, Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
}