diff --git a/DysonNetwork.Pusher/Notification/PushService.cs b/DysonNetwork.Pusher/Notification/PushService.cs index 0055611..13d4b45 100644 --- a/DysonNetwork.Pusher/Notification/PushService.cs +++ b/DysonNetwork.Pusher/Notification/PushService.cs @@ -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 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(); + } +} \ No newline at end of file diff --git a/DysonNetwork.Pusher/Services/PusherServiceGrpc.cs b/DysonNetwork.Pusher/Services/PusherServiceGrpc.cs index bf24b15..d23623b 100644 --- a/DysonNetwork.Pusher/Services/PusherServiceGrpc.cs +++ b/DysonNetwork.Pusher/Services/PusherServiceGrpc.cs @@ -105,6 +105,9 @@ public override Task 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 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 )); diff --git a/DysonNetwork.Pusher/Services/QueueBackgroundService.cs b/DysonNetwork.Pusher/Services/QueueBackgroundService.cs index f7c4ac9..06ab3c2 100644 --- a/DysonNetwork.Pusher/Services/QueueBackgroundService.cs +++ b/DysonNetwork.Pusher/Services/QueueBackgroundService.cs @@ -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(); var emailMessage = JsonSerializer.Deserialize(message.Data)