refactor(comment): unify status transitions with atomic count maintenance
Consolidate comment status changes through transitionStatus() to ensure posts.comments_count and comments.replies_count are atomically maintained within the same transaction, preventing inconsistencies between displayed and stored counts. - Remove separate ApplyPublishedStats() calls, now handled in UpdateModerationStatus - Add cache invalidation for admin moderation operations - Update report auto-hide to use unified status transition
This commit is contained in:
@@ -188,7 +188,11 @@ func (s *reportServiceImpl) validateTargetExists(ctx context.Context, targetType
|
||||
return nil
|
||||
}
|
||||
|
||||
// hideTargetContent 隐藏目标内容
|
||||
// hideTargetContent 隐藏目标内容。
|
||||
//
|
||||
// 评论隐藏统一走 commentRepo.UpdateModerationStatus(published → deleted),
|
||||
// 经由仓储内的统一状态转换层维护 posts.comments_count / comments.replies_count,
|
||||
// 避免此前用裸 Update 改 status 导致举报自动隐藏后计数漏减的问题。
|
||||
func (s *reportServiceImpl) hideTargetContent(ctx context.Context, targetType model.ReportTargetType, targetID string) error {
|
||||
switch targetType {
|
||||
case model.ReportTargetTypePost:
|
||||
@@ -205,9 +209,13 @@ func (s *reportServiceImpl) hideTargetContent(ctx context.Context, targetType mo
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if comment != nil && comment.Status != model.CommentStatusDeleted {
|
||||
comment.Status = model.CommentStatusDeleted
|
||||
return s.commentRepo.Update(comment)
|
||||
// 已是终态(deleted/rejected 等非 published)无需再隐藏,也避免重复调整计数
|
||||
if comment == nil || comment.Status != model.CommentStatusPublished {
|
||||
return nil
|
||||
}
|
||||
// published → deleted:统一状态转换层会原子维护计数(永不为负)
|
||||
if _, err := s.commentRepo.UpdateModerationStatus(targetID, model.CommentStatusDeleted, "举报自动隐藏", "system"); err != nil {
|
||||
return err
|
||||
}
|
||||
case model.ReportTargetTypeMessage:
|
||||
// 消息隐藏通过更新状态实现
|
||||
|
||||
Reference in New Issue
Block a user