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(question): add question review
LinkinStarsc26d83c27a6c
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex c1eb434b..62fc8854 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -171,7 +171,9 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,answerActivityRepo := activity.NewAnswerActivityRepo(dataData, activityRepo, userRankRepo, notificationQueueService)answerActivityService := activity2.NewAnswerActivityService(answerActivityRepo, configService)externalNotificationService := notification.NewExternalNotificationService(dataData, userNotificationConfigRepo, followRepo, emailService, userRepo, externalNotificationQueueService, userExternalLoginRepo, siteInfoCommonService)- questionService := content.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, userRepo, userRoleRelService, revisionService, metaService, collectionCommon, answerActivityService, emailService, notificationQueueService, externalNotificationQueueService, activityQueueService, siteInfoCommonService, externalNotificationService)+ 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)reportHandle := report_handle.NewReportHandle(questionService, answerService, commentService)reportService := report2.NewReportService(reportRepo, objService, userCommon, answerRepo, questionRepo, commentCommonRepo, reportHandle, configService)@@ -193,8 +195,6 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon, tagCommonService)searchService := content.NewSearchService(searchParser, searchRepo)searchController := controller.NewSearchController(searchService, captchaService)- reviewRepo := review.NewReviewRepo(dataData)- reviewService := review2.NewReviewService(reviewRepo, objService, userCommon)contentRevisionService := content.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService, notificationQueueService, activityQueueService, reportRepo, reviewService)revisionController := controller.NewRevisionController(contentRevisionService, rankService)rankController := controller.NewRankController(rankService)diff --git a/internal/controller/review_controller.go b/internal/controller/review_controller.goindex a80b9860..0000a951 100644--- a/internal/controller/review_controller.go+++ b/internal/controller/review_controller.go@@ -25,6 +25,7 @@ import ("github.com/apache/incubator-answer/internal/service/action""github.com/apache/incubator-answer/internal/service/rank""github.com/apache/incubator-answer/internal/service/review"+ "github.com/apache/incubator-answer/plugin""github.com/gin-gonic/gin")@@ -66,6 +67,13 @@ func (rc *ReviewController) GetUnreviewedPostPage(ctx *gin.Context) {// TODO: check permission+ req.ReviewerMapping = make(map[string]string)+ _ = plugin.CallReviewer(func(base plugin.Reviewer) error {+ info := base.Info()+ req.ReviewerMapping[info.SlugName] = info.Name.Translate(ctx)+ return nil+ })+resp, err := rc.reviewService.GetUnreviewedPostPage(ctx, req)handler.HandleResponse(ctx, err, resp)}diff --git a/internal/entity/review_entity.go b/internal/entity/review_entity.goindex f1372d83..199e635d 100644--- a/internal/entity/review_entity.go+++ b/internal/entity/review_entity.go@@ -29,17 +29,16 @@ const (// Review reviewtype Review struct {- ID string `xorm:"not null pk autoincr BIGINT(20) id"`- CreatedAt time.Time `xorm:"created TIMESTAMP created_at"`- UpdatedAt time.Time `xorm:"updated TIMESTAMP updated_at"`- UserID string `xorm:"not null BIGINT(20) user_id"`- ObjectID string `xorm:"not null BIGINT(20) object_id"`- ObjectType int `xorm:"not null default 0 INT(11) object_type"`- ReviewerUserID string `xorm:"not null default 0 BIGINT(20) reviewer_user_id"`- Submitter string `xorm:"not null default '' VARCHAR(100) submitter"`- SubmitterDisplayName string `xorm:"not null default '' VARCHAR(100) submitter_display_name"`- Reason string `xorm:"not null default '' TEXT reason"`- Status int `xorm:"not null default 0 INT(11) status"`+ ID string `xorm:"not null pk autoincr BIGINT(20) id"`+ CreatedAt time.Time `xorm:"created TIMESTAMP created_at"`+ UpdatedAt time.Time `xorm:"updated TIMESTAMP updated_at"`+ UserID string `xorm:"not null BIGINT(20) user_id"`+ ObjectID string `xorm:"not null BIGINT(20) object_id"`+ ObjectType int `xorm:"not null default 0 INT(11) object_type"`+ ReviewerUserID string `xorm:"not null default 0 BIGINT(20) reviewer_user_id"`+ Submitter string `xorm:"not null default '' VARCHAR(100) submitter"`+ Reason string `xorm:"not null default '' TEXT reason"`+ Status int `xorm:"not null default 0 INT(11) status"`}// TableName review table namediff --git a/internal/schema/review_schema.go b/internal/schema/review_schema.goindex c3341ab7..ee03c107 100644--- a/internal/schema/review_schema.go+++ b/internal/schema/review_schema.go@@ -17,8 +17,9 @@ func (r *UpdateReviewReq) IsReject() bool {// GetUnreviewedPostPageReq get review page requesttype GetUnreviewedPostPageReq struct {- Page int `json:"page" form:"page"`- UserID string `json:"-"`+ Page int `json:"page" form:"page"`+ ReviewerMapping map[string]string `json:"-"`+ UserID string `json:"-"`}// GetUnreviewedPostPageResp get review page responsediff --git a/internal/service/content/question_service.go b/internal/service/content/question_service.goindex c2bfe125..aa1eff3b 100644--- a/internal/service/content/question_service.go+++ b/internal/service/content/question_service.go@@ -42,6 +42,7 @@ import ("github.com/apache/incubator-answer/internal/service/notification""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""github.com/apache/incubator-answer/internal/service/siteinfo_common"@@ -77,6 +78,7 @@ type QuestionService struct {activityQueueService activity_queue.ActivityQueueServicesiteInfoService siteinfo_common.SiteInfoCommonServicenewQuestionNotificationService *notification.ExternalNotificationService+ reviewService *review.ReviewService}func NewQuestionService(@@ -96,6 +98,7 @@ func NewQuestionService(activityQueueService activity_queue.ActivityQueueService,siteInfoService siteinfo_common.SiteInfoCommonService,newQuestionNotificationService *notification.ExternalNotificationService,+ reviewService *review.ReviewService,) *QuestionService {return &QuestionService{questionRepo: questionRepo,@@ -114,6 +117,7 @@ func NewQuestionService(activityQueueService: activityQueueService,siteInfoService: siteInfoService,newQuestionNotificationService: newQuestionNotificationService,+ reviewService: reviewService,}}@@ -306,6 +310,9 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Questionquestion.Pin = entity.QuestionUnPinquestion.Show = entity.QuestionShow//question.UpdatedAt = nil+ if qs.reviewService.AddQuestionReview(ctx, question, req.Tags) {+ question.Status = entity.QuestionStatusPending+ }err = qs.questionRepo.AddQuestion(ctx, question)if err != nil {returndiff --git a/internal/service/review/review_service.go b/internal/service/review/review_service.goindex 7a0f90ae..b47097b6 100644--- a/internal/service/review/review_service.go+++ b/internal/service/review/review_service.go@@ -22,12 +22,17 @@ package reviewimport ("context"+ "github.com/apache/incubator-answer/internal/base/constant""github.com/apache/incubator-answer/internal/base/pager""github.com/apache/incubator-answer/internal/base/reason""github.com/apache/incubator-answer/internal/entity""github.com/apache/incubator-answer/internal/schema"+ answercommon "github.com/apache/incubator-answer/internal/service/answer_common""github.com/apache/incubator-answer/internal/service/object_info"+ questioncommon "github.com/apache/incubator-answer/internal/service/question_common"+ "github.com/apache/incubator-answer/internal/service/role"usercommon "github.com/apache/incubator-answer/internal/service/user_common"+ "github.com/apache/incubator-answer/plugin""github.com/jinzhu/copier""github.com/segmentfault/pacman/errors""github.com/segmentfault/pacman/log"@@ -47,6 +52,9 @@ type ReviewService struct {reviewRepo ReviewRepoobjectInfoService *object_info.ObjServiceuserCommon *usercommon.UserCommon+ questionRepo questioncommon.QuestionRepo+ answerRepo answercommon.AnswerRepo+ userRoleService *role.UserRoleRelService}// NewReviewService new review service@@ -54,17 +62,74 @@ func NewReviewService(reviewRepo ReviewRepo,objectInfoService *object_info.ObjService,userCommon *usercommon.UserCommon,+ questionRepo questioncommon.QuestionRepo,+ answerRepo answercommon.AnswerRepo,+ userRoleService *role.UserRoleRelService,) *ReviewService {return &ReviewService{reviewRepo: reviewRepo,objectInfoService: objectInfoService,userCommon: userCommon,+ questionRepo: questionRepo,+ answerRepo: answerRepo,+ userRoleService: userRoleService,}}-// AddReview add review-func (cs *ReviewService) AddReview(ctx context.Context, review *entity.Review) (err error) {- return cs.reviewRepo.AddReview(ctx, review)+// AddQuestionReview add review for question if needed+func (cs *ReviewService) AddQuestionReview(ctx context.Context,+ question *entity.Question, tags []*schema.TagItem) (needReview bool) {+ reviewContent := &plugin.ReviewContent{+ Title: question.Title,+ Content: question.ParsedText,+ }+ for _, tag := range tags {+ reviewContent.Tags = append(reviewContent.Tags, tag.SlugName)+ }+ reviewContent.Author = cs.getReviewContentAuthorInfo(ctx, question.UserID)++ 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)+ }+ }+ return needReview+}++// get review content author info+func (cs *ReviewService) getReviewContentAuthorInfo(ctx context.Context, userID string) (author plugin.ReviewContentAuthor) {+ user, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, userID)+ if err != nil {+ log.Errorf("get user info failed, err: %v", err)+ return+ }+ if !exist {+ log.Errorf("user not found by id: %s", userID)+ return+ }+ author.Rank = user.Rank+ author.ApprovedQuestionAmount, _ = cs.questionRepo.GetUserQuestionCount(ctx, userID)+ author.ApprovedAnswerAmount, _ = cs.answerRepo.GetCountByUserID(ctx, userID)+ author.Role, _ = cs.userRoleService.GetUserRole(ctx, userID)+ return}// UpdateReview update review@@ -80,10 +145,8 @@ func (cs *ReviewService) UpdateReview(ctx context.Context, req *schema.UpdateRevreturn nil}- if req.IsApprove() {- // TODO update object status- } else {- // TODO update object status+ if err = cs.updateObjectStatus(ctx, review, req.IsApprove()); err != nil {+ return err}if req.IsApprove() {@@ -94,6 +157,43 @@ func (cs *ReviewService) UpdateReview(ctx context.Context, req *schema.UpdateRevreturn}+// update object status+func (cs *ReviewService) updateObjectStatus(ctx context.Context, review *entity.Review, isApprove bool) (err error) {+ objectType := constant.ObjectTypeNumberMapping[review.ObjectType]+ switch objectType {+ case constant.QuestionObjectType:+ question, exist, err := cs.questionRepo.GetQuestion(ctx, review.ObjectID)+ if err != nil {+ return err+ }+ if !exist {+ return errors.BadRequest(reason.ObjectNotFound)+ }+ if isApprove {+ question.Status = entity.QuestionStatusAvailable+ } else {+ question.Status = entity.QuestionStatusDeleted+ }+ return cs.questionRepo.UpdateQuestionStatus(ctx, question.ID, question.Status)+ case constant.AnswerObjectType:+ answer, exist, err := cs.answerRepo.GetAnswer(ctx, review.ObjectID)+ if err != nil {+ return err+ }+ if !exist {+ return errors.BadRequest(reason.ObjectNotFound)+ }+ if isApprove {+ answer.Status = entity.AnswerStatusAvailable+ } else {+ answer.Status = entity.AnswerStatusDeleted+ }+ return cs.answerRepo.UpdateAnswerStatus(ctx, answer.ID, answer.Status)+ }+ return++}+// GetReviewPendingCount get review pending countfunc (cs *ReviewService) GetReviewPendingCount(ctx context.Context) (count int64, err error) {return cs.reviewRepo.GetReviewCount(ctx, entity.ReviewStatusPending)@@ -125,7 +225,7 @@ func (cs *ReviewService) GetUnreviewedPostPage(ctx context.Context, req *schema.Tags: info.Tags,AuthorUserID: info.ObjectCreatorUserID,SubmitAt: review.CreatedAt.Unix(),- SubmitterDisplayName: review.SubmitterDisplayName,+ SubmitterDisplayName: req.ReviewerMapping[review.Submitter],}// get user infodiff --git a/plugin/plugin.go b/plugin/plugin.goindex 79e43f68..e492fdc4 100644--- a/plugin/plugin.go+++ b/plugin/plugin.go@@ -21,6 +21,7 @@ package pluginimport ("encoding/json"+"github.com/segmentfault/pacman/i18n""github.com/apache/incubator-answer/internal/base/handler"@@ -88,6 +89,10 @@ func Register(p Base) {if _, ok := p.(Notification); ok {registerNotification(p.(Notification))}++ if _, ok := p.(Reviewer); ok {+ registerReviewer(p.(Reviewer))+ }}type Stack[T Base] struct {diff --git a/plugin/reviewer.go b/plugin/reviewer.gonew file mode 100644index 00000000..dc307ded--- /dev/null+++ b/plugin/reviewer.go@@ -0,0 +1,39 @@+package plugin++type Reviewer interface {+ Base+ Review(content *ReviewContent) (result *ReviewResult)+}++// ReviewContent is a struct that contains the content of a review+type ReviewContent struct {+ Title string+ Content string+ Tags []string+ // The author of the content+ Author ReviewContentAuthor+}++type ReviewContentAuthor struct {+ // The user's reputation+ Rank int+ // The amount of questions and answers that the user has approved+ ApprovedQuestionAmount int64+ ApprovedAnswerAmount int64+ // 1:User 2:Admin 3:Moderator+ Role int+}++// ReviewResult is a struct that contains the result of a review+type ReviewResult struct {+ // If the review is approved+ Approved bool+ // The reason for the result+ Reason string+}++var (+ // CallReviewer is a function that calls all registered parsers+ CallReviewer,+ registerReviewer = MakePlugin[Reviewer](false)+)