Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

fix(user): Fix the problem that user reputation will become negative

LinkinStar9dc8faace6ed
diff --git a/internal/repo/activity/vote_repo.go b/internal/repo/activity/vote_repo.go
index f934329f..208028df 100644
--- a/internal/repo/activity/vote_repo.go
+++ b/internal/repo/activity/vote_repo.go
@@ -70,6 +70,7 @@ var LimitDownActions = map[string][]string{
func (vr *VoteRepo) vote(ctx context.Context, objectID string, userID, objectUserID string, actions []string) (resp *schema.VoteResp, err error) {
resp = &schema.VoteResp{}
+ notificationUserIDs := make([]string, 0)
_, err = vr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
result = nil
for _, action := range actions {
@@ -126,8 +127,7 @@ func (vr *VoteRepo) vote(ctx context.Context, objectID string, userID, objectUse
if isReachStandard {
insertActivity.Rank = 0
}
-
- vr.sendNotification(ctx, activityUserID, objectUserID, objectID)
+ notificationUserIDs = append(notificationUserIDs, activityUserID)
}
if has {
@@ -165,11 +165,15 @@ func (vr *VoteRepo) vote(ctx context.Context, objectID string, userID, objectUse
resp, err = vr.GetVoteResultByObjectId(ctx, objectID)
resp.VoteStatus = vr.voteCommon.GetVoteStatus(ctx, objectID, userID)
+ for _, activityUserID := range notificationUserIDs {
+ vr.sendNotification(ctx, activityUserID, objectUserID, objectID)
+ }
return
}
func (vr *VoteRepo) voteCancel(ctx context.Context, objectID string, userID, objectUserID string, actions []string) (resp *schema.VoteResp, err error) {
resp = &schema.VoteResp{}
+ notificationUserIDs := make([]string, 0)
_, err = vr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
for _, action := range actions {
var (
@@ -216,13 +220,12 @@ func (vr *VoteRepo) voteCancel(ctx context.Context, objectID string, userID, obj
}
// trigger user rank and send notification
- if hasRank != 0 {
+ if hasRank != 0 && existsActivity.Rank > 0 {
_, err = vr.userRankRepo.TriggerUserRank(ctx, session, activityUserID, -deltaRank, activityType)
if err != nil {
return
}
-
- vr.sendNotification(ctx, activityUserID, objectUserID, objectID)
+ notificationUserIDs = append(notificationUserIDs, activityUserID)
}
// update votes
@@ -245,6 +248,10 @@ func (vr *VoteRepo) voteCancel(ctx context.Context, objectID string, userID, obj
}
resp, err = vr.GetVoteResultByObjectId(ctx, objectID)
resp.VoteStatus = vr.voteCommon.GetVoteStatus(ctx, objectID, userID)
+
+ for _, activityUserID := range notificationUserIDs {
+ vr.sendNotification(ctx, activityUserID, objectUserID, objectID)
+ }
return
}
diff --git a/internal/repo/rank/user_rank_repo.go b/internal/repo/rank/user_rank_repo.go
index 13996817..56a0a42f 100644
--- a/internal/repo/rank/user_rank_repo.go
+++ b/internal/repo/rank/user_rank_repo.go
@@ -43,7 +43,7 @@ func (ur *UserRankRepo) TriggerUserRank(ctx context.Context,
if deltaRank < 0 {
// if user rank is lower than 1 after this action, then user rank will be set to 1 only.
var isReachMin bool
- isReachMin, err = ur.checkUserMinRank(ctx, session, userID, activityType)
+ isReachMin, err = ur.checkUserMinRank(ctx, session, userID, deltaRank)
if err != nil {
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@@ -52,7 +52,7 @@ func (ur *UserRankRepo) TriggerUserRank(ctx context.Context,
if err != nil {
return false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
- return false, nil
+ return true, nil
}
} else {
isReachStandard, err = ur.checkUserTodayRank(ctx, session, userID, activityType)