2023-07-12 13:31:55 +05:00
|
|
|
using System;
|
2023-07-10 16:59:11 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2023-07-11 19:07:57 +05:00
|
|
|
using AsbCloudApp.Services.Notifications;
|
2023-10-30 12:13:38 +05:00
|
|
|
using AsbCloudWebApi.SignalR.Clients;
|
2023-07-14 11:40:57 +05:00
|
|
|
using AsbCloudWebApi.SignalR.Services;
|
2023-07-10 16:59:11 +05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-07-14 11:40:57 +05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-07-10 16:59:11 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.SignalR;
|
|
|
|
|
|
|
|
[Authorize]
|
2023-10-30 12:13:38 +05:00
|
|
|
public class NotificationHub : BaseHub<INotificationHubClient>
|
2023-07-10 16:59:11 +05:00
|
|
|
{
|
2024-07-04 11:02:45 +05:00
|
|
|
private readonly ConnectionManagerService connectionManagerService;
|
|
|
|
private readonly NotificationService notificationService;
|
|
|
|
private readonly NotificationPublisher notificationPublisher;
|
2023-07-10 16:59:11 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public NotificationHub(ConnectionManagerService connectionManagerService,
|
|
|
|
NotificationService notificationService,
|
|
|
|
NotificationPublisher notificationPublisher)
|
|
|
|
{
|
|
|
|
this.connectionManagerService = connectionManagerService;
|
|
|
|
this.notificationService = notificationService;
|
|
|
|
this.notificationPublisher = notificationPublisher;
|
|
|
|
}
|
2023-07-10 16:59:11 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public override async Task OnConnectedAsync()
|
|
|
|
{
|
|
|
|
var idUser = Context.User?.GetUserId();
|
2023-07-17 11:48:52 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
2023-07-17 11:48:52 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
string connectionId = Context.ConnectionId;
|
2023-07-17 11:48:52 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
|
2023-07-17 11:48:52 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
await base.OnConnectedAsync();
|
2023-07-17 11:48:52 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
await notificationPublisher.PublishCountUnreadAsync(idUser.Value, CancellationToken.None);
|
2023-08-04 09:47:22 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
await notificationService.RenotifyAsync(idUser.Value,
|
|
|
|
CancellationToken.None);
|
|
|
|
}
|
2023-07-12 13:31:55 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
|
|
{
|
|
|
|
var idUser = Context.User?.GetUserId();
|
2023-07-14 11:40:57 +05:00
|
|
|
|
2024-07-04 11:02:45 +05:00
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
connectionManagerService.RemoveConnection(idUser.Value);
|
|
|
|
await base.OnDisconnectedAsync(exception);
|
|
|
|
}
|
2023-07-10 16:59:11 +05:00
|
|
|
}
|