2023-07-11 19:07:57 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-07-14 11:40:57 +05:00
|
|
|
using System.Linq;
|
2023-07-11 19:07:57 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
2023-07-25 13:36:09 +05:00
|
|
|
using AsbCloudApp.Repositories;
|
2023-07-11 19:07:57 +05:00
|
|
|
using AsbCloudApp.Services.Notifications;
|
2023-07-26 15:41:51 +05:00
|
|
|
using AsbCloudInfrastructure.Background;
|
2023-07-11 19:07:57 +05:00
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2023-07-26 15:41:51 +05:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2023-07-11 19:07:57 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.SignalR.Services;
|
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
public class SignalRNotificationTransportService : INotificationTransportService
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
2023-07-26 15:41:51 +05:00
|
|
|
private readonly NotificationBackgroundWorker backgroundWorker;
|
2023-07-14 11:40:57 +05:00
|
|
|
private readonly ConnectionManagerService connectionManagerService;
|
2023-07-11 19:07:57 +05:00
|
|
|
private readonly IHubContext<NotificationHub> notificationHubContext;
|
|
|
|
|
2023-07-26 15:41:51 +05:00
|
|
|
public SignalRNotificationTransportService(NotificationBackgroundWorker backgroundWorker,
|
|
|
|
ConnectionManagerService connectionManagerService,
|
|
|
|
IHubContext<NotificationHub> notificationHubContext)
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
2023-07-26 15:41:51 +05:00
|
|
|
this.backgroundWorker = backgroundWorker;
|
2023-07-14 11:40:57 +05:00
|
|
|
this.connectionManagerService = connectionManagerService;
|
2023-07-11 19:07:57 +05:00
|
|
|
this.notificationHubContext = notificationHubContext;
|
|
|
|
}
|
2023-07-13 14:44:40 +05:00
|
|
|
|
|
|
|
public int IdTransportType => 0;
|
|
|
|
|
2023-07-26 15:41:51 +05:00
|
|
|
public Task SendAsync(NotificationDto notification,
|
2023-07-11 19:07:57 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-26 15:41:51 +05:00
|
|
|
var workId = notification.Id.ToString();
|
2023-07-11 19:07:57 +05:00
|
|
|
|
2023-07-26 15:41:51 +05:00
|
|
|
if (!backgroundWorker.Contains(workId))
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
2023-07-26 15:41:51 +05:00
|
|
|
var connectionId = connectionManagerService.GetConnectionIdByUserId(notification.IdUser);
|
2023-07-25 13:36:09 +05:00
|
|
|
|
2023-07-26 15:41:51 +05:00
|
|
|
if (!string.IsNullOrWhiteSpace(connectionId))
|
|
|
|
{
|
|
|
|
var workAction = MakeSignalRSendWorkAction(notification, connectionId);
|
|
|
|
var work = new WorkBase(workId, workAction);
|
|
|
|
backgroundWorker.Push(work);
|
|
|
|
}
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
2023-07-26 15:41:51 +05:00
|
|
|
|
|
|
|
return Task.CompletedTask;
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications,
|
2023-07-11 19:07:57 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-17 11:48:52 +05:00
|
|
|
var tasks = notifications
|
|
|
|
.Select(notification => SendAsync(notification, cancellationToken));
|
2023-07-14 11:40:57 +05:00
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
2023-07-26 15:41:51 +05:00
|
|
|
|
|
|
|
private Func<string, IServiceProvider, CancellationToken, Task> MakeSignalRSendWorkAction(NotificationDto notification,
|
|
|
|
string connectionId)
|
|
|
|
{
|
|
|
|
const string method = "receiveNotifications";
|
|
|
|
|
|
|
|
return async (_, serviceProvider, cancellationToken) =>
|
|
|
|
{
|
|
|
|
notification.SentDate = DateTime.UtcNow;
|
|
|
|
|
|
|
|
await notificationHubContext.Clients.Client(connectionId)
|
|
|
|
.SendAsync(method,
|
|
|
|
notification,
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
|
var notificationRepository = serviceProvider.GetRequiredService<INotificationRepository>();
|
|
|
|
|
|
|
|
await notificationRepository.UpdateAsync(notification, cancellationToken);
|
|
|
|
};
|
|
|
|
}
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|