This is an automated email from the ASF dual-hosted git repository.
kumfo pushed a commit to branch feat/1.3.1/review
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
The following commit(s) were added to refs/heads/feat/1.3.1/review by this push:
new 24832704 feat(review): add ip and user-agent check
24832704 is described below
commit 248327047a292b11fd084592d7aed0806d7eef67
Author: kumfo <[email protected]>
AuthorDate: Wed Mar 27 17:57:08 2024 +0800
feat(review): add ip and user-agent check
---
internal/controller/answer_controller.go | 5 ++++-
internal/controller/question_controller.go | 11 ++++++++---
internal/service/content/answer_service.go | 4 ++--
internal/service/content/question_service.go | 4 ++--
internal/service/review/review_service.go | 8 ++++++--
5 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/internal/controller/answer_controller.go
b/internal/controller/answer_controller.go
index 390aa422..4795e0b3 100644
--- a/internal/controller/answer_controller.go
+++ b/internal/controller/answer_controller.go
@@ -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
diff --git a/internal/controller/question_controller.go
b/internal/controller/question_controller.go
index 072ffd74..7e67766b 100644
--- a/internal/controller/question_controller.go
+++ b/internal/controller/question_controller.go
@@ -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
diff --git a/internal/service/content/answer_service.go
b/internal/service/content/answer_service.go
index 1b113270..eb4d61d3 100644
--- a/internal/service/content/answer_service.go
+++ b/internal/service/content/answer_service.go
@@ -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
}
diff --git a/internal/service/content/question_service.go
b/internal/service/content/question_service.go
index d717a465..81f674d8 100644
--- a/internal/service/content/question_service.go
+++ b/internal/service/content/question_service.go
@@ -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
}
diff --git a/internal/service/review/review_service.go
b/internal/service/review/review_service.go
index 2d928f6a..8db95725 100644
--- a/internal/service/review/review_service.go
+++ b/internal/service/review/review_service.go
@@ -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)