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(activity): incorrectly uses value as id

LinkinStarsd0bc403c8848
diff --git a/internal/repo/activity/activity_repo.go b/internal/repo/activity/activity_repo.go
index 1c364dbf..c1c9f6b0 100644
--- a/internal/repo/activity/activity_repo.go
+++ b/internal/repo/activity/activity_repo.go
@@ -2,13 +2,12 @@ package activity
import (
"context"
- "fmt"
- "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity"
+ "github.com/answerdev/answer/internal/service/activity_type"
"github.com/answerdev/answer/internal/service/config"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
@@ -49,19 +48,12 @@ func (ar *activityRepo) GetObjectAllActivity(ctx context.Context, objectID strin
func (ar *activityRepo) getAllActivityType(ctx context.Context) (activityTypes []int) {
var activityTypeNotShown []int
- for _, obj := range []string{constant.AnswerObjectType, constant.QuestionObjectType, constant.CommentObjectType} {
- for _, act := range []string{
- constant.ActVotedDown,
- constant.ActVotedUp,
- constant.ActVoteDown,
- constant.ActVoteUp,
- } {
- id, err := ar.configService.GetIDByKey(ctx, fmt.Sprintf("%s.%s", obj, act))
- if err != nil {
- log.Error(err)
- } else {
- activityTypeNotShown = append(activityTypeNotShown, id)
- }
+ for _, key := range activity_type.VoteActivityTypeList {
+ id, err := ar.configService.GetIDByKey(ctx, key)
+ if err != nil {
+ log.Errorf("get config id by key [%s] error: %v", key, err)
+ } else {
+ activityTypeNotShown = append(activityTypeNotShown, id)
}
}
return activityTypeNotShown
diff --git a/internal/repo/activity/follow_repo.go b/internal/repo/activity/follow_repo.go
index 6cc1bdca..8f9ade89 100644
--- a/internal/repo/activity/follow_repo.go
+++ b/internal/repo/activity/follow_repo.go
@@ -39,7 +39,11 @@ func NewFollowRepo(
}
func (ar *FollowRepo) Follow(ctx context.Context, objectID, userID string) error {
- activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(ctx, objectID, "follow")
+ objectTypeStr, err := obj.GetObjectTypeStrByObjectID(objectID)
+ if err != nil {
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectTypeStr, "follow")
if err != nil {
return err
}
@@ -102,7 +106,11 @@ func (ar *FollowRepo) Follow(ctx context.Context, objectID, userID string) error
}
func (ar *FollowRepo) FollowCancel(ctx context.Context, objectID, userID string) error {
- activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(ctx, objectID, "follow")
+ objectTypeStr, err := obj.GetObjectTypeStrByObjectID(objectID)
+ if err != nil {
+ return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectTypeStr, "follow")
if err != nil {
return err
}
diff --git a/internal/repo/activity_common/activity_repo.go b/internal/repo/activity_common/activity_repo.go
index 20a37181..a59b7085 100644
--- a/internal/repo/activity_common/activity_repo.go
+++ b/internal/repo/activity_common/activity_repo.go
@@ -51,7 +51,7 @@ func (ar *ActivityRepo) GetActivityTypeByObjID(ctx context.Context, objectID str
if err != nil {
return
}
- rank = cfg.GetIntValue()
+ activityType, rank = cfg.ID, cfg.GetIntValue()
hasRank = 0
if rank != 0 {
hasRank = 1
diff --git a/internal/repo/activity_common/follow.go b/internal/repo/activity_common/follow.go
index 0ab331b6..fdcf5fbd 100644
--- a/internal/repo/activity_common/follow.go
+++ b/internal/repo/activity_common/follow.go
@@ -111,7 +111,7 @@ func (ar *FollowRepo) GetFollowIDs(ctx context.Context, userID, objectKey string
// IsFollowed check user if follow object or not
func (ar *FollowRepo) IsFollowed(ctx context.Context, userID, objectID string) (bool, error) {
- activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(context.TODO(), objectID, "follow")
+ activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(ctx, objectID, "follow")
if err != nil {
return false, err
}
diff --git a/internal/repo/activity_common/vote.go b/internal/repo/activity_common/vote.go
index a8589fc4..d9986cab 100644
--- a/internal/repo/activity_common/vote.go
+++ b/internal/repo/activity_common/vote.go
@@ -8,6 +8,7 @@ import (
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity_common"
"github.com/segmentfault/pacman/errors"
+ "github.com/segmentfault/pacman/log"
)
// VoteRepo activity repository
@@ -29,10 +30,12 @@ func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string)
at := &entity.Activity{}
activityType, _, _, err := vr.activityRepo.GetActivityTypeByObjID(ctx, objectID, action)
if err != nil {
+ log.Error(err)
return ""
}
has, err := vr.data.DB.Context(ctx).Where("object_id =? AND cancelled=0 AND activity_type=? AND user_id=?", objectID, activityType, userID).Get(at)
if err != nil {
+ log.Error(err)
return ""
}
if has {
diff --git a/internal/repo/config/config_repo.go b/internal/repo/config/config_repo.go
index 6b7bb98e..c81b9af7 100644
--- a/internal/repo/config/config_repo.go
+++ b/internal/repo/config/config_repo.go
@@ -42,7 +42,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !exist {
- return nil, errors.BadRequest(reason.ObjectNotFound)
+ return nil, fmt.Errorf("config not found by id: %d", id)
}
// update cache
@@ -68,7 +68,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if !exist {
- return nil, errors.BadRequest(reason.ObjectNotFound)
+ return nil, fmt.Errorf("config not found by key: %s", key)
}
// update cache
diff --git a/internal/service/activity/activity.go b/internal/service/activity/activity.go
index 9f1e9158..ebd5e6df 100644
--- a/internal/service/activity/activity.go
+++ b/internal/service/activity/activity.go
@@ -101,19 +101,18 @@ func (as *ActivityService) GetObjectTimeline(ctx context.Context, req *schema.Ge
}
cfg, err := as.configService.GetConfigByID(ctx, act.ActivityType)
- if err == nil {
- log.Error(err)
- } else {
- item.ActivityType = cfg.Key
- }
- // database save activity type is number, change to activity type string is like "question.asked".
- // so we need to cut the front part of '.', only need string like 'asked'
- _, item.ActivityType, _ = strings.Cut(cfg.Key, ".")
- // format activity type string to show
- if isHidden, formattedActivityType := formatActivity(item.ActivityType); isHidden {
- continue
+ if err != nil {
+ log.Errorf("fail to get config by id: %d, err: %v, act id is: %s", act.ActivityType, err, act.ID)
} else {
- item.ActivityType = formattedActivityType
+ // database save activity type is number, change to activity type string is like "question.asked".
+ // so we need to cut the front part of '.', only need string like 'asked'
+ _, item.ActivityType, _ = strings.Cut(cfg.Key, ".")
+ // format activity type string to show
+ if isHidden, formattedActivityType := formatActivity(item.ActivityType); isHidden {
+ continue
+ } else {
+ item.ActivityType = formattedActivityType
+ }
}
// if activity is down vote, only admin can see who does it.
diff --git a/internal/service/activity_type/activity_type.go b/internal/service/activity_type/activity_type.go
index 65f7992f..9fafcb5b 100644
--- a/internal/service/activity_type/activity_type.go
+++ b/internal/service/activity_type/activity_type.go
@@ -3,46 +3,54 @@ package activity_type
const (
QuestionVoteUp = "question.vote_up"
QuestionVoteDown = "question.vote_down"
- AnswerVoteUp = "answer.vote_up"
- AnswerVoteDown = "answer.vote_down"
- CommentVoteUp = "comment.vote_up"
- CommentVoteDown = "comment.vote_down"
- AnswerAccepted = "answer.accepted"
- AnswerAccept = "answer.accept"
QuestionVotedUp = "question.voted_up"
QuestionVotedDown = "question.voted_down"
+ AnswerVoteUp = "answer.vote_up"
+ AnswerVoteDown = "answer.vote_down"
AnswerVotedUp = "answer.voted_up"
AnswerVotedDown = "answer.voted_down"
+ AnswerAccepted = "answer.accepted"
+ AnswerAccept = "answer.accept"
+ CommentVoteUp = "comment.vote_up"
)
var (
ActivityTypeList = []string{
QuestionVoteUp,
QuestionVoteDown,
+ QuestionVotedUp,
+ QuestionVotedDown,
AnswerVoteUp,
AnswerVoteDown,
- CommentVoteUp,
- CommentVoteDown,
+ AnswerVotedUp,
+ AnswerVotedDown,
AnswerAccepted,
AnswerAccept,
+ CommentVoteUp,
+ }
+ VoteActivityTypeList = []string{
+ QuestionVoteUp,
+ QuestionVoteDown,
QuestionVotedUp,
QuestionVotedDown,
+ AnswerVoteUp,
+ AnswerVoteDown,
AnswerVotedUp,
AnswerVotedDown,
+ CommentVoteUp,
}
activityTypeFlagMapping = map[string]string{
QuestionVoteUp: "upvote",
QuestionVoteDown: "downvote",
- AnswerVoteUp: "upvote",
- AnswerVoteDown: "downvote",
- CommentVoteUp: "upvote",
- CommentVoteDown: "downvote",
- AnswerAccepted: "accepted",
- AnswerAccept: "accept",
QuestionVotedUp: "upvoted",
QuestionVotedDown: "downvoted",
+ AnswerVoteUp: "upvote",
+ AnswerVoteDown: "downvote",
AnswerVotedUp: "upvoted",
AnswerVotedDown: "downvoted",
+ AnswerAccepted: "accepted",
+ AnswerAccept: "accept",
+ CommentVoteUp: "upvote",
}
)
diff --git a/internal/service/question_service.go b/internal/service/question_service.go
index b456d473..026d69ee 100644
--- a/internal/service/question_service.go
+++ b/internal/service/question_service.go
@@ -304,11 +304,11 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
// user add question count
userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, question.UserID)
if err != nil {
- log.Error("user GetUserQuestionCount error", err.Error())
+ log.Errorf("get user question count error %v", err)
} else {
err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, userQuestionCount)
if err != nil {
- log.Error("user IncreaseQuestionCount error", err.Error())
+ log.Errorf("update user question count error %v", err)
}
}