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,30 +78,25 @@ 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);
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 // Notify each subscriber
var notifiedCount = 0; var notifiedCount = 0;
foreach (var target in queryResponse.Accounts) foreach (var target in queryResponse.Accounts.GroupBy(x => x.Language))
{ {
try try
{ {
CultureService.SetCultureInfo(target); CultureService.SetCultureInfo(target.Key);
await pusher.SendPushNotificationToUserAsync( var notification = new PushNotification
new SendPushNotificationToUserRequest {
{ Topic = "posts.new",
UserId = target.Id, Title = localizer["PostSubscriptionTitle", post.Publisher.Name, title],
Notification = notification 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++; notifiedCount++;
} }
catch (Exception) catch (Exception)