🐛 Post reaction fixes

This commit is contained in:
2025-05-05 13:12:20 +08:00
parent 2206676214
commit 02aee07116
6 changed files with 2512 additions and 24 deletions

View File

@ -216,8 +216,8 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
[HttpPost("{id:long}/reactions")]
[Authorize]
[RequiredPermission("global", "posts.reactions.create")]
public async Task<ActionResult<PostReaction>> CreatePostReaction(long id, [FromBody] PostReactionRequest request)
[RequiredPermission("global", "posts.react")]
public async Task<ActionResult<PostReaction>> ReactPost(long id, [FromBody] PostReactionRequest request)
{
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
if (currentUserValue is not Account.Account currentUser) return Unauthorized();
@ -241,9 +241,9 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
PostId = post.Id,
AccountId = currentUser.Id
};
await ps.ModifyPostVotes(post, reaction, isExistingReaction);
var isRemoving = await ps.ModifyPostVotes(post, reaction, isExistingReaction);
if (isExistingReaction) return NoContent();
if (isRemoving) return NoContent();
return Ok(reaction);
}

View File

@ -174,27 +174,39 @@ public class PostService(AppDatabase db, FileService fs, ActivityService act)
/// <param name="post">Post that modifying</param>
/// <param name="reaction">The new / target reaction adding / removing</param>
/// <param name="isRemoving">Indicate this operation is adding / removing</param>
public async Task ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving)
public async Task<bool> ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving)
{
var isExistingReaction = await db.Set<PostReaction>()
.AnyAsync(r => r.PostId == post.Id && r.AccountId == reaction.AccountId);
if (isExistingReaction) return;
if (!isRemoving)
if (isRemoving)
await db.PostReactions
.Where(r => r.PostId == post.Id && r.Symbol == reaction.Symbol && r.AccountId == reaction.AccountId)
.ExecuteDeleteAsync();
else
db.PostReactions.Add(reaction);
if (isExistingReaction)
{
db.Add(reaction);
switch (reaction.Attitude)
{
case PostReactionAttitude.Positive:
post.Upvotes++;
break;
case PostReactionAttitude.Negative:
post.Downvotes++;
break;
}
if (!isRemoving)
await db.SaveChangesAsync();
return isRemoving;
}
switch (reaction.Attitude)
{
case PostReactionAttitude.Positive:
if (isRemoving) post.Upvotes--;
else post.Upvotes++;
break;
case PostReactionAttitude.Negative:
if (isRemoving) post.Downvotes--;
else post.Downvotes++;
break;
}
await db.SaveChangesAsync();
return isRemoving;
}
public async Task<Dictionary<string, int>> GetPostReactionMap(long postId)