MCPcopy Index your code
hub / github.com/apache/answer / VoteDown

Method VoteDown

internal/controller/vote_controller.go:122–163  ·  view source on GitHub ↗

VoteDown godoc @Summary vote down @Description add vote @Tags Activity @Accept json @Produce json @Security ApiKeyAuth @Param data body schema.VoteReq true "vote" @Success 200 {object} handler.RespBody{data=schema.VoteResp} @Router /answer/api/v1/vote/down [post]

(ctx *gin.Context)

Source from the content-addressed store, hash-verified

120// @Success 200 {object} handler.RespBody{data=schema.VoteResp}
121// @Router /answer/api/v1/vote/down [post]
122func (vc *VoteController) VoteDown(ctx *gin.Context) {
123 req := &schema.VoteReq{}
124 if handler.BindAndCheck(ctx, req) {
125 return
126 }
127 req.ObjectID = uid.DeShortID(req.ObjectID)
128 req.UserID = middleware.GetLoginUserIDFromContext(ctx)
129 isAdmin := middleware.GetUserIsAdminModerator(ctx)
130
131 can, needRank, err := vc.rankService.CheckVotePermission(ctx, req.UserID, req.ObjectID, false)
132 if err != nil {
133 handler.HandleResponse(ctx, err, nil)
134 return
135 }
136 if !can {
137 lang := handler.GetLangByCtx(ctx)
138 msg := translator.TrWithData(lang, reason.NoEnoughRankToOperate, &schema.PermissionTrTplData{Rank: needRank})
139 handler.HandleResponse(ctx, errors.Forbidden(reason.NoEnoughRankToOperate).WithMsg(msg), nil)
140 return
141 }
142
143 if !isAdmin {
144 captchaPass := vc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionVote, req.UserID, req.CaptchaID, req.CaptchaCode)
145 if !captchaPass {
146 errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
147 ErrorField: "captcha_code",
148 ErrorMsg: translator.Tr(handler.GetLangByCtx(ctx), reason.CaptchaVerificationFailed),
149 })
150 handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
151 return
152 }
153 }
154 if !isAdmin {
155 vc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionVote, req.UserID)
156 }
157 resp, err := vc.VoteService.VoteDown(ctx, req)
158 if err != nil {
159 handler.HandleResponse(ctx, err, schema.ErrTypeToast)
160 } else {
161 handler.HandleResponse(ctx, err, resp)
162 }
163}
164
165// UserVotes user votes
166// @Summary get user personal votes

Callers

nothing calls this directly

Calls 11

BindAndCheckFunction · 0.92
DeShortIDFunction · 0.92
GetUserIsAdminModeratorFunction · 0.92
HandleResponseFunction · 0.92
GetLangByCtxFunction · 0.92
TrWithDataFunction · 0.92
TrFunction · 0.92
CheckVotePermissionMethod · 0.80
ActionRecordAddMethod · 0.80

Tested by

no test coverage detected