From cd76cedb7becb6360b7a8732b12d17591f65884e Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 14 Aug 2025 23:20:30 +0800 Subject: [PATCH] :zap: Optimize the post notification --- DysonNetwork.Pass/Program.cs | 7 ---- .../Publisher/PublisherSubscriptionService.cs | 35 ++++++++----------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/DysonNetwork.Pass/Program.cs b/DysonNetwork.Pass/Program.cs index 000b825..34f3379 100644 --- a/DysonNetwork.Pass/Program.cs +++ b/DysonNetwork.Pass/Program.cs @@ -45,13 +45,6 @@ using (var scope = app.Services.CreateScope()) { var db = scope.ServiceProvider.GetRequiredService(); await db.Database.MigrateAsync(); - - // _ = Task.Run(async () => - // { - // var migrationScope = app.Services.CreateScope(); - // var migrationAuthService = migrationScope.ServiceProvider.GetRequiredService(); - // await migrationAuthService.MigrateDeviceIdToClient(); - // }); } // Configure application middleware pipeline diff --git a/DysonNetwork.Sphere/Publisher/PublisherSubscriptionService.cs b/DysonNetwork.Sphere/Publisher/PublisherSubscriptionService.cs index 562b2a2..e9d7fd7 100644 --- a/DysonNetwork.Sphere/Publisher/PublisherSubscriptionService.cs +++ b/DysonNetwork.Sphere/Publisher/PublisherSubscriptionService.cs @@ -78,30 +78,25 @@ public class PublisherSubscriptionService( queryRequest.Id.AddRange(subscribers.DistinctBy(s => s.AccountId).Select(m => m.AccountId.ToString())); var queryResponse = await accounts.GetAccountBatchAsync(queryRequest); - var notification = new PushNotification - { - Topic = "posts.new", - Title = localizer["PostSubscriptionTitle", post.Publisher.Name, title], - Body = message, - Meta = GrpcTypeHelper.ConvertObjectToByteString(data), - IsSavable = true, - ActionUri = $"/posts/{post.Id}" - }; - // Notify each subscriber var notifiedCount = 0; - foreach (var target in queryResponse.Accounts) + foreach (var target in queryResponse.Accounts.GroupBy(x => x.Language)) { try { - CultureService.SetCultureInfo(target); - await pusher.SendPushNotificationToUserAsync( - new SendPushNotificationToUserRequest - { - UserId = target.Id, - Notification = notification - } - ); + CultureService.SetCultureInfo(target.Key); + var notification = new PushNotification + { + Topic = "posts.new", + Title = localizer["PostSubscriptionTitle", post.Publisher.Name, title], + Body = message, + Meta = GrpcTypeHelper.ConvertObjectToByteString(data), + IsSavable = true, + ActionUri = $"/posts/{post.Id}" + }; + var request = new SendPushNotificationToUsersRequest { Notification = notification }; + request.UserIds.AddRange(target.Select(x => x.Id.ToString())); + await pusher.SendPushNotificationToUsersAsync(request); notifiedCount++; } catch (Exception) @@ -204,4 +199,4 @@ public class PublisherSubscriptionService( return true; } -} \ No newline at end of file +}