Skip to content

Commit

Permalink
feat(badge): add trigger object id for all badge award event
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Sep 4, 2024
1 parent b540191 commit d5d9f38
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 47 deletions.
3 changes: 2 additions & 1 deletion internal/controller/template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ func (tc *TemplateController) QuestionInfo(ctx *gin.Context) {
userInfo, err := tc.userService.GetOtherUserInfoByUsername(
ctx, &schema.GetOtherUserInfoByUsernameReq{Username: shareUsername})
if err == nil {
tc.eventQueueService.Send(ctx, schema.NewEvent(constant.EventUserShare, userInfo.ID))
tc.eventQueueService.Send(ctx, schema.NewEvent(constant.EventUserShare, userInfo.ID).
QID(id, detail.UserID).AID(answerid, ""))
}
}
encodeTitle := htmltext.UrlTitle(detail.Title)
Expand Down
2 changes: 1 addition & 1 deletion internal/entity/badge_award_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
IsBadgeNotDeleted = 0
IsBadgeDeleted = 1

BadgeOnceAwardKey = "0"
BadgeEmptyAwardKey = "0"
)

// BadgeAward badge_award
Expand Down
9 changes: 3 additions & 6 deletions internal/repo/badge/badge_event_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func NewEventRuleRepo(data *data.Data) badge.EventRuleRepo {
func (br *eventRuleRepo) HandleEventWithRule(ctx context.Context, msg *schema.EventMsg) (
awards []*entity.BadgeAward) {
handlers := br.EventRuleMapping[msg.EventType]
for _, h := range handlers {
t, err := h(ctx, msg)
for _, handler := range handlers {
t, err := handler(ctx, msg)
if err != nil {
log.Errorf("error handling badge event %+v: %v", msg, err)
} else {
Expand All @@ -97,7 +97,7 @@ func (br *eventRuleRepo) FirstUpdateUserProfile(ctx context.Context,
continue
}
if len(bean.Bio) > 0 {
awards = append(awards, br.createBadgeAward(event.UserID, "", b))
awards = append(awards, br.createBadgeAward(event.UserID, entity.BadgeEmptyAwardKey, b))
}
}
return awards, nil
Expand Down Expand Up @@ -245,9 +245,6 @@ func (br *eventRuleRepo) getBadgesByHandler(ctx context.Context, handler string)
}

func (br *eventRuleRepo) createBadgeAward(userID, awardKey string, badge *entity.Badge) (awards *entity.BadgeAward) {
if badge.Single == entity.BadgeSingleAward {
awardKey = entity.BadgeOnceAwardKey
}
return &entity.BadgeAward{
UserID: userID,
BadgeID: badge.ID,
Expand Down
9 changes: 9 additions & 0 deletions internal/repo/badge/badge_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ func (r *badgeRepo) UpdateStatus(ctx context.Context, id string, status int8) (e

return
}

// UpdateAwardCount updates the award count of a badge
func (r *badgeRepo) UpdateAwardCount(ctx context.Context, badgeID string, awardCount int) (err error) {
_, err = r.data.DB.Context(ctx).ID(badgeID).Cols("award_count").Update(&entity.Badge{AwardCount: awardCount})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
20 changes: 17 additions & 3 deletions internal/repo/badge_award/badge_award_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ func (r *badgeAwardRepo) AwardBadgeForUser(ctx context.Context, badgeAward *enti
return nil, fmt.Errorf("badge not exist")
}

old := &entity.BadgeAward{}
exist, err = session.Where("user_id = ? AND badge_id = ? AND award_key = ? AND is_badge_deleted = 0",
badgeAward.UserID, badgeAward.BadgeID, badgeAward.AwardKey).Get(old)
old := &entity.BadgeAward{
UserID: badgeAward.UserID,
BadgeID: badgeAward.BadgeID,
IsBadgeDeleted: entity.IsBadgeNotDeleted,
}
if badgeInfo.Single != entity.BadgeSingleAward {
old.AwardKey = badgeAward.AwardKey
}
exist, err = session.Get(old)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,6 +114,14 @@ func (r *badgeAwardRepo) CountByUserIdAndBadgeId(ctx context.Context, userID str
return
}

func (r *badgeAwardRepo) CountByBadgeID(ctx context.Context, badgeID string) (awardCount int64, err error) {
awardCount, err = r.data.DB.Context(ctx).Count(&entity.BadgeAward{BadgeID: badgeID})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}

func (r *badgeAwardRepo) SumUserEarnedGroupByBadgeID(ctx context.Context, userID string) (earnedCounts []*entity.BadgeEarnedCount, err error) {
err = r.data.DB.Context(ctx).Select("badge_id, count(`id`) AS earned_count").Where("user_id = ?", userID).GroupBy("badge_id").Find(&earnedCounts)
return
Expand Down
24 changes: 21 additions & 3 deletions internal/schema/event_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@

package schema

import "github.com/apache/incubator-answer/internal/base/constant"
import (
"github.com/apache/incubator-answer/internal/base/constant"
"github.com/apache/incubator-answer/pkg/uid"
)

// EventMsg event message
type EventMsg struct {
EventType constant.EventType
UserID string

TriggerObjectID string

QuestionID string
QuestionUserID string

Expand All @@ -49,14 +54,18 @@ func NewEvent(e constant.EventType, userID string) *EventMsg {

// QID get question id
func (e *EventMsg) QID(questionID, userID string) *EventMsg {
e.QuestionID = questionID
if len(questionID) > 0 {
e.QuestionID = uid.DeShortID(questionID)
}
e.QuestionUserID = userID
return e
}

// AID get answer id
func (e *EventMsg) AID(answerID, userID string) *EventMsg {
e.AnswerID = answerID
if len(answerID) > 0 {
e.AnswerID = uid.DeShortID(answerID)
}
e.AnswerUserID = userID
return e
}
Expand All @@ -68,6 +77,12 @@ func (e *EventMsg) CID(comment, userID string) *EventMsg {
return e
}

// TID get trigger object id
func (e *EventMsg) TID(triggerObjectID string) *EventMsg {
e.TriggerObjectID = triggerObjectID
return e
}

// AddExtra add extra info
func (e *EventMsg) AddExtra(key, value string) *EventMsg {
e.ExtraInfo[key] = value
Expand All @@ -84,6 +99,9 @@ func (e *EventMsg) GetExtra(key string) string {

// GetObjectID get object id
func (e *EventMsg) GetObjectID() string {
if len(e.TriggerObjectID) > 0 {
return e.TriggerObjectID
}
if len(e.CommentID) > 0 {
return e.CommentID
}
Expand Down
1 change: 1 addition & 0 deletions internal/service/badge/badge_award_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type BadgeAwardRepo interface {
AwardBadgeForUser(ctx context.Context, badgeAward *entity.BadgeAward) (err error)

CountByUserIdAndBadgeId(ctx context.Context, userID string, badgeID string) (awardCount int64)
CountByBadgeID(ctx context.Context, badgeID string) (awardCount int64, err error)

SumUserEarnedGroupByBadgeID(ctx context.Context, userID string) (earnedCounts []*entity.BadgeEarnedCount, err error)

Expand Down
40 changes: 26 additions & 14 deletions internal/service/badge/badge_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/apache/incubator-answer/pkg/uid"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
"strings"
)

Expand All @@ -42,6 +43,7 @@ type BadgeRepo interface {
ListInactivated(ctx context.Context, page int, pageSize int) (badges []*entity.Badge, total int64, err error)

UpdateStatus(ctx context.Context, id string, status int8) (err error)
UpdateAwardCount(ctx context.Context, badgeID string, awardCount int) (err error)
}

type BadgeService struct {
Expand Down Expand Up @@ -266,32 +268,42 @@ func (b *BadgeService) GetBadgeInfo(ctx *gin.Context, id string, userID string)

// UpdateStatus update badge status
func (b *BadgeService) UpdateStatus(ctx *gin.Context, req *schema.UpdateBadgeStatusReq) (err error) {
var (
badge *entity.Badge
exists bool
)
req.ID = uid.DeShortID(req.ID)

badge, exists, err = b.badgeRepo.GetByID(ctx, req.ID)
badge, exists, err := b.badgeRepo.GetByID(ctx, req.ID)
if err != nil {
return
return err
}
if !exists {
err = errors.BadRequest(reason.BadgeObjectNotFound)
return
return errors.BadRequest(reason.BadgeObjectNotFound)
}

status, ok := schema.BadgeStatusEMap[req.Status]
// check duplicate action
if badge.Status == status {
return
}

status, ok := schema.BadgeStatusEMap[req.Status]
if !ok {
err = errors.BadRequest(reason.StatusInvalid)
return
}
if badge.Status == status {
return
}

err = b.badgeRepo.UpdateStatus(ctx, req.ID, status)
return
if err != nil {
return err
}

if status == entity.BadgeStatusActive {
count, err := b.badgeAwardRepo.CountByBadgeID(ctx, badge.ID)
if err != nil {
log.Errorf("count badge award failed: %v", err)
return nil
}
err = b.badgeRepo.UpdateAwardCount(ctx, badge.ID, int(count))
if err != nil {
log.Errorf("update badge award count failed: %v", err)
return nil
}
}
return nil
}
9 changes: 5 additions & 4 deletions internal/service/comment/comment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
switch objInfo.ObjectType {
case constant.QuestionObjectType:
activityMsg.ActivityTypeKey = constant.ActQuestionCommented
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).TID(comment.ID).
CID(comment.ID, comment.UserID).QID(objInfo.QuestionID, objInfo.ObjectCreatorUserID)
case constant.AnswerObjectType:
activityMsg.ActivityTypeKey = constant.ActAnswerCommented
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).
event = schema.NewEvent(constant.EventCommentCreate, req.UserID).TID(comment.ID).
CID(comment.ID, comment.UserID).AID(objInfo.AnswerID, objInfo.ObjectCreatorUserID)
}
cs.activityQueueService.Send(ctx, activityMsg)
Expand Down Expand Up @@ -255,7 +255,8 @@ func (cs *CommentService) RemoveComment(ctx context.Context, req *schema.RemoveC
if err != nil {
return err
}
cs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventCommentDelete, req.UserID).CID(req.CommentID, req.UserID))
cs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventCommentDelete, req.UserID).
TID(req.CommentID).CID(req.CommentID, req.UserID))
return nil
}

Expand Down Expand Up @@ -288,7 +289,7 @@ func (cs *CommentService) UpdateComment(ctx context.Context, req *schema.UpdateC
OriginalText: req.OriginalText,
ParsedText: req.ParsedText,
}
cs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventCommentUpdate, req.UserID).
cs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventCommentUpdate, req.UserID).TID(old.ID).
CID(old.ID, old.UserID))
return resp, nil
}
Expand Down
8 changes: 4 additions & 4 deletions internal/service/content/answer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (as *AnswerService) RemoveAnswer(ctx context.Context, req *schema.RemoveAns
OriginalObjectID: answerInfo.ID,
ActivityTypeKey: constant.ActAnswerDeleted,
})
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerDelete, req.UserID).
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerDelete, req.UserID).TID(answerInfo.ID).
AID(answerInfo.ID, answerInfo.UserID))
return
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
OriginalObjectID: questionInfo.ID,
ActivityTypeKey: constant.ActQuestionAnswered,
})
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerCreate, req.UserID).
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerCreate, req.UserID).TID(insertData.ID).
AID(insertData.ID, insertData.UserID))
return insertData.ID, nil
}
Expand Down Expand Up @@ -391,7 +391,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
ActivityTypeKey: constant.ActAnswerEdited,
RevisionID: revisionID,
})
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerUpdate, req.UserID).
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventAnswerUpdate, req.UserID).TID(insertData.ID).
AID(insertData.ID, insertData.UserID))
}

Expand Down Expand Up @@ -447,7 +447,7 @@ func (as *AnswerService) AcceptAnswer(ctx context.Context, req *schema.AcceptAns
}

if acceptedAnswerInfo != nil {
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionAccept, req.UserID).
as.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionAccept, req.UserID).TID(acceptedAnswerInfo.ID).
QID(questionInfo.ID, questionInfo.UserID).AID(acceptedAnswerInfo.ID, acceptedAnswerInfo.UserID))
}

Expand Down
6 changes: 3 additions & 3 deletions internal/service/content/question_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
qs.externalNotificationQueueService.Send(ctx,
schema.CreateNewQuestionNotificationMsg(question.ID, question.Title, question.UserID, tags))
}
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionCreate, req.UserID).
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionCreate, req.UserID).TID(question.ID).
QID(question.ID, question.UserID))

questionInfo, err = qs.GetQuestion(ctx, question.ID, question.UserID, req.QuestionPermission)
Expand Down Expand Up @@ -560,7 +560,7 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
OriginalObjectID: questionInfo.ID,
ActivityTypeKey: constant.ActQuestionDeleted,
})
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionDelete, req.UserID).
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionDelete, req.UserID).TID(questionInfo.ID).
QID(questionInfo.ID, questionInfo.UserID))
return nil
}
Expand Down Expand Up @@ -953,7 +953,7 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest
RevisionID: revisionID,
OriginalObjectID: question.ID,
})
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionUpdate, req.UserID).
qs.eventQueueService.Send(ctx, schema.NewEvent(constant.EventQuestionUpdate, req.UserID).TID(question.ID).
QID(question.ID, question.UserID))
}

Expand Down
6 changes: 3 additions & 3 deletions internal/service/content/vote_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ func (vs *VoteService) sendEvent(ctx context.Context,
var event *schema.EventMsg
switch objectInfo.ObjectType {
case constant.QuestionObjectType:
event = schema.NewEvent(constant.EventQuestionVote, req.UserID).
event = schema.NewEvent(constant.EventQuestionVote, req.UserID).TID(objectInfo.QuestionID).
QID(objectInfo.QuestionID, objectInfo.ObjectCreatorUserID)
case constant.AnswerObjectType:
event = schema.NewEvent(constant.EventAnswerVote, req.UserID).
event = schema.NewEvent(constant.EventAnswerVote, req.UserID).TID(objectInfo.AnswerID).
AID(objectInfo.AnswerID, objectInfo.ObjectCreatorUserID)
case constant.CommentObjectType:
event = schema.NewEvent(constant.EventCommentVote, req.UserID).
event = schema.NewEvent(constant.EventCommentVote, req.UserID).TID(objectInfo.CommentID).
CID(objectInfo.CommentID, objectInfo.ObjectCreatorUserID)
default:
return
Expand Down
4 changes: 2 additions & 2 deletions internal/service/meta/meta_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (ms *MetaService) AddOrUpdateReaction(ctx context.Context, req *schema.Upda
if !exist {
return nil, myErrors.BadRequest(reason.AnswerNotFound)
}
event = schema.NewEvent(constant.EventAnswerReact, req.UserID).
event = schema.NewEvent(constant.EventAnswerReact, req.UserID).TID(answerInfo.ID).
AID(answerInfo.ID, answerInfo.UserID)
} else if objectType == constant.QuestionObjectType {
questionInfo, exist, err := ms.questionRepo.GetQuestion(ctx, req.ObjectID)
Expand All @@ -114,7 +114,7 @@ func (ms *MetaService) AddOrUpdateReaction(ctx context.Context, req *schema.Upda
if !exist {
return nil, myErrors.BadRequest(reason.QuestionNotFound)
}
event = schema.NewEvent(constant.EventQuestionReact, req.UserID).
event = schema.NewEvent(constant.EventQuestionReact, req.UserID).TID(questionInfo.ID).
QID(questionInfo.ID, questionInfo.UserID)
} else {
return nil, myErrors.BadRequest(reason.ObjectNotFound)
Expand Down
6 changes: 3 additions & 3 deletions internal/service/report/report_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ func (rs *ReportService) sendEvent(ctx context.Context,
var event *schema.EventMsg
switch objectInfo.ObjectType {
case constant.QuestionObjectType:
event = schema.NewEvent(constant.EventQuestionFlag, report.UserID).
event = schema.NewEvent(constant.EventQuestionFlag, report.UserID).TID(objectInfo.QuestionID).
QID(objectInfo.QuestionID, objectInfo.ObjectCreatorUserID)
case constant.AnswerObjectType:
event = schema.NewEvent(constant.EventAnswerFlag, report.UserID).
event = schema.NewEvent(constant.EventAnswerFlag, report.UserID).TID(objectInfo.AnswerID).
AID(objectInfo.AnswerID, objectInfo.ObjectCreatorUserID)
case constant.CommentObjectType:
event = schema.NewEvent(constant.EventCommentFlag, report.UserID).
event = schema.NewEvent(constant.EventCommentFlag, report.UserID).TID(objectInfo.CommentID).
CID(objectInfo.CommentID, objectInfo.ObjectCreatorUserID)
default:
return
Expand Down

0 comments on commit d5d9f38

Please sign in to comment.