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-13 14:44:40 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-07-11 19:07:57 +05:00
|
|
|
using AsbCloudApp.Services.Notifications;
|
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]
|
|
|
|
public class NotificationHub : BaseHub
|
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
private readonly ConnectionManagerService connectionManagerService;
|
2023-07-10 16:59:11 +05:00
|
|
|
private readonly INotificationService notificationService;
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public NotificationHub(ConnectionManagerService connectionManagerService,
|
2023-07-11 19:07:57 +05:00
|
|
|
INotificationService notificationService)
|
2023-07-10 16:59:11 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
this.connectionManagerService = connectionManagerService;
|
2023-07-10 16:59:11 +05:00
|
|
|
this.notificationService = notificationService;
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public async Task OnConnected(NotificationRequest request)
|
2023-07-10 16:59:11 +05:00
|
|
|
{
|
2023-07-12 13:31:55 +05:00
|
|
|
try
|
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
await base.OnConnectedAsync();
|
|
|
|
|
|
|
|
var idUser = Context.User?.GetUserId();
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
|
|
|
|
2023-07-12 13:31:55 +05:00
|
|
|
string connectionId = Context.ConnectionId;
|
2023-07-10 16:59:11 +05:00
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
|
2023-07-10 16:59:11 +05:00
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
await notificationService.ResendNotificationAsync(idUser.Value,
|
2023-07-13 14:44:40 +05:00
|
|
|
request,
|
2023-07-12 13:31:55 +05:00
|
|
|
CancellationToken.None);
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public async Task OnDisconnected()
|
2023-07-12 13:31:55 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
await base.OnDisconnectedAsync(null);
|
|
|
|
|
|
|
|
var idUser = Context.User?.GetUserId();
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
connectionManagerService.RemoveConnection(idUser.Value);
|
2023-07-10 16:59:11 +05:00
|
|
|
}
|
|
|
|
}
|