mirror of
https://github.com/Govor-team/Govor.git
synced 2026-11-17 04:58:46 +00:00
632ac04b56
Add message forwarding and reply lookup, friend removal and blocking endpoints, and database-backed Govor+ privileges with customizable profile tags. Include migrations, integration tests, and updated API and operations documentation.
69 lines
3.3 KiB
C#
69 lines
3.3 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, ILogger<ChatPushDispatcher> logger = null)
|
|
{
|
|
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 => new { n.MessageId, n.Attempts }).ToListAsync(cancellationToken);
|
|
|
|
foreach (var item in pending)
|
|
{
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
var id = item.MessageId;
|
|
now = dateTimeProvider.Now;
|
|
// 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 && n.Attempts == item.Attempts)
|
|
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, now.AddMinutes(2))
|
|
.SetProperty(n => n.Attempts, n => n.Attempts + 1), cancellationToken);
|
|
if (claimed == 0)
|
|
continue;
|
|
|
|
var attempt = item.Attempts + 1;
|
|
// An old worker must not complete or reschedule a newer worker's lease.
|
|
var owned = context.ChatPushNotifications.Where(n => n.MessageId == id && n.Attempts == attempt);
|
|
var delivered = false;
|
|
try
|
|
{
|
|
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,
|
|
ForwardedFromMessageId = m.ForwardedFromMessageId,
|
|
ForwardedFromUserId = m.ForwardedFromUserId,
|
|
ForwardedFromSentAt = m.ForwardedFromSentAt
|
|
}).FirstOrDefaultAsync(cancellationToken);
|
|
delivered = message is null || await notifier.DeliverPushAsync(message);
|
|
}
|
|
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
|
{
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logger?.LogError(ex, "Push attempt failed for message {MessageId}; delivery will be retried.", id);
|
|
}
|
|
if (delivered)
|
|
await owned.ExecuteDeleteAsync(cancellationToken);
|
|
else
|
|
{
|
|
var retryAt = dateTimeProvider.Now.AddSeconds(Math.Min(300, Math.Pow(2, Math.Min(attempt, 9))));
|
|
await owned
|
|
.ExecuteUpdateAsync(s => s.SetProperty(n => n.NextAttemptAt, retryAt), cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
}
|