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(review): Add review API
LinkinStars6af56c63a034
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.goindex de173cfb..c1eb434b 100644--- a/cmd/wire_gen.go+++ b/cmd/wire_gen.go@@ -33,6 +33,7 @@ import ("github.com/apache/incubator-answer/internal/repo/rank""github.com/apache/incubator-answer/internal/repo/reason""github.com/apache/incubator-answer/internal/repo/report"+ "github.com/apache/incubator-answer/internal/repo/review""github.com/apache/incubator-answer/internal/repo/revision""github.com/apache/incubator-answer/internal/repo/role""github.com/apache/incubator-answer/internal/repo/search_common"@@ -70,6 +71,7 @@ import (reason2 "github.com/apache/incubator-answer/internal/service/reason"report2 "github.com/apache/incubator-answer/internal/service/report""github.com/apache/incubator-answer/internal/service/report_handle"+ review2 "github.com/apache/incubator-answer/internal/service/review""github.com/apache/incubator-answer/internal/service/revision_common"role2 "github.com/apache/incubator-answer/internal/service/role""github.com/apache/incubator-answer/internal/service/search_parser"@@ -191,7 +193,9 @@ 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)- contentRevisionService := content.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService, notificationQueueService, activityQueueService, reportRepo)+ 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)userAdminRepo := user.NewUserAdminRepo(dataData, authRepo)@@ -224,7 +228,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,pluginController := controller_admin.NewPluginController(pluginCommonService)permissionController := controller.NewPermissionController(rankService)userPluginController := controller.NewUserPluginController(pluginCommonService)- answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, userAdminController, reasonController, themeController, siteInfoController, controllerSiteInfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController, permissionController, userPluginController)+ reviewController := controller.NewReviewController(reviewService, rankService, captchaService)+ answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, userAdminController, reasonController, themeController, siteInfoController, controllerSiteInfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController, permissionController, userPluginController, reviewController)swaggerRouter := router.NewSwaggerRouter(swaggerConf)uiRouter := router.NewUIRouter(controllerSiteInfoController, siteInfoCommonService)authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)diff --git a/internal/controller/controller.go b/internal/controller/controller.goindex 0dd8e3ef..42c2c1d3 100644--- a/internal/controller/controller.go+++ b/internal/controller/controller.go@@ -47,4 +47,5 @@ var ProviderSetController = wire.NewSet(NewUserCenterController,NewPermissionController,NewUserPluginController,+ NewReviewController,)diff --git a/internal/controller/review_controller.go b/internal/controller/review_controller.gonew file mode 100644index 00000000..a80b9860--- /dev/null+++ b/internal/controller/review_controller.go@@ -0,0 +1,94 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package controller++import (+ "github.com/apache/incubator-answer/internal/base/handler"+ "github.com/apache/incubator-answer/internal/schema"+ "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/gin-gonic/gin"+)++// ReviewController review controller+type ReviewController struct {+ reviewService *review.ReviewService+ rankService *rank.RankService+ actionService *action.CaptchaService+}++// NewReviewController new controller+func NewReviewController(+ reviewService *review.ReviewService,+ rankService *rank.RankService,+ actionService *action.CaptchaService,+) *ReviewController {+ return &ReviewController{+ reviewService: reviewService,+ rankService: rankService,+ actionService: actionService,+ }+}++// GetUnreviewedPostPage get unreviewed post page+// @Summary get unreviewed post page+// @Description get unreviewed post page+// @Tags Review+// @Accept json+// @Produce json+// @Security ApiKeyAuth+// @Param page query int false "page"+// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetUnreviewedPostPageResp}}+// @Router /answer/api/v1/review/pending/post/page [get]+func (rc *ReviewController) GetUnreviewedPostPage(ctx *gin.Context) {+ req := &schema.GetUnreviewedPostPageReq{}+ if handler.BindAndCheck(ctx, req) {+ return+ }++ // TODO: check permission++ resp, err := rc.reviewService.GetUnreviewedPostPage(ctx, req)+ handler.HandleResponse(ctx, err, resp)+}++// UpdateReview update review+// @Summary update review+// @Description update review+// @Security ApiKeyAuth+// @Tags Review+// @Accept json+// @Produce json+// @Security ApiKeyAuth+// @Param data body schema.UpdateReviewReq true "review"+// @Success 200 {object} handler.RespBody+// @Router /answer/api/v1/review/pending/post [put]+func (rc *ReviewController) UpdateReview(ctx *gin.Context) {+ req := &schema.UpdateReviewReq{}+ if handler.BindAndCheck(ctx, req) {+ return+ }++ // TODO: check permission++ err := rc.reviewService.UpdateReview(ctx, req)+ handler.HandleResponse(ctx, err, nil)+}diff --git a/internal/entity/review_entity.go b/internal/entity/review_entity.gonew file mode 100644index 00000000..f1372d83--- /dev/null+++ b/internal/entity/review_entity.go@@ -0,0 +1,48 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package entity++import "time"++const (+ ReviewStatusPending = 1+ ReviewStatusApproved = 2+ ReviewStatusRejected = 3+)++// Review review+type 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"`+}++// TableName review table name+func (Review) TableName() string {+ return "review"+}diff --git a/internal/repo/provider.go b/internal/repo/provider.goindex 931ccde1..041b6c87 100644--- a/internal/repo/provider.go+++ b/internal/repo/provider.go@@ -38,6 +38,7 @@ import ("github.com/apache/incubator-answer/internal/repo/rank""github.com/apache/incubator-answer/internal/repo/reason""github.com/apache/incubator-answer/internal/repo/report"+ "github.com/apache/incubator-answer/internal/repo/review""github.com/apache/incubator-answer/internal/repo/revision""github.com/apache/incubator-answer/internal/repo/role""github.com/apache/incubator-answer/internal/repo/search_common"@@ -97,4 +98,5 @@ var ProviderSetRepo = wire.NewSet(user_notification_config.NewUserNotificationConfigRepo,limit.NewRateLimitRepo,plugin_config.NewPluginUserConfigRepo,+ review.NewReviewRepo,)diff --git a/internal/repo/review/review_repo.go b/internal/repo/review/review_repo.gonew file mode 100644index 00000000..80a02071--- /dev/null+++ b/internal/repo/review/review_repo.go@@ -0,0 +1,94 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package review++import (+ "context"++ "github.com/apache/incubator-answer/internal/base/data"+ "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/service/review"+ "github.com/segmentfault/pacman/errors"+)++// reviewRepo review repository+type reviewRepo struct {+ data *data.Data+}++// NewReviewRepo new repository+func NewReviewRepo(data *data.Data) review.ReviewRepo {+ return &reviewRepo{+ data: data,+ }+}++// AddReview add review+func (cr *reviewRepo) AddReview(ctx context.Context, review *entity.Review) (err error) {+ _, err = cr.data.DB.Context(ctx).Insert(review)+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}++// UpdateReviewStatus update review status+func (cr *reviewRepo) UpdateReviewStatus(ctx context.Context, reviewID, reviewerUserID string, status int) (err error) {+ _, err = cr.data.DB.Context(ctx).ID(reviewID).Update(&entity.Review{+ ReviewerUserID: reviewerUserID, Status: status})+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}++// GetReview get review one+func (cr *reviewRepo) GetReview(ctx context.Context, reviewID string) (+ review *entity.Review, exist bool, err error) {+ review = &entity.Review{}+ exist, err = cr.data.DB.Context(ctx).ID(reviewID).Get(review)+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}++// GetReviewCount get review count+func (cr *reviewRepo) GetReviewCount(ctx context.Context, status int) (count int64, err error) {+ count, err = cr.data.DB.Context(ctx).Count(&entity.Review{Status: status})+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}++// GetReviewPage get review page+func (cr *reviewRepo) GetReviewPage(ctx context.Context, page, pageSize int, cond *entity.Review) (+ reviewList []*entity.Review, total int64, err error) {+ session := cr.data.DB.Context(ctx).Asc("created_at")+ reviewList = make([]*entity.Review, 0)+ total, err = pager.Help(page, pageSize, &reviewList, cond, session)+ if err != nil {+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ return+}diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.goindex b5786ba0..1ea2be89 100644--- a/internal/router/answer_api_router.go+++ b/internal/router/answer_api_router.go@@ -53,6 +53,7 @@ type AnswerAPIRouter struct {pluginController *controller_admin.PluginControllerpermissionController *controller.PermissionControlleruserPluginController *controller.UserPluginController+ reviewController *controller.ReviewController}func NewAnswerAPIRouter(@@ -82,6 +83,7 @@ func NewAnswerAPIRouter(pluginController *controller_admin.PluginController,permissionController *controller.PermissionController,userPluginController *controller.UserPluginController,+ reviewController *controller.ReviewController,) *AnswerAPIRouter {return &AnswerAPIRouter{langController: langController,@@ -110,6 +112,7 @@ func NewAnswerAPIRouter(pluginController: pluginController,permissionController: permissionController,userPluginController: userPluginController,+ reviewController: reviewController,}}@@ -203,6 +206,10 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {r.GET("/report/unreviewed/post", a.reportController.GetUnreviewedReportPostPage)r.PUT("/report/review", a.reportController.ReviewReport)+ // review+ r.GET("/review/pending/post/page", a.reviewController.GetUnreviewedPostPage)+ r.PUT("/review/pending/post", a.reviewController.UpdateReview)+// voter.POST("/vote/up", a.voteController.VoteUp)r.POST("/vote/down", a.voteController.VoteDown)diff --git a/internal/schema/review_schema.go b/internal/schema/review_schema.gonew file mode 100644index 00000000..c3341ab7--- /dev/null+++ b/internal/schema/review_schema.go@@ -0,0 +1,37 @@+package schema++// UpdateReviewReq update review request+type UpdateReviewReq struct {+ ReviewID string `validate:"required" json:"review_id"`+ Status string `validate:"required,oneof=approve reject" json:"status"`+ UserID string `json:"-"`+}++func (r *UpdateReviewReq) IsApprove() bool {+ return r.Status == "approve"+}++func (r *UpdateReviewReq) IsReject() bool {+ return r.Status == "reject"+}++// GetUnreviewedPostPageReq get review page request+type GetUnreviewedPostPageReq struct {+ Page int `json:"page" form:"page"`+ UserID string `json:"-"`+}++// GetUnreviewedPostPageResp get review page response+type GetUnreviewedPostPageResp struct {+ ReviewID string `json:"review_id"`+ CreatedAt int64 `json:"created_at"`+ ObjectID string `json:"object_id"`+ ObjectType string `json:"object_type" enums:"question,answer,comment"`+ Title string `json:"title"`+ ParsedText string `json:"parsed_text"`+ Tags []*TagResp `json:"tags"`+ AuthorUserID string `json:"author_user_id"`+ UserInfo UserBasicInfo `json:"user_info"`+ SubmitAt int64 `json:"submit_at"`+ SubmitterDisplayName string `json:"submitter_display_name"`+}diff --git a/internal/schema/simple_obj_info_schema.go b/internal/schema/simple_obj_info_schema.goindex 7d2d4057..2c20d63b 100644--- a/internal/schema/simple_obj_info_schema.go+++ b/internal/schema/simple_obj_info_schema.go@@ -34,9 +34,11 @@ type SimpleObjectInfo struct {}type UnreviewedRevisionInfoInfo struct {- ObjectID string `json:"object_id"`- Title string `json:"title"`- Content string `json:"content"`- Html string `json:"html"`- Tags []*TagResp `json:"tags"`+ CreatedAt int64 `json:"created_at"`+ ObjectID string `json:"object_id"`+ ObjectCreatorUserID string `json:"object_creator_user_id"`+ Title string `json:"title"`+ Content string `json:"content"`+ Html string `json:"html"`+ Tags []*TagResp `json:"tags"`}diff --git a/internal/service/content/revision_service.go b/internal/service/content/revision_service.goindex da051965..3a3b8d5c 100644--- a/internal/service/content/revision_service.go+++ b/internal/service/content/revision_service.go@@ -35,6 +35,7 @@ import ("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/report_common"+ "github.com/apache/incubator-answer/internal/service/review""github.com/apache/incubator-answer/internal/service/revision""github.com/apache/incubator-answer/internal/service/tag_common"tagcommon "github.com/apache/incubator-answer/internal/service/tag_common"@@ -60,6 +61,7 @@ type RevisionService struct {notificationQueueService notice_queue.NotificationQueueServiceactivityQueueService activity_queue.ActivityQueueServicereportRepo report_common.ReportRepo+ reviewService *review.ReviewService}func NewRevisionService(@@ -75,6 +77,7 @@ func NewRevisionService(notificationQueueService notice_queue.NotificationQueueService,activityQueueService activity_queue.ActivityQueueService,reportRepo report_common.ReportRepo,+ reviewService *review.ReviewService,) *RevisionService {return &RevisionService{revisionRepo: revisionRepo,@@ -89,6 +92,7 @@ func NewRevisionService(notificationQueueService: notificationQueueService,activityQueueService: activityQueueService,reportRepo: reportRepo,+ reviewService: reviewService,}}@@ -451,12 +455,19 @@ func (rs *RevisionService) CheckCanUpdateRevision(ctx context.Context, req *schefunc (rs *RevisionService) GetReviewingType(ctx context.Context, req *schema.GetReviewingTypeReq) (resp []*schema.GetReviewingTypeResp, err error) {resp = make([]*schema.GetReviewingTypeResp, 0)- // TODO: get queue amount- resp = append(resp, &schema.GetReviewingTypeResp{- Name: string(constant.QueuedPost),- Label: "",- TodoAmount: 0,- })+ // get queue amount+ if req.IsAdmin {+ reportCount, err := rs.reviewService.GetReviewPendingCount(ctx)+ if err != nil {+ log.Errorf("get report count failed: %v", err)+ } else {+ resp = append(resp, &schema.GetReviewingTypeResp{+ Name: string(constant.QueuedPost),+ Label: "Queued post",+ TodoAmount: reportCount,+ })+ }+ }// get flag amountif req.IsAdmin {@@ -466,7 +477,7 @@ func (rs *RevisionService) GetReviewingType(ctx context.Context, req *schema.Get} else {resp = append(resp, &schema.GetReviewingTypeResp{Name: string(constant.FlaggedPost),- Label: "",+ Label: "Flagged post",TodoAmount: reportCount,})}@@ -479,7 +490,7 @@ func (rs *RevisionService) GetReviewingType(ctx context.Context, req *schema.Get} else {resp = append(resp, &schema.GetReviewingTypeResp{Name: string(constant.SuggestedPostEdit),- Label: "",+ Label: "Suggested edits",TodoAmount: countUnreviewedRevision,})}diff --git a/internal/service/object_info/object_info.go b/internal/service/object_info/object_info.goindex 9ef5a892..43f87743 100644--- a/internal/service/object_info/object_info.go+++ b/internal/service/object_info/object_info.go@@ -87,11 +87,13 @@ func (os *ObjService) GetUnreviewedRevisionInfo(ctx context.Context, objectID streturn nil, err}objInfo = &schema.UnreviewedRevisionInfoInfo{- ObjectID: questionInfo.ID,- Title: questionInfo.Title,- Content: questionInfo.OriginalText,- Html: questionInfo.ParsedText,- Tags: tags,+ CreatedAt: questionInfo.CreatedAt.Unix(),+ ObjectID: questionInfo.ID,+ ObjectCreatorUserID: questionInfo.UserID,+ Title: questionInfo.Title,+ Content: questionInfo.OriginalText,+ Html: questionInfo.ParsedText,+ Tags: tags,}case constant.AnswerObjectType:answerInfo, exist, err := os.answerRepo.GetAnswer(ctx, objectID)@@ -113,12 +115,13 @@ func (os *ObjService) GetUnreviewedRevisionInfo(ctx context.Context, objectID stquestionInfo.ID = uid.EnShortID(questionInfo.ID)}objInfo = &schema.UnreviewedRevisionInfoInfo{- ObjectID: answerInfo.ID,- Title: questionInfo.Title,- Content: answerInfo.OriginalText,- Html: answerInfo.ParsedText,+ CreatedAt: answerInfo.CreatedAt.Unix(),+ ObjectID: answerInfo.ID,+ ObjectCreatorUserID: answerInfo.UserID,+ Title: questionInfo.Title,+ Content: answerInfo.OriginalText,+ Html: answerInfo.ParsedText,}-case constant.TagObjectType:tagInfo, exist, err := os.tagRepo.GetTagByID(ctx, objectID, true)if err != nil {@@ -128,10 +131,26 @@ func (os *ObjService) GetUnreviewedRevisionInfo(ctx context.Context, objectID stbreak}objInfo = &schema.UnreviewedRevisionInfoInfo{- ObjectID: tagInfo.ID,- Title: tagInfo.SlugName,- Content: tagInfo.OriginalText,- Html: tagInfo.ParsedText,+ CreatedAt: tagInfo.CreatedAt.Unix(),+ ObjectID: tagInfo.ID,+ Title: tagInfo.SlugName,+ Content: tagInfo.OriginalText,+ Html: tagInfo.ParsedText,+ }+ case constant.CommentObjectType:+ commentInfo, exist, err := os.commentRepo.GetComment(ctx, objectID)+ if err != nil {+ return nil, err+ }+ if !exist {+ break+ }+ objInfo = &schema.UnreviewedRevisionInfoInfo{+ CreatedAt: commentInfo.CreatedAt.Unix(),+ ObjectID: commentInfo.ID,+ ObjectCreatorUserID: commentInfo.UserID,+ Content: commentInfo.OriginalText,+ Html: commentInfo.ParsedText,}}if objInfo == nil {diff --git a/internal/service/provider.go b/internal/service/provider.goindex 5e381bf6..2bdd42b6 100644--- a/internal/service/provider.go+++ b/internal/service/provider.go@@ -46,6 +46,7 @@ import ("github.com/apache/incubator-answer/internal/service/reason""github.com/apache/incubator-answer/internal/service/report""github.com/apache/incubator-answer/internal/service/report_handle"+ "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/search_parser"@@ -113,4 +114,5 @@ var ProviderSetService = wire.NewSet(user_notification_config.NewUserNotificationConfigService,notification.NewExternalNotificationService,notice_queue.NewNewQuestionNotificationQueueService,+ review.NewReviewService,)diff --git a/internal/service/review/review_service.go b/internal/service/review/review_service.gonew file mode 100644index 00000000..7a0f90ae--- /dev/null+++ b/internal/service/review/review_service.go@@ -0,0 +1,141 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one+ * or more contributor license agreements. See the NOTICE file+ * distributed with this work for additional information+ * regarding copyright ownership. The ASF licenses this file+ * to you under the Apache License, Version 2.0 (the+ * "License"); you may not use this file except in compliance+ * with the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing,+ * software distributed under the License is distributed on an+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+ * KIND, either express or implied. See the License for the+ * specific language governing permissions and limitations+ * under the License.+ */++package review++import (+ "context"++ "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"+ "github.com/apache/incubator-answer/internal/service/object_info"+ usercommon "github.com/apache/incubator-answer/internal/service/user_common"+ "github.com/jinzhu/copier"+ "github.com/segmentfault/pacman/errors"+ "github.com/segmentfault/pacman/log"+)++// ReviewRepo review repository+type ReviewRepo interface {+ AddReview(ctx context.Context, review *entity.Review) (err error)+ UpdateReviewStatus(ctx context.Context, reviewID, reviewerUserID string, status int) (err error)+ GetReview(ctx context.Context, reviewID string) (review *entity.Review, exist bool, err error)+ GetReviewCount(ctx context.Context, status int) (count int64, err error)+ GetReviewPage(ctx context.Context, page, pageSize int, cond *entity.Review) (reviewList []*entity.Review, total int64, err error)+}++// ReviewService user service+type ReviewService struct {+ reviewRepo ReviewRepo+ objectInfoService *object_info.ObjService+ userCommon *usercommon.UserCommon+}++// NewReviewService new review service+func NewReviewService(+ reviewRepo ReviewRepo,+ objectInfoService *object_info.ObjService,+ userCommon *usercommon.UserCommon,+) *ReviewService {+ return &ReviewService{+ reviewRepo: reviewRepo,+ objectInfoService: objectInfoService,+ userCommon: userCommon,+ }+}++// AddReview add review+func (cs *ReviewService) AddReview(ctx context.Context, review *entity.Review) (err error) {+ return cs.reviewRepo.AddReview(ctx, review)+}++// UpdateReview update review+func (cs *ReviewService) UpdateReview(ctx context.Context, req *schema.UpdateReviewReq) (err error) {+ review, exist, err := cs.reviewRepo.GetReview(ctx, req.ReviewID)+ if err != nil {+ return err+ }+ if !exist {+ return errors.BadRequest(reason.ObjectNotFound)+ }+ if review.Status != entity.ReviewStatusPending {+ return nil+ }++ if req.IsApprove() {+ // TODO update object status+ } else {+ // TODO update object status+ }++ if req.IsApprove() {+ err = cs.reviewRepo.UpdateReviewStatus(ctx, req.ReviewID, req.UserID, entity.ReviewStatusApproved)+ } else {+ err = cs.reviewRepo.UpdateReviewStatus(ctx, req.ReviewID, req.UserID, entity.ReviewStatusRejected)+ }+ return+}++// GetReviewPendingCount get review pending count+func (cs *ReviewService) GetReviewPendingCount(ctx context.Context) (count int64, err error) {+ return cs.reviewRepo.GetReviewCount(ctx, entity.ReviewStatusPending)+}++// GetUnreviewedPostPage get review page+func (cs *ReviewService) GetUnreviewedPostPage(ctx context.Context, req *schema.GetUnreviewedPostPageReq) (+ pageModel *pager.PageModel, err error) {+ reviewList, total, err := cs.reviewRepo.GetReviewPage(ctx, req.Page, 1, &entity.Review{})+ if err != nil {+ return+ }++ resp := make([]*schema.GetUnreviewedPostPageResp, 0)+ for _, review := range reviewList {+ info, err := cs.objectInfoService.GetUnreviewedRevisionInfo(ctx, review.ObjectID)+ if err != nil {+ log.Errorf("GetUnreviewedRevisionInfo failed, err: %v", err)+ continue+ }++ r := &schema.GetUnreviewedPostPageResp{+ ReviewID: review.ID,+ CreatedAt: info.CreatedAt,+ ObjectID: info.ObjectID,+ ObjectType: info.ObjectID,+ Title: info.Title,+ ParsedText: info.Html,+ Tags: info.Tags,+ AuthorUserID: info.ObjectCreatorUserID,+ SubmitAt: review.CreatedAt.Unix(),+ SubmitterDisplayName: review.SubmitterDisplayName,+ }++ // get user info+ userInfo, exists, e := cs.userCommon.GetUserBasicInfoByID(ctx, info.ObjectCreatorUserID)+ if e != nil {+ log.Errorf("user not found by id: %s, err: %v", info.ObjectCreatorUserID, e)+ }+ if exists {+ _ = copier.Copy(&r.UserInfo, userInfo)+ }+ }+ return pager.NewPageModel(total, resp), nil+}