forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
5f459b79b8
1. Добавил отправку всех неотправленных уведомлений и кол-во непрочитаннах уведомлений при первом подключении 2. При изменении статуса прочтения уведомления, клиенту отправляется информация о том сколько непрочитанных уведомлений ещё есть. 3. Добавил объект NotificationMessage, который отправляется клиенту. 4. Сделал небольшой рефакторинг
57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Services.Notifications;
|
|
using AsbCloudDb.Model;
|
|
using AsbCloudInfrastructure.Background;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace AsbCloudWebApi.SignalR.Services;
|
|
|
|
public class SignalRNotificationTransportService : INotificationTransportService
|
|
{
|
|
private readonly NotificationBackgroundWorker backgroundWorker;
|
|
|
|
public SignalRNotificationTransportService(NotificationBackgroundWorker backgroundWorker)
|
|
{
|
|
this.backgroundWorker = backgroundWorker;
|
|
}
|
|
|
|
public int IdTransportType => Notification.IdTransportTypeSignalR;
|
|
|
|
public Task SendAsync(NotificationDto notification,
|
|
CancellationToken cancellationToken) => SendRangeAsync(new[] { notification }, cancellationToken);
|
|
|
|
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var workId = HashCode.Combine(notifications.Select(n => n.Id)).ToString("x");
|
|
|
|
if (backgroundWorker.Contains(workId))
|
|
return Task.CompletedTask;
|
|
|
|
var workAction = MakeSignalRSendWorkAction(notifications);
|
|
var work = new WorkBase(workId, workAction);
|
|
backgroundWorker.Push(work);
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
private Func<string, IServiceProvider, CancellationToken, Task> MakeSignalRSendWorkAction(IEnumerable<NotificationDto> notifications)
|
|
{
|
|
return async (_, serviceProvider, cancellationToken) =>
|
|
{
|
|
var notificationPublisher = serviceProvider.GetRequiredService<NotificationPublisher>();
|
|
|
|
var groupedNotificationsByUsers = notifications.GroupBy(n => n.IdUser);
|
|
|
|
foreach (var groupedNotificationByUser in groupedNotificationsByUsers)
|
|
{
|
|
await notificationPublisher.PublishAsync(groupedNotificationByUser, cancellationToken);
|
|
}
|
|
};
|
|
}
|
|
} |