Optimize the post notification

This commit is contained in:
2025-08-14 23:20:30 +08:00
parent f273445451
commit cd76cedb7b
2 changed files with 15 additions and 27 deletions

View File

@@ -45,13 +45,6 @@ using (var scope = app.Services.CreateScope())
{ {
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>(); var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
await db.Database.MigrateAsync(); await db.Database.MigrateAsync();
// _ = Task.Run(async () =>
// {
// var migrationScope = app.Services.CreateScope();
// var migrationAuthService = migrationScope.ServiceProvider.GetRequiredService<AuthService>();
// await migrationAuthService.MigrateDeviceIdToClient();
// });
} }
// Configure application middleware pipeline // Configure application middleware pipeline

View File

@@ -78,6 +78,13 @@ public class PublisherSubscriptionService(
queryRequest.Id.AddRange(subscribers.DistinctBy(s => s.AccountId).Select(m => m.AccountId.ToString())); queryRequest.Id.AddRange(subscribers.DistinctBy(s => s.AccountId).Select(m => m.AccountId.ToString()));
var queryResponse = await accounts.GetAccountBatchAsync(queryRequest); var queryResponse = await accounts.GetAccountBatchAsync(queryRequest);
// Notify each subscriber
var notifiedCount = 0;
foreach (var target in queryResponse.Accounts.GroupBy(x => x.Language))
{
try
{
CultureService.SetCultureInfo(target.Key);
var notification = new PushNotification var notification = new PushNotification
{ {
Topic = "posts.new", Topic = "posts.new",
@@ -87,21 +94,9 @@ public class PublisherSubscriptionService(
IsSavable = true, IsSavable = true,
ActionUri = $"/posts/{post.Id}" ActionUri = $"/posts/{post.Id}"
}; };
var request = new SendPushNotificationToUsersRequest { Notification = notification };
// Notify each subscriber request.UserIds.AddRange(target.Select(x => x.Id.ToString()));
var notifiedCount = 0; await pusher.SendPushNotificationToUsersAsync(request);
foreach (var target in queryResponse.Accounts)
{
try
{
CultureService.SetCultureInfo(target);
await pusher.SendPushNotificationToUserAsync(
new SendPushNotificationToUserRequest
{
UserId = target.Id,
Notification = notification
}
);
notifiedCount++; notifiedCount++;
} }
catch (Exception) catch (Exception)