2022-09-27 17:59:05 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
|
|
|
"github.com/answerdev/answer/internal/base/middleware"
|
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
2023-07-19 17:30:27 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/translator"
|
|
|
|
"github.com/answerdev/answer/internal/base/validator"
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/schema"
|
2023-07-19 17:30:27 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/action"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/comment"
|
2022-12-02 15:08:18 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/permission"
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/service/rank"
|
2023-03-03 17:54:59 +08:00
|
|
|
"github.com/answerdev/answer/pkg/uid"
|
2022-09-27 17:59:05 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/segmentfault/pacman/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CommentController comment controller
|
|
|
|
type CommentController struct {
|
|
|
|
commentService *comment.CommentService
|
|
|
|
rankService *rank.RankService
|
2023-07-19 17:30:27 +08:00
|
|
|
actionService *action.CaptchaService
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCommentController new controller
|
|
|
|
func NewCommentController(
|
|
|
|
commentService *comment.CommentService,
|
2023-07-19 17:30:27 +08:00
|
|
|
rankService *rank.RankService,
|
|
|
|
actionService *action.CaptchaService,
|
|
|
|
) *CommentController {
|
|
|
|
return &CommentController{
|
|
|
|
commentService: commentService,
|
|
|
|
rankService: rankService,
|
|
|
|
actionService: actionService,
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddComment add comment
|
|
|
|
// @Summary add comment
|
|
|
|
// @Description add comment
|
|
|
|
// @Tags Comment
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.AddCommentReq true "comment"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=schema.GetCommentResp}
|
|
|
|
// @Router /answer/api/v1/comment [post]
|
|
|
|
func (cc *CommentController) AddComment(ctx *gin.Context) {
|
|
|
|
req := &schema.AddCommentReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2023-03-03 17:54:59 +08:00
|
|
|
req.ObjectID = uid.DeShortID(req.ObjectID)
|
2022-09-27 17:59:05 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2023-08-01 15:01:28 +08:00
|
|
|
|
|
|
|
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
|
|
|
permission.CommentAdd,
|
|
|
|
permission.CommentEdit,
|
|
|
|
permission.CommentDelete,
|
|
|
|
permission.LinkUrlLimit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
linkUrlLimitUser := canList[3]
|
2023-07-19 18:42:12 +08:00
|
|
|
isAdmin := middleware.GetUserIsAdminModerator(ctx)
|
2023-08-01 15:01:28 +08:00
|
|
|
if !isAdmin || !linkUrlLimitUser {
|
2023-07-19 18:42:12 +08:00
|
|
|
captchaPass := cc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionComment, req.UserID, req.CaptchaID, req.CaptchaCode)
|
|
|
|
if !captchaPass {
|
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
|
|
|
return
|
|
|
|
}
|
2023-07-19 17:30:27 +08:00
|
|
|
}
|
|
|
|
|
2022-11-25 12:07:56 +08:00
|
|
|
req.CanAdd = canList[0]
|
|
|
|
req.CanEdit = canList[1]
|
|
|
|
req.CanDelete = canList[2]
|
|
|
|
if !req.CanAdd {
|
2022-11-24 18:00:30 +08:00
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cc.commentService.AddComment(ctx, req)
|
2023-08-01 15:01:28 +08:00
|
|
|
if !isAdmin || !linkUrlLimitUser {
|
2023-07-19 18:42:12 +08:00
|
|
|
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionComment, req.UserID)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveComment remove comment
|
|
|
|
// @Summary remove comment
|
|
|
|
// @Description remove comment
|
|
|
|
// @Tags Comment
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.RemoveCommentReq true "comment"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/comment [delete]
|
|
|
|
func (cc *CommentController) RemoveComment(ctx *gin.Context) {
|
|
|
|
req := &schema.RemoveCommentReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2023-07-19 18:42:12 +08:00
|
|
|
isAdmin := middleware.GetUserIsAdminModerator(ctx)
|
|
|
|
if !isAdmin {
|
|
|
|
captchaPass := cc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionDelete, req.UserID, req.CaptchaID, req.CaptchaCode)
|
|
|
|
if !captchaPass {
|
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
|
|
|
return
|
|
|
|
}
|
2023-07-19 17:46:14 +08:00
|
|
|
}
|
2022-12-02 15:08:18 +08:00
|
|
|
can, err := cc.rankService.CheckOperationPermission(ctx, req.UserID, permission.CommentDelete, req.CommentID)
|
2022-11-24 18:00:30 +08:00
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !can {
|
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-24 18:00:30 +08:00
|
|
|
err = cc.commentService.RemoveComment(ctx, req)
|
2023-07-19 18:42:12 +08:00
|
|
|
if !isAdmin {
|
|
|
|
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionDelete, req.UserID)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateComment update comment
|
|
|
|
// @Summary update comment
|
|
|
|
// @Description update comment
|
|
|
|
// @Tags Comment
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security ApiKeyAuth
|
|
|
|
// @Param data body schema.UpdateCommentReq true "comment"
|
|
|
|
// @Success 200 {object} handler.RespBody
|
|
|
|
// @Router /answer/api/v1/comment [put]
|
|
|
|
func (cc *CommentController) UpdateComment(ctx *gin.Context) {
|
|
|
|
req := &schema.UpdateCommentReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2023-09-04 18:09:08 +08:00
|
|
|
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
|
2023-08-01 15:01:28 +08:00
|
|
|
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
|
|
|
permission.CommentEdit,
|
|
|
|
permission.LinkUrlLimit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
2023-09-04 18:09:08 +08:00
|
|
|
req.CanEdit = canList[0] || cc.rankService.CheckOperationObjectOwner(ctx, req.UserID, req.CommentID)
|
|
|
|
linkUrlLimitUser := canList[1]
|
|
|
|
if !req.CanEdit {
|
|
|
|
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !req.IsAdmin || !linkUrlLimitUser {
|
2023-07-19 18:42:12 +08:00
|
|
|
captchaPass := cc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionEdit, req.UserID, req.CaptchaID, req.CaptchaCode)
|
|
|
|
if !captchaPass {
|
|
|
|
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
|
|
|
|
ErrorField: "captcha_code",
|
|
|
|
ErrorMsg: translator.Tr(handler.GetLang(ctx), reason.CaptchaVerificationFailed),
|
|
|
|
})
|
|
|
|
handler.HandleResponse(ctx, errors.BadRequest(reason.CaptchaVerificationFailed), errFields)
|
|
|
|
return
|
|
|
|
}
|
2023-07-19 17:30:27 +08:00
|
|
|
}
|
|
|
|
|
2023-02-24 17:04:22 +08:00
|
|
|
resp, err := cc.commentService.UpdateComment(ctx, req)
|
2023-09-04 18:09:08 +08:00
|
|
|
if !req.IsAdmin || !linkUrlLimitUser {
|
2023-07-19 18:42:12 +08:00
|
|
|
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionEdit, req.UserID)
|
|
|
|
}
|
2023-02-24 17:04:22 +08:00
|
|
|
handler.HandleResponse(ctx, err, resp)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommentWithPage get comment page
|
|
|
|
// @Summary get comment page
|
|
|
|
// @Description get comment page
|
|
|
|
// @Tags Comment
|
|
|
|
// @Produce json
|
|
|
|
// @Param page query int false "page"
|
|
|
|
// @Param page_size query int false "page size"
|
|
|
|
// @Param object_id query string true "object id"
|
|
|
|
// @Param query_cond query string false "query condition" Enums(vote)
|
|
|
|
// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetCommentResp}}
|
|
|
|
// @Router /answer/api/v1/comment/page [get]
|
|
|
|
func (cc *CommentController) GetCommentWithPage(ctx *gin.Context) {
|
|
|
|
req := &schema.GetCommentWithPageReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
2023-03-03 17:54:59 +08:00
|
|
|
req.ObjectID = uid.DeShortID(req.ObjectID)
|
2022-09-27 17:59:05 +08:00
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-11-25 12:07:56 +08:00
|
|
|
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
2022-12-02 15:08:18 +08:00
|
|
|
permission.CommentEdit,
|
|
|
|
permission.CommentDelete,
|
2022-12-09 17:41:23 +08:00
|
|
|
})
|
2022-11-25 12:07:56 +08:00
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.CanEdit = canList[0]
|
|
|
|
req.CanDelete = canList[1]
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
resp, err := cc.commentService.GetCommentWithPage(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommentPersonalWithPage user personal comment list
|
|
|
|
// @Summary user personal comment list
|
|
|
|
// @Description user personal comment list
|
|
|
|
// @Tags Comment
|
|
|
|
// @Produce json
|
|
|
|
// @Param page query int false "page"
|
|
|
|
// @Param page_size query int false "page size"
|
|
|
|
// @Param username query string false "username"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetCommentPersonalWithPageResp}}
|
|
|
|
// @Router /answer/api/v1/personal/comment/page [get]
|
|
|
|
func (cc *CommentController) GetCommentPersonalWithPage(ctx *gin.Context) {
|
|
|
|
req := &schema.GetCommentPersonalWithPageReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
|
|
|
|
|
|
|
resp, err := cc.commentService.GetCommentPersonalWithPage(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetComment godoc
|
|
|
|
// @Summary get comment by id
|
|
|
|
// @Description get comment by id
|
|
|
|
// @Tags Comment
|
|
|
|
// @Produce json
|
|
|
|
// @Param id query string true "id"
|
|
|
|
// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetCommentResp}}
|
|
|
|
// @Router /answer/api/v1/comment [get]
|
|
|
|
func (cc *CommentController) GetComment(ctx *gin.Context) {
|
|
|
|
req := &schema.GetCommentReq{}
|
|
|
|
if handler.BindAndCheck(ctx, req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
2022-11-25 12:07:56 +08:00
|
|
|
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
|
2022-12-02 15:08:18 +08:00
|
|
|
permission.CommentEdit,
|
|
|
|
permission.CommentDelete,
|
2022-12-09 17:41:23 +08:00
|
|
|
})
|
2022-11-25 12:07:56 +08:00
|
|
|
if err != nil {
|
|
|
|
handler.HandleResponse(ctx, err, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.CanEdit = canList[0]
|
|
|
|
req.CanDelete = canList[1]
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
resp, err := cc.commentService.GetComment(ctx, req)
|
|
|
|
handler.HandleResponse(ctx, err, resp)
|
|
|
|
}
|