diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index 84dfbc5..922d4d2 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -5,6 +5,7 @@ using DysonNetwork.Shared; using DysonNetwork.Shared.Cache; using DysonNetwork.Shared.Data; using DysonNetwork.Shared.Proto; +using DysonNetwork.Shared.Registry; using DysonNetwork.Sphere.WebReader; using DysonNetwork.Sphere.Localization; using DysonNetwork.Sphere.Poll; @@ -24,6 +25,7 @@ public partial class PostService( ILogger logger, FileService.FileServiceClient files, FileReferenceService.FileReferenceServiceClient fileRefs, + PusherService.PusherServiceClient pusher, PollService polls, Publisher.PublisherService ps, WebReaderService reader @@ -903,6 +905,9 @@ public partial class PostService( string? message ) { + var post = await db.Posts.Where(p => p.Id == postId).FirstOrDefaultAsync(); + if (post is null) throw new InvalidOperationException("Post not found"); + var award = new PostAward { Amount = amount, @@ -920,6 +925,51 @@ public partial class PostService( await db.Posts.Where(p => p.Id == postId) .ExecuteUpdateAsync(s => s.SetProperty(p => p.AwardedScore, p => p.AwardedScore + delta)); + _ = Task.Run(async () => + { + using var scope = factory.CreateScope(); + var pub = scope.ServiceProvider.GetRequiredService(); + var nty = scope.ServiceProvider.GetRequiredService(); + var accounts = scope.ServiceProvider.GetRequiredService(); + var accountsHelper = scope.ServiceProvider.GetRequiredService(); + try + { + var sender = await accountsHelper.GetAccount(accountId); + + var members = await pub.GetPublisherMembers(post.PublisherId); + var queryRequest = new GetAccountBatchRequest(); + queryRequest.Id.AddRange(members.Select(m => m.AccountId.ToString())); + var queryResponse = await accounts.GetAccountBatchAsync(queryRequest); + foreach (var member in queryResponse.Accounts) + { + if (member is null) continue; + CultureService.SetCultureInfo(member); + + await nty.SendPushNotificationToUserAsync( + new SendPushNotificationToUserRequest + { + UserId = member.Id, + Notification = new PushNotification + { + Topic = "posts.awards.new", + Title = localizer["PostAwardedTitle", sender.Nick], + Body = string.IsNullOrWhiteSpace(post.Title) + ? localizer["PostAwardedBody", sender.Nick, amount] + : localizer["PostAwardedContentBody", sender.Nick, amount, + post.Title], + IsSavable = true, + ActionUri = $"/posts/{post.Id}" + } + } + ); + } + } + catch (Exception ex) + { + logger.LogError($"Error when sending post awarded notification: {ex.Message} {ex.StackTrace}"); + } + }); + return award; } } diff --git a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.Designer.cs b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.Designer.cs index c604395..309dd62 100644 --- a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.Designer.cs +++ b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.Designer.cs @@ -158,5 +158,23 @@ namespace DysonNetwork.Sphere.Resources.Localization { return ResourceManager.GetString("OrderPaidBody", resourceCulture); } } + + internal static string PostAwardedTitle { + get { + return ResourceManager.GetString("PostAwardedTitle", resourceCulture); + } + } + + internal static string PostAwardedBody { + get { + return ResourceManager.GetString("PostAwardedBody", resourceCulture); + } + } + + internal static string PostAwardedContentBody { + get { + return ResourceManager.GetString("PostAwardedContentBody", resourceCulture); + } + } } } diff --git a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.resx b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.resx index 283612a..0cbb2ba 100644 --- a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.resx +++ b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.resx @@ -80,4 +80,13 @@ {0} {1} was removed from your wallet to pay {2} + + {0} awarded your post + + + {0} added {1} points to your post + + + {0} added {1} points to your post {2} + \ No newline at end of file diff --git a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.zh-hans.resx b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.zh-hans.resx index 4cf16b1..2f1d2ea 100644 --- a/DysonNetwork.Sphere/Resources/Localization/NotificationResource.zh-hans.resx +++ b/DysonNetwork.Sphere/Resources/Localization/NotificationResource.zh-hans.resx @@ -72,4 +72,13 @@ {0} {1} 已从你的帐户中扣除来支付 {2} + + {0} 赞赏了你的帖子 + + + {0} 给你的帖子 {2} 添加了 {1} 分 + + + {0} 给你的帖子添加了 {1} 分 + \ No newline at end of file diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index 6e97067..77593bf 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -157,7 +157,7 @@ False - True + False False