feat(review): add ip and user-agent check
This commit is contained in:
@@ -260,7 +260,10 @@ func (ac *AnswerController) Add(ctx *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
answerID, err := ac.answerService.Insert(ctx, req)
|
||||
ua := ctx.GetHeader("User-Agent")
|
||||
ip := ctx.ClientIP()
|
||||
|
||||
answerID, err := ac.answerService.Insert(ctx, req, ip, ua)
|
||||
if err != nil {
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
return
|
||||
|
||||
@@ -434,7 +434,10 @@ func (qc *QuestionController) AddQuestion(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := qc.questionService.AddQuestion(ctx, req)
|
||||
ua := ctx.GetHeader("User-Agent")
|
||||
ip := ctx.ClientIP()
|
||||
|
||||
resp, err := qc.questionService.AddQuestion(ctx, req, ip, ua)
|
||||
if err != nil {
|
||||
errlist, ok := resp.([]*validator.FormErrorField)
|
||||
if ok {
|
||||
@@ -526,7 +529,9 @@ func (qc *QuestionController) AddQuestionByAnswer(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := qc.questionService.AddQuestion(ctx, questionReq)
|
||||
ua := ctx.GetHeader("User-Agent")
|
||||
ip := ctx.ClientIP()
|
||||
resp, err := qc.questionService.AddQuestion(ctx, questionReq, ip, ua)
|
||||
if err != nil {
|
||||
errlist, ok := resp.([]*validator.FormErrorField)
|
||||
if ok {
|
||||
@@ -550,7 +555,7 @@ func (qc *QuestionController) AddQuestionByAnswer(ctx *gin.Context) {
|
||||
answerReq.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||
answerReq.Content = req.AnswerContent
|
||||
answerReq.HTML = req.AnswerHTML
|
||||
answerID, err := qc.answerService.Insert(ctx, answerReq)
|
||||
answerID, err := qc.answerService.Insert(ctx, answerReq, ip, ua)
|
||||
if err != nil {
|
||||
handler.HandleResponse(ctx, err, nil)
|
||||
return
|
||||
|
||||
@@ -216,7 +216,7 @@ func (as *AnswerService) RecoverAnswer(ctx context.Context, req *schema.RecoverA
|
||||
return nil
|
||||
}
|
||||
|
||||
func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (string, error) {
|
||||
func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq, ip, ua string) (string, error) {
|
||||
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -241,7 +241,7 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
|
||||
if err = as.answerRepo.AddAnswer(ctx, insertData); err != nil {
|
||||
return "", err
|
||||
}
|
||||
insertData.Status = as.reviewService.AddAnswerReview(ctx, insertData)
|
||||
insertData.Status = as.reviewService.AddAnswerReview(ctx, insertData, ip, ua)
|
||||
if err := as.answerRepo.UpdateAnswerStatus(ctx, insertData.ID, insertData.Status); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -258,7 +258,7 @@ func (qs *QuestionService) HasNewTag(ctx context.Context, tags []*schema.TagItem
|
||||
}
|
||||
|
||||
// AddQuestion add question
|
||||
func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd) (questionInfo any, err error) {
|
||||
func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.QuestionAdd, ip, ua string) (questionInfo any, err error) {
|
||||
if len(req.Tags) == 0 {
|
||||
errorlist := make([]*validator.FormErrorField, 0)
|
||||
errorlist = append(errorlist, &validator.FormErrorField{
|
||||
@@ -327,7 +327,7 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
question.Status = qs.reviewService.AddQuestionReview(ctx, question, req.Tags)
|
||||
question.Status = qs.reviewService.AddQuestionReview(ctx, question, req.Tags, ip, ua)
|
||||
if err := qs.questionRepo.UpdateQuestionStatus(ctx, question.ID, question.Status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -99,11 +99,13 @@ func NewReviewService(
|
||||
|
||||
// AddQuestionReview add review for question if needed
|
||||
func (cs *ReviewService) AddQuestionReview(ctx context.Context,
|
||||
question *entity.Question, tags []*schema.TagItem) (questionStatus int) {
|
||||
question *entity.Question, tags []*schema.TagItem, ip, ua string) (questionStatus int) {
|
||||
reviewContent := &plugin.ReviewContent{
|
||||
ObjectType: constant.QuestionObjectType,
|
||||
Title: question.Title,
|
||||
Content: question.ParsedText,
|
||||
IP: ip,
|
||||
UserAgent: ua,
|
||||
}
|
||||
for _, tag := range tags {
|
||||
reviewContent.Tags = append(reviewContent.Tags, tag.SlugName)
|
||||
@@ -123,10 +125,12 @@ func (cs *ReviewService) AddQuestionReview(ctx context.Context,
|
||||
|
||||
// AddAnswerReview add review for answer if needed
|
||||
func (cs *ReviewService) AddAnswerReview(ctx context.Context,
|
||||
answer *entity.Answer) (answerStatus int) {
|
||||
answer *entity.Answer, ip, ua string) (answerStatus int) {
|
||||
reviewContent := &plugin.ReviewContent{
|
||||
ObjectType: constant.AnswerObjectType,
|
||||
Content: answer.ParsedText,
|
||||
IP: ip,
|
||||
UserAgent: ua,
|
||||
}
|
||||
reviewContent.Author = cs.getReviewContentAuthorInfo(ctx, answer.UserID)
|
||||
reviewStatus := cs.callPluginToReview(ctx, answer.UserID, answer.ID, reviewContent)
|
||||
|
||||
Reference in New Issue
Block a user