2023-07-11 19:07:57 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
using AsbCloudApp.Services.Notifications;
|
|
|
|
using AsbCloudWebApi.SignalR.ConnectionManager;
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2023-07-12 13:31:55 +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
|
|
|
{
|
|
|
|
private readonly IConnectionManager connectionManager;
|
|
|
|
private readonly IHubContext<NotificationHub> notificationHubContext;
|
2023-07-12 13:31:55 +05:00
|
|
|
private readonly IServiceProvider serviceProvider;
|
2023-07-11 19:07:57 +05:00
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
public SignalRNotificationTransportService(IConnectionManager connectionManager,
|
2023-07-11 19:07:57 +05:00
|
|
|
IHubContext<NotificationHub> notificationHubContext,
|
2023-07-12 13:31:55 +05:00
|
|
|
IServiceProvider serviceProvider)
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
|
|
|
this.connectionManager = connectionManager;
|
|
|
|
this.notificationHubContext = notificationHubContext;
|
2023-07-12 13:31:55 +05:00
|
|
|
this.serviceProvider = serviceProvider;
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
2023-07-13 14:44:40 +05:00
|
|
|
|
|
|
|
public int IdTransportType => 0;
|
|
|
|
|
2023-07-11 19:07:57 +05:00
|
|
|
public async Task SendAsync(NotificationDto notification,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
const string method = "notifications";
|
|
|
|
|
|
|
|
var connectionId = connectionManager.GetConnectionIdByUserId(notification.IdUser);
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(connectionId))
|
|
|
|
{
|
2023-07-13 14:44:40 +05:00
|
|
|
notification.SentDate = DateTime.UtcNow;
|
2023-07-12 13:31:55 +05:00
|
|
|
|
2023-07-11 19:07:57 +05:00
|
|
|
await notificationHubContext.Clients.Client(connectionId)
|
|
|
|
.SendAsync(method,
|
2023-07-13 14:44:40 +05:00
|
|
|
notification,
|
2023-07-11 19:07:57 +05:00
|
|
|
cancellationToken);
|
2023-07-13 14:44:40 +05:00
|
|
|
|
2023-07-12 13:31:55 +05:00
|
|
|
var scope = serviceProvider.CreateScope();
|
|
|
|
|
|
|
|
var notificationRepository = scope.ServiceProvider.GetService<INotificationRepository>();
|
|
|
|
|
|
|
|
if (notificationRepository != null)
|
|
|
|
{
|
|
|
|
await notificationRepository.UpdateAsync(notification, cancellationToken);
|
|
|
|
}
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SendRangeAsync(IEnumerable<NotificationDto> notifications,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
foreach (var notification in notifications)
|
|
|
|
{
|
|
|
|
await SendAsync(notification,
|
|
|
|
cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|