Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(answer): add answer review

LinkinStarsd4aea5f34f05
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index 62fc8854..47123b97 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -174,7 +174,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
reviewRepo := review.NewReviewRepo(dataData)
reviewService := review2.NewReviewService(reviewRepo, objService, userCommon, questionRepo, answerRepo, userRoleRelService)
questionService := content.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, userRepo, userRoleRelService, revisionService, metaService, collectionCommon, answerActivityService, emailService, notificationQueueService, externalNotificationQueueService, activityQueueService, siteInfoCommonService, externalNotificationService, reviewService)
- answerService := content.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo, emailService, userRoleRelService, notificationQueueService, externalNotificationQueueService, activityQueueService)
+ answerService := content.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo, emailService, userRoleRelService, notificationQueueService, externalNotificationQueueService, activityQueueService, reviewService)
reportHandle := report_handle.NewReportHandle(questionService, answerService, commentService)
reportService := report2.NewReportService(reportRepo, objService, userCommon, answerRepo, questionRepo, commentCommonRepo, reportHandle, configService)
reportController := controller.NewReportController(reportService, rankService, captchaService)
diff --git a/internal/service/content/answer_service.go b/internal/service/content/answer_service.go
index 9cbcb255..a916c016 100644
--- a/internal/service/content/answer_service.go
+++ b/internal/service/content/answer_service.go
@@ -37,6 +37,7 @@ import (
"github.com/apache/incubator-answer/internal/service/notice_queue"
"github.com/apache/incubator-answer/internal/service/permission"
questioncommon "github.com/apache/incubator-answer/internal/service/question_common"
+ "github.com/apache/incubator-answer/internal/service/review"
"github.com/apache/incubator-answer/internal/service/revision_common"
"github.com/apache/incubator-answer/internal/service/role"
usercommon "github.com/apache/incubator-answer/internal/service/user_common"
@@ -64,6 +65,7 @@ type AnswerService struct {
notificationQueueService notice_queue.NotificationQueueService
externalNotificationQueueService notice_queue.ExternalNotificationQueueService
activityQueueService activity_queue.ActivityQueueService
+ reviewService *review.ReviewService
}
func NewAnswerService(
@@ -82,6 +84,7 @@ func NewAnswerService(
notificationQueueService notice_queue.NotificationQueueService,
externalNotificationQueueService notice_queue.ExternalNotificationQueueService,
activityQueueService activity_queue.ActivityQueueService,
+ reviewService *review.ReviewService,
) *AnswerService {
return &AnswerService{
answerRepo: answerRepo,
@@ -99,6 +102,7 @@ func NewAnswerService(
notificationQueueService: notificationQueueService,
externalNotificationQueueService: externalNotificationQueueService,
activityQueueService: activityQueueService,
+ reviewService: reviewService,
}
}
@@ -223,7 +227,7 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
err = errors.BadRequest(reason.AnswerCannotAddByClosedQuestion)
return "", err
}
- insertData := new(entity.Answer)
+ insertData := &entity.Answer{}
insertData.UserID = req.UserID
insertData.OriginalText = req.Content
insertData.ParsedText = req.HTML
@@ -233,6 +237,9 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
insertData.LastEditUserID = "0"
insertData.Status = entity.AnswerStatusAvailable
//insertData.UpdatedAt = now
+ if as.reviewService.AddAnswerReview(ctx, insertData) {
+ insertData.Status = entity.AnswerStatusPending
+ }
if err = as.answerRepo.AddAnswer(ctx, insertData); err != nil {
return "", err
}
diff --git a/internal/service/review/review_service.go b/internal/service/review/review_service.go
index b47097b6..bead8ee4 100644
--- a/internal/service/review/review_service.go
+++ b/internal/service/review/review_service.go
@@ -87,31 +87,18 @@ func (cs *ReviewService) AddQuestionReview(ctx context.Context,
reviewContent.Tags = append(reviewContent.Tags, tag.SlugName)
}
reviewContent.Author = cs.getReviewContentAuthorInfo(ctx, question.UserID)
+ return cs.callPluginToReview(ctx, question.UserID, question.ID, reviewContent)
+}
- needReview = true
- r := &entity.Review{
- UserID: question.UserID,
- ObjectID: question.ID,
- ObjectType: constant.ObjectTypeStrMapping[constant.QuestionObjectType],
- Status: entity.ReviewStatusPending,
- }
- _ = plugin.CallReviewer(func(reviewer plugin.Reviewer) error {
- result := reviewer.Review(reviewContent)
- if result.Approved {
- needReview = false
- } else {
- r.Reason = result.Reason
- r.Submitter = reviewer.Info().SlugName
- }
- return nil
- })
-
- if needReview {
- if err := cs.reviewRepo.AddReview(ctx, r); err != nil {
- log.Errorf("add review failed, err: %v", err)
- }
+// AddAnswerReview add review for answer if needed
+func (cs *ReviewService) AddAnswerReview(ctx context.Context,
+ answer *entity.Answer) (needReview bool) {
+ reviewContent := &plugin.ReviewContent{
+ ObjectType: constant.AnswerObjectType,
+ Content: answer.ParsedText,
}
- return needReview
+ reviewContent.Author = cs.getReviewContentAuthorInfo(ctx, answer.UserID)
+ return cs.callPluginToReview(ctx, answer.UserID, answer.ID, reviewContent)
}
// get review content author info
@@ -132,6 +119,40 @@ func (cs *ReviewService) getReviewContentAuthorInfo(ctx context.Context, userID
return
}
+// call plugin to review
+func (cs *ReviewService) callPluginToReview(ctx context.Context, userID, objectID string,
+ reviewContent *plugin.ReviewContent) (approved bool) {
+ // As default, no need review
+ approved = true
+
+ r := &entity.Review{
+ UserID: userID,
+ ObjectID: objectID,
+ ObjectType: constant.ObjectTypeStrMapping[reviewContent.ObjectType],
+ Status: entity.ReviewStatusPending,
+ }
+
+ _ = plugin.CallReviewer(func(reviewer plugin.Reviewer) error {
+ // If one of the reviewer plugin return false, then the review is not approved
+ if !approved {
+ return nil
+ }
+ if result := reviewer.Review(reviewContent); !result.Approved {
+ approved = false
+ r.Reason = result.Reason
+ r.Submitter = reviewer.Info().SlugName
+ }
+ return nil
+ })
+
+ if !approved {
+ if err := cs.reviewRepo.AddReview(ctx, r); err != nil {
+ log.Errorf("add review failed, err: %v", err)
+ }
+ }
+ return approved
+}
+
// UpdateReview update review
func (cs *ReviewService) UpdateReview(ctx context.Context, req *schema.UpdateReviewReq) (err error) {
review, exist, err := cs.reviewRepo.GetReview(ctx, req.ReviewID)
diff --git a/plugin/reviewer.go b/plugin/reviewer.go
index dc307ded..b3dc8abb 100644
--- a/plugin/reviewer.go
+++ b/plugin/reviewer.go
@@ -7,9 +7,11 @@ type Reviewer interface {
// ReviewContent is a struct that contains the content of a review
type ReviewContent struct {
- Title string
- Content string
- Tags []string
+ // The type of the content, e.g. question, answer
+ ObjectType string
+ Title string
+ Content string
+ Tags []string
// The author of the content
Author ReviewContentAuthor
}