From 40690a2a36379ba1b69f17cd13cfa785815b7f57 Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Mon, 29 May 2023 10:38:59 +0800 Subject: [PATCH] fix(notification): add rank activity type translation --- go.sum | 4 -- i18n/en_US.yaml | 15 ++++++ i18n/zh_CN.yaml | 15 ++++++ internal/base/constant/question.go | 5 ++ internal/controller/rank_controller.go | 4 +- internal/schema/rank_schema.go | 4 +- .../service/activity_type/activity_type.go | 34 +++++-------- internal/service/rank/rank_service.go | 51 ++++++++++++------- internal/service/vote_service.go | 38 +++++++------- 9 files changed, 106 insertions(+), 64 deletions(-) create mode 100644 internal/base/constant/question.go diff --git a/go.sum b/go.sum index e51c9462..e26ab91c 100644 --- a/go.sum +++ b/go.sum @@ -625,16 +625,12 @@ github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405 h1:2i github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405/go.mod h1:rIxVzVLKlBwLxO+lC+k/I4HJfRQcemg/f/76Xmmzsec= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= -github.com/segmentfault/pacman v1.0.3 h1:/K8LJHQMiCaCIvC/e8GQITpYTEG6RH4KTLTZjPTghl4= -github.com/segmentfault/pacman v1.0.3/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs= github.com/segmentfault/pacman v1.0.4 h1:6UIXuMHUeYMWe5toflV9SXZQizRny1RczjZJLj9kul0= github.com/segmentfault/pacman v1.0.4/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs= github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs= github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs= github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk= github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY= -github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093245-f9384b820548 h1:R+FH23Qrdp5ECuHXmZy4BvoO/x7m2wZgNeiC46+jqCQ= -github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093245-f9384b820548/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E= github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 h1:OEuW1D7RGDE0CZDr0oGMw9Eiq7fAbD9C4WMrvSixamk= github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E= github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05 h1:jcGZU2juv0L3eFEkuZYV14ESLUlWfGMWnP0mjOfrSZc= diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml index 63702843..59c6a125 100644 --- a/i18n/en_US.yaml +++ b/i18n/en_US.yaml @@ -359,6 +359,8 @@ backend: other: answered modified: other: modified + deleted_title: + other: Deleted question notification: action: update_question: @@ -433,6 +435,19 @@ backend: other: "[{{.SiteName}}] Test Email" body: other: "This is a test email." + action_activity_type: + upvote: + other: upvote + upvoted: + other: upvoted + downvote: + other: downvote + downvoted: + other: downvoted + accept: + other: accept + accepted: + other: accepted # The following fields are used for interface presentation(Front-end) ui: diff --git a/i18n/zh_CN.yaml b/i18n/zh_CN.yaml index 1987e6e0..8bc80aee 100644 --- a/i18n/zh_CN.yaml +++ b/i18n/zh_CN.yaml @@ -344,6 +344,8 @@ backend: other: 回答于 modified: other: 修改于 + deleted_title: + other: 问题已被删除 notification: action: update_question: @@ -418,6 +420,19 @@ backend: other: "[{{.SiteName}}] 测试邮件" body: other: "这是一封测试邮件。" + action_activity_type: + upvote: + other: 点赞 + upvoted: + other: 被赞 + downvote: + other: 点踩 + downvoted: + other: 被踩 + accept: + other: 采纳 + accepted: + other: 被采纳 #The following fields are used for interface presentation(Front-end) ui: how_to_format: diff --git a/internal/base/constant/question.go b/internal/base/constant/question.go new file mode 100644 index 00000000..9f54ea5b --- /dev/null +++ b/internal/base/constant/question.go @@ -0,0 +1,5 @@ +package constant + +const ( + DeletedQuestionTitleTrKey = "question.deleted_title" +) diff --git a/internal/controller/rank_controller.go b/internal/controller/rank_controller.go index b3340f61..4ad38750 100644 --- a/internal/controller/rank_controller.go +++ b/internal/controller/rank_controller.go @@ -27,7 +27,7 @@ func NewRankController( // @Param page query int false "page" // @Param page_size query int false "page size" // @Param username query string false "username" -// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetRankPersonalWithPageResp}} +// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetRankPersonalPageResp}} // @Router /answer/api/v1/personal/rank/page [get] func (cc *RankController) GetRankPersonalWithPage(ctx *gin.Context) { req := &schema.GetRankPersonalWithPageReq{} @@ -37,6 +37,6 @@ func (cc *RankController) GetRankPersonalWithPage(ctx *gin.Context) { req.UserID = middleware.GetLoginUserIDFromContext(ctx) - resp, err := cc.rankService.GetRankPersonalWithPage(ctx, req) + resp, err := cc.rankService.GetRankPersonalPage(ctx, req) handler.HandleResponse(ctx, err, resp) } diff --git a/internal/schema/rank_schema.go b/internal/schema/rank_schema.go index b09eb66b..a5eb20fb 100644 --- a/internal/schema/rank_schema.go +++ b/internal/schema/rank_schema.go @@ -12,8 +12,8 @@ type GetRankPersonalWithPageReq struct { UserID string `json:"-"` } -// GetRankPersonalWithPageResp rank response -type GetRankPersonalWithPageResp struct { +// GetRankPersonalPageResp rank response +type GetRankPersonalPageResp struct { // create time CreatedAt int64 `json:"created_at"` // object id diff --git a/internal/service/activity_type/activity_type.go b/internal/service/activity_type/activity_type.go index 9fafcb5b..93282257 100644 --- a/internal/service/activity_type/activity_type.go +++ b/internal/service/activity_type/activity_type.go @@ -39,27 +39,17 @@ var ( AnswerVotedDown, CommentVoteUp, } - activityTypeFlagMapping = map[string]string{ - QuestionVoteUp: "upvote", - QuestionVoteDown: "downvote", - QuestionVotedUp: "upvoted", - QuestionVotedDown: "downvoted", - AnswerVoteUp: "upvote", - AnswerVoteDown: "downvote", - AnswerVotedUp: "upvoted", - AnswerVotedDown: "downvoted", - AnswerAccepted: "accepted", - AnswerAccept: "accept", - CommentVoteUp: "upvote", + ActivityTypeFlagMapping = map[string]string{ + QuestionVoteUp: "action_activity_type.upvote", + QuestionVoteDown: "action_activity_type.downvote", + QuestionVotedUp: "action_activity_type.upvoted", + QuestionVotedDown: "action_activity_type.downvoted", + AnswerVoteUp: "action_activity_type.upvote", + AnswerVoteDown: "action_activity_type.downvote", + AnswerVotedUp: "action_activity_type.upvoted", + AnswerVotedDown: "action_activity_type.downvoted", + AnswerAccepted: "action_activity_type.accepted", + AnswerAccept: "action_activity_type.accept", + CommentVoteUp: "action_activity_type.upvote", } ) - -func Format(activityTypeID int) string { - return "" - //activityTypeStr := config_common.ID2KeyMapping[activityTypeID] - //activityTypeFlag := activityTypeFlagMapping[activityTypeStr] - //if len(activityTypeFlag) == 0 { - // return "edit" // to edit - //} - //return activityTypeFlag // todo i18n support -} diff --git a/internal/service/rank/rank_service.go b/internal/service/rank/rank_service.go index bb7289f9..307bc591 100644 --- a/internal/service/rank/rank_service.go +++ b/internal/service/rank/rank_service.go @@ -4,8 +4,10 @@ import ( "context" "github.com/answerdev/answer/internal/base/constant" + "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/pager" "github.com/answerdev/answer/internal/base/reason" + "github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/schema" "github.com/answerdev/answer/internal/service/activity_type" @@ -234,8 +236,8 @@ func (rs *RankService) checkUserRank(ctx context.Context, userID string, userRan return true, requireRank } -// GetRankPersonalWithPage get personal comment list page -func (rs *RankService) GetRankPersonalWithPage(ctx context.Context, req *schema.GetRankPersonalWithPageReq) ( +// GetRankPersonalPage get personal comment list page +func (rs *RankService) GetRankPersonalPage(ctx context.Context, req *schema.GetRankPersonalWithPageReq) ( pageModel *pager.PageModel, err error) { if plugin.RankAgentEnabled() { return pager.NewPageModel(0, []string{}), nil @@ -258,32 +260,47 @@ func (rs *RankService) GetRankPersonalWithPage(ctx context.Context, req *schema. if err != nil { return nil, err } - resp := make([]*schema.GetRankPersonalWithPageResp, 0) + + resp := rs.decorateRankPersonalPageResp(ctx, userRankPage) + return pager.NewPageModel(total, resp), nil +} + +func (rs *RankService) decorateRankPersonalPageResp( + ctx context.Context, userRankPage []*entity.Activity) []*schema.GetRankPersonalPageResp { + resp := make([]*schema.GetRankPersonalPageResp, 0) + lang := handler.GetLangByCtx(ctx) + for _, userRankInfo := range userRankPage { if len(userRankInfo.ObjectID) == 0 || userRankInfo.ObjectID == "0" { continue } - commentResp := &schema.GetRankPersonalWithPageResp{ + objInfo, err := rs.objectInfoService.GetInfo(ctx, userRankInfo.ObjectID) + if err != nil { + log.Error(err) + continue + } + + commentResp := &schema.GetRankPersonalPageResp{ CreatedAt: userRankInfo.CreatedAt.Unix(), ObjectID: userRankInfo.ObjectID, Reputation: userRankInfo.Rank, } - objInfo, err := rs.objectInfoService.GetInfo(ctx, userRankInfo.ObjectID) + cfg, err := rs.configService.GetConfigByID(ctx, userRankInfo.ActivityType) if err != nil { log.Error(err) - } else { - commentResp.RankType = activity_type.Format(userRankInfo.ActivityType) - commentResp.ObjectType = objInfo.ObjectType - commentResp.Title = objInfo.Title - commentResp.UrlTitle = htmltext.UrlTitle(objInfo.Title) - commentResp.Content = objInfo.Content - if objInfo.QuestionStatus == entity.QuestionStatusDeleted { - commentResp.Title = "Deleted question" - } - commentResp.QuestionID = objInfo.QuestionID - commentResp.AnswerID = objInfo.AnswerID + continue } + commentResp.RankType = translator.Tr(lang, activity_type.ActivityTypeFlagMapping[cfg.Key]) + commentResp.ObjectType = objInfo.ObjectType + commentResp.Title = objInfo.Title + commentResp.UrlTitle = htmltext.UrlTitle(objInfo.Title) + commentResp.Content = objInfo.Content + if objInfo.QuestionStatus == entity.QuestionStatusDeleted { + commentResp.Title = translator.Tr(lang, constant.DeletedQuestionTitleTrKey) + } + commentResp.QuestionID = objInfo.QuestionID + commentResp.AnswerID = objInfo.AnswerID resp = append(resp, commentResp) } - return pager.NewPageModel(total, resp), nil + return resp } diff --git a/internal/service/vote_service.go b/internal/service/vote_service.go index 1a3096e7..022f9dca 100644 --- a/internal/service/vote_service.go +++ b/internal/service/vote_service.go @@ -3,7 +3,10 @@ package service import ( "context" + "github.com/answerdev/answer/internal/base/constant" + "github.com/answerdev/answer/internal/base/handler" "github.com/answerdev/answer/internal/base/pager" + "github.com/answerdev/answer/internal/base/translator" "github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/service/activity_type" "github.com/answerdev/answer/internal/service/comment_common" @@ -151,23 +154,22 @@ func (vs *VoteService) GetObjectUserID(ctx context.Context, objectID string) (us // ListUserVotes list user's votes func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithPageReq) (model *pager.PageModel, err error) { - var ( - resp []schema.GetVoteWithPageResp - typeKeys = []string{ - "question.vote_up", - "question.vote_down", - "answer.vote_up", - "answer.vote_down", - } - activityTypes []int - ) + typeKeys := []string{ + activity_type.QuestionVoteUp, + activity_type.QuestionVoteDown, + activity_type.AnswerVoteUp, + activity_type.AnswerVoteDown, + } + activityTypes := make([]int, 0) + activityTypeMapping := make(map[int]string, 0) for _, typeKey := range typeKeys { cfg, err := vs.configService.GetConfigByKey(ctx, typeKey) if err != nil { continue } - activityTypes = append(activityTypes, cfg.GetIntValue()) + activityTypes = append(activityTypes, cfg.ID) + activityTypeMapping[cfg.ID] = typeKey } voteList, total, err := vs.voteRepo.ListUserVotes(ctx, req.UserID, req, activityTypes) @@ -175,15 +177,17 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWith return } + lang := handler.GetLangByCtx(ctx) + + resp := make([]*schema.GetVoteWithPageResp, 0) for _, voteInfo := range voteList { - var objInfo *schema.SimpleObjectInfo - objInfo, err = vs.objectService.GetInfo(ctx, voteInfo.ObjectID) + objInfo, err := vs.objectService.GetInfo(ctx, voteInfo.ObjectID) if err != nil { log.Error(err) continue } - item := schema.GetVoteWithPageResp{ + item := &schema.GetVoteWithPageResp{ CreatedAt: voteInfo.CreatedAt.Unix(), ObjectID: objInfo.ObjectID, QuestionID: objInfo.QuestionID, @@ -192,13 +196,13 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWith Title: objInfo.Title, UrlTitle: htmltext.UrlTitle(objInfo.Title), Content: objInfo.Content, - VoteType: activity_type.Format(voteInfo.ActivityType), } + item.VoteType = translator.Tr(lang, + activity_type.ActivityTypeFlagMapping[activityTypeMapping[voteInfo.ActivityType]]) if objInfo.QuestionStatus == entity.QuestionStatusDeleted { - item.Title = "Deleted question" + item.Title = translator.Tr(lang, constant.DeletedQuestionTitleTrKey) } resp = append(resp, item) } - return pager.NewPageModel(total, resp), err }