🐛 Fix websocket missing in notification

This commit is contained in:
2025-08-25 17:43:37 +08:00
parent 9b205a73fd
commit 83a49be725
2 changed files with 11 additions and 11 deletions

View File

@@ -5,6 +5,7 @@ using DysonNetwork.Pusher.Services;
using DysonNetwork.Shared.Proto; using DysonNetwork.Shared.Proto;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime; using NodaTime;
using WebSocketPacket = DysonNetwork.Pusher.Connection.WebSocketPacket;
namespace DysonNetwork.Pusher.Notification; namespace DysonNetwork.Pusher.Notification;
@@ -149,6 +150,12 @@ public class PushService
public async Task DeliverPushNotification(Notification notification, CancellationToken cancellationToken = default) public async Task DeliverPushNotification(Notification notification, CancellationToken cancellationToken = default)
{ {
_ws.SendPacketToAccount(notification.AccountId.ToString(), new WebSocketPacket()
{
Type = "notifications.new",
Data = notification,
});
try try
{ {
_logger.LogInformation( _logger.LogInformation(
@@ -162,7 +169,7 @@ public class PushService
.Where(s => s.AccountId == notification.AccountId) .Where(s => s.AccountId == notification.AccountId)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
if (!subscriptions.Any()) if (subscriptions.Count == 0)
{ {
_logger.LogInformation("No push subscriptions found for account {AccountId}", notification.AccountId); _logger.LogInformation("No push subscriptions found for account {AccountId}", notification.AccountId);
return; return;
@@ -174,7 +181,7 @@ public class PushService
{ {
try try
{ {
tasks.Add(SendPushNotificationAsync(subscription, notification, cancellationToken)); tasks.Add(SendPushNotificationAsync(subscription, notification));
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -254,17 +261,10 @@ public class PushService
}); });
} }
// Fetch all subscribers once and enqueue to workers
var subscriptions = await _db.PushSubscriptions
.Where(s => accounts.Contains(s.AccountId))
.AsNoTracking()
.ToListAsync();
await DeliverPushNotification(notification); await DeliverPushNotification(notification);
} }
private async Task SendPushNotificationAsync(PushSubscription subscription, Notification notification, private async Task SendPushNotificationAsync(PushSubscription subscription, Notification notification)
CancellationToken cancellationToken)
{ {
try try
{ {

View File

@@ -121,7 +121,7 @@ public class QueueBackgroundService(
{ {
var pushService = scope.ServiceProvider.GetRequiredService<PushService>(); var pushService = scope.ServiceProvider.GetRequiredService<PushService>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueBackgroundService>>(); var logger = scope.ServiceProvider.GetRequiredService<ILogger<QueueBackgroundService>>();
var notification = JsonSerializer.Deserialize<Notification.Notification>(message.Data); var notification = JsonSerializer.Deserialize<Notification.Notification>(message.Data);
if (notification == null) if (notification == null)
{ {