using MediatR; using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.Authorization; using NexusReader.Application.Commands.Sync; using System.Security.Claims; namespace NexusReader.Infrastructure.RealTime; [Authorize] public class SyncHub : Hub { private readonly IMediator _mediator; public SyncHub(IMediator mediator) { _mediator = mediator; } public async Task UpdateProgress(string pageId, Guid ebookId, double progress, string? chapterTitle, int chapterIndex) { var userId = Context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (userId != null) { await _mediator.Send(new UpdateReadingProgressCommand(pageId, userId, ebookId, progress, chapterTitle, chapterIndex, Context.ConnectionId)); } } public override async Task OnConnectedAsync() { var userId = Context.UserIdentifier; if (!string.IsNullOrEmpty(userId)) { await Groups.AddToGroupAsync(Context.ConnectionId, $"User_{userId}"); } await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception? exception) { var userId = Context.UserIdentifier; if (!string.IsNullOrEmpty(userId)) { await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"User_{userId}"); } await base.OnDisconnectedAsync(exception); } }