using System; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Requests; using AsbCloudApp.Services.Notifications; using AsbCloudWebApi.SignalR.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.SignalR; [Authorize] public class NotificationHub : BaseHub { private readonly ConnectionManagerService connectionManagerService; private readonly NotificationService notificationService; public NotificationHub(ConnectionManagerService connectionManagerService, NotificationService notificationService) { this.connectionManagerService = connectionManagerService; this.notificationService = notificationService; } public override async Task OnConnectedAsync() { var idUser = Context.User?.GetUserId(); if (!idUser.HasValue) return; string connectionId = Context.ConnectionId; connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId); await base.OnConnectedAsync(); await notificationService.ResendNotificationAsync(idUser.Value, new NotificationRequest { IsSent = false, IdTransportType = 0}, CancellationToken.None); } public override async Task OnDisconnectedAsync(Exception? exception) { var idUser = Context.User?.GetUserId(); if (!idUser.HasValue) return; connectionManagerService.RemoveConnection(idUser.Value); await base.OnDisconnectedAsync(exception); } }