Files
Govor/Govor.API/Hubs/Infrastructure/ChatPushDispatcher.cs
T
Artemy 29cc51ee0c Add reactions, reliable push & session checks
Implements message reactions and reaction packs (domain models, services, admin HTTP API, media processing via ImageSharp and limits) and adds reliable push delivery (ChatPushNotification, ChatPushDispatcher, ChatPushWorker, improved ChatNotificationService). Enhances message reading (ReadChat API, unread counts, concurrency-safe receipts) and tightens session security (access-token session validation in JWT, hub filter, SessionAuthorization). Improves SignalR behavior (session groups, reaction/channel events, hub/error mappings). Adds EF migrations, many controller fixes (Forbid handling, password API), tests (Govor.Server.Tests), docs, CI and tooling updates (Dockerfile → .NET 10, GH Actions matrix for sqlite/postgres, Govor.Server.slnx, global.json), and small infra changes (AutoMapper bump, NullPushProvider, storage/token handling).
2026-10-03 17:42:24 +07:00

52 lines
2.6 KiB
C#

using Govor.Application.Infrastructure.Common;
using Govor.Contracts.Responses.SignalR;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
namespace Govor.API.Hubs.Infrastructure;
public class ChatPushDispatcher(GovorDbContext context, IChatNotificationService notifier,
INowDateTimeProvider dateTimeProvider)
{
public async Task DispatchAsync(CancellationToken cancellationToken = default)
{
var now = dateTimeProvider.Now;
var pending = await context.ChatPushNotifications.AsNoTracking()
.Where(n => n.NextAttemptAt <= now).OrderBy(n => n.NextAttemptAt)
.Take(20).Select(n => n.MessageId).ToListAsync(cancellationToken);
foreach (var id in pending)
{
cancellationToken.ThrowIfCancellationRequested();
// Conditional update claims a two-minute lease, including across server instances.
// An interrupted worker leaves a retryable row rather than losing the notification.
var claimed = await context.ChatPushNotifications
.Where(n => n.MessageId == id && n.NextAttemptAt <= now)
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, now.AddMinutes(2))
.SetProperty(n => n.Attempts, n => n.Attempts + 1), cancellationToken);
if (claimed == 0)
continue;
var message = await context.Messages.AsNoTracking()
.Where(m => m.Id == id).Select(m => new UserMessageResponse
{
MessageId = m.Id, SenderId = m.SenderId, RecipientId = m.RecipientId,
RecipientType = m.RecipientType, EncryptedContent = m.EncryptedContent,
SentAt = m.SentAt, ReplyToMessageId = m.ReplyToMessageId
}).FirstOrDefaultAsync(cancellationToken);
var delivered = message is null || await notifier.DeliverPushAsync(message);
if (delivered)
await context.ChatPushNotifications.Where(n => n.MessageId == id)
.ExecuteDeleteAsync(cancellationToken);
else
{
var attempts = await context.ChatPushNotifications.Where(n => n.MessageId == id)
.Select(n => n.Attempts).FirstOrDefaultAsync(cancellationToken);
var retryAt = dateTimeProvider.Now.AddSeconds(Math.Min(300, Math.Pow(2, Math.Min(attempts, 9))));
await context.ChatPushNotifications.Where(n => n.MessageId == id)
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, retryAt), cancellationToken);
}
}
}
}