From 4cfd4387b6faff88ae916aa7a9d5d57bab7bf990 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 31 Jul 2025 20:48:44 +0800 Subject: [PATCH] :sparkles: Reaction made status --- DysonNetwork.Sphere/Post/Post.cs | 1 + DysonNetwork.Sphere/Post/PostService.cs | 37 +++++++++++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/DysonNetwork.Sphere/Post/Post.cs b/DysonNetwork.Sphere/Post/Post.cs index 9747f4d..2211e2a 100644 --- a/DysonNetwork.Sphere/Post/Post.cs +++ b/DysonNetwork.Sphere/Post/Post.cs @@ -47,6 +47,7 @@ public class Post : ModelBase, IIdentifiedResource, IActivity public int Downvotes { get; set; } [NotMapped] public Dictionary ReactionsCount { get; set; } = new(); [NotMapped] public int RepliesCount { get; set; } + [NotMapped] public Dictionary? ReactionsMade { get; set; } public Guid? RepliedPostId { get; set; } public Post? RepliedPost { get; set; } diff --git a/DysonNetwork.Sphere/Post/PostService.cs b/DysonNetwork.Sphere/Post/PostService.cs index a4f0a99..a56e516 100644 --- a/DysonNetwork.Sphere/Post/PostService.cs +++ b/DysonNetwork.Sphere/Post/PostService.cs @@ -182,7 +182,8 @@ public partial class PostService( Title = localizer["PostReplyTitle", sender.Nick], Body = string.IsNullOrWhiteSpace(post.Title) ? localizer["PostReplyBody", sender.Nick, ChopPostForNotification(post).content] - : localizer["PostReplyContentBody", sender.Nick, post.Title, ChopPostForNotification(post).content], + : localizer["PostReplyContentBody", sender.Nick, post.Title, + ChopPostForNotification(post).content], IsSavable = true, ActionUri = $"/posts/{post.Id}" } @@ -519,6 +520,24 @@ public partial class PostService( ); } + public async Task>> GetPostReactionMadeMapBatch(List postIds, Guid accountId) + { + var reactions = await db.Set() + .Where(r => postIds.Contains(r.PostId) && r.AccountId == accountId) + .Select(r => new { r.PostId, r.Symbol }) + .ToListAsync(); + + return postIds.ToDictionary( + postId => postId, + postId => reactions + .Where(r => r.PostId == postId) + .ToDictionary( + r => r.Symbol, + _ => true + ) + ); + } + /// /// Increases the view count for a post. /// Uses the flush buffer service to batch database updates for better performance. @@ -595,20 +614,28 @@ public partial class PostService( var postsId = posts.Select(e => e.Id).ToList(); var reactionMaps = await GetPostReactionMapBatch(postsId); + var reactionMadeMap = currentUser is not null + ? await GetPostReactionMadeMapBatch(postsId, Guid.Parse(currentUser.Id)) + : new Dictionary>(); var repliesCountMap = await GetPostRepliesCountBatch(postsId); foreach (var post in posts) { - // Set reactions count + // Set reaction count post.ReactionsCount = reactionMaps.TryGetValue(post.Id, out var count) ? count : new Dictionary(); - // Set replies count + // Set reaction made status + post.ReactionsMade = reactionMadeMap.TryGetValue(post.Id, out var made) + ? made + : []; + + // Set reply count post.RepliesCount = repliesCountMap.TryGetValue(post.Id, out var repliesCount) ? repliesCount : 0; - + // Track view for each post in the list if (currentUser != null) await IncreaseViewCount(post.Id, currentUser.Id); @@ -688,4 +715,4 @@ public static class PostQueryExtensions (e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) || publishersId.Contains(e.PublisherId)); } -} +} \ No newline at end of file