🐛 Fix queue background service in pusher didn't save notification now

This commit is contained in:
2025-08-24 16:59:27 +08:00
parent d9a5fed77f
commit f8c35c0350
3 changed files with 37 additions and 9 deletions

View File

@@ -2,7 +2,6 @@ using CorePush.Apple;
using CorePush.Firebase;
using DysonNetwork.Pusher.Connection;
using DysonNetwork.Pusher.Services;
using DysonNetwork.Shared.Cache;
using DysonNetwork.Shared.Proto;
using Microsoft.EntityFrameworkCore;
using NodaTime;
@@ -56,7 +55,7 @@ public class PushService
_ws = ws;
_queueService = queueService;
_logger = logger;
_logger.LogInformation("PushService initialized");
}
@@ -264,7 +263,8 @@ public class PushService
await DeliverPushNotification(notification);
}
private async Task SendPushNotificationAsync(PushSubscription subscription, Notification notification, CancellationToken cancellationToken)
private async Task SendPushNotificationAsync(PushSubscription subscription, Notification notification,
CancellationToken cancellationToken)
{
try
{
@@ -361,4 +361,26 @@ public class PushService
_logger.LogInformation(
$"Successfully pushed notification #{notification.Id} to device {subscription.DeviceId} provider {subscription.Provider}");
}
}
public async Task SaveNotification(Notification notification)
{
_db.Notifications.Add(notification);
await _db.SaveChangesAsync();
}
public async Task SaveNotification(Notification notification, List<Guid> accounts)
{
_db.Notifications.AddRange(accounts.Select(a => new Notification
{
AccountId = a,
Content = notification.Content,
Title = notification.Title,
Subtitle = notification.Subtitle,
Meta = notification.Meta,
Priority = notification.Priority,
CreatedAt = notification.CreatedAt,
UpdatedAt = notification.UpdatedAt,
}));
await _db.SaveChangesAsync();
}
}

View File

@@ -105,6 +105,9 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
if (request.Notification.ActionUri is not null)
notification.Meta["action_uri"] = request.Notification.ActionUri;
if (request.Notification.IsSavable)
await pushService.SaveNotification(notification);
await queueService.EnqueuePushNotification(
notification,
@@ -131,11 +134,15 @@ public override Task<Empty> PushWebSocketPacketToDevice(PushWebSocketPacketToDev
if (request.Notification.ActionUri is not null)
notification.Meta["action_uri"] = request.Notification.ActionUri;
var userIds = request.UserIds.Select(Guid.Parse).ToList();
if (request.Notification.IsSavable)
await pushService.SaveNotification(notification, userIds);
var tasks = request.UserIds
var tasks = userIds
.Select(userId => queueService.EnqueuePushNotification(
notification,
Guid.Parse(userId),
userId,
request.Notification.IsSavable
));

View File

@@ -81,7 +81,7 @@ public class QueueBackgroundService(
switch (message.Type)
{
case QueueMessageType.Email:
await ProcessEmailMessageAsync(message, scope, cancellationToken);
await ProcessEmailMessageAsync(message, scope);
break;
case QueueMessageType.PushNotification:
@@ -103,8 +103,7 @@ public class QueueBackgroundService(
}
}
private static async Task ProcessEmailMessageAsync(QueueMessage message, IServiceScope scope,
CancellationToken cancellationToken)
private static async Task ProcessEmailMessageAsync(QueueMessage message, IServiceScope scope)
{
var emailService = scope.ServiceProvider.GetRequiredService<EmailService>();
var emailMessage = JsonSerializer.Deserialize<EmailMessage>(message.Data)