From b901f5e3a82306c4eff9a75d5f6b219b4250a257 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:04:22 +0800 Subject: [PATCH 1/2] update comment --- internal/controller/comment_controller.go | 16 ++++++++++++-- internal/schema/comment_schema.go | 6 ++++++ internal/service/comment/comment_service.go | 24 +++++++++++++++++---- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/internal/controller/comment_controller.go b/internal/controller/comment_controller.go index 79ed9681..6b74b754 100644 --- a/internal/controller/comment_controller.go +++ b/internal/controller/comment_controller.go @@ -112,6 +112,18 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) { req.UserID = middleware.GetLoginUserIDFromContext(ctx) req.IsAdmin = middleware.GetIsAdminFromContext(ctx) + canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{ + permission.CommentAdd, + permission.CommentEdit, + permission.CommentDelete, + }) + if err != nil { + handler.HandleResponse(ctx, err, nil) + return + } + req.CanAdd = canList[0] + req.CanEdit = canList[1] + req.CanDelete = canList[2] can, err := cc.rankService.CheckOperationPermission(ctx, req.UserID, permission.CommentEdit, req.CommentID) if err != nil { handler.HandleResponse(ctx, err, nil) @@ -122,8 +134,8 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) { return } - err = cc.commentService.UpdateComment(ctx, req) - handler.HandleResponse(ctx, err, nil) + resp, err := cc.commentService.UpdateComment(ctx, req) + handler.HandleResponse(ctx, err, resp) } // GetCommentWithPage get comment page diff --git a/internal/schema/comment_schema.go b/internal/schema/comment_schema.go index dcc44bd5..5b045a07 100644 --- a/internal/schema/comment_schema.go +++ b/internal/schema/comment_schema.go @@ -53,6 +53,12 @@ type UpdateCommentReq struct { // user id UserID string `json:"-"` IsAdmin bool `json:"-"` + + CanAdd bool `json:"-"` + // whether user can edit it + CanEdit bool `json:"-"` + // whether user can delete it + CanDelete bool `json:"-"` } func (req *UpdateCommentReq) Check() (errFields []*validator.FormErrorField, err error) { diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.go index 18b755ab..6a33ee90 100644 --- a/internal/service/comment/comment_service.go +++ b/internal/service/comment/comment_service.go @@ -209,24 +209,40 @@ func (cs *CommentService) RemoveComment(ctx context.Context, req *schema.RemoveC } // UpdateComment update comment -func (cs *CommentService) UpdateComment(ctx context.Context, req *schema.UpdateCommentReq) (err error) { +func (cs *CommentService) UpdateComment(ctx context.Context, req *schema.UpdateCommentReq) ( + resp *schema.GetCommentResp, err error) { + resp = &schema.GetCommentResp{} + old, exist, err := cs.commentCommonRepo.GetComment(ctx, req.CommentID) if err != nil { return } if !exist { - return errors.BadRequest(reason.CommentNotFound) + return resp, errors.BadRequest(reason.CommentNotFound) } // user can edit the comment that was posted by himself before deadline. if !req.IsAdmin && (time.Now().After(old.CreatedAt.Add(constant.CommentEditDeadline))) { - return errors.BadRequest(reason.CommentCannotEditAfterDeadline) + return resp, errors.BadRequest(reason.CommentCannotEditAfterDeadline) } comment := &entity.Comment{} _ = copier.Copy(comment, req) comment.ID = req.CommentID - return cs.commentRepo.UpdateComment(ctx, comment) + resp.SetFromComment(comment) + resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID, + time.Now(), req.CanEdit, req.CanDelete) + userInfo, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.UserID) + if err != nil { + return nil, err + } + if exist { + resp.Username = userInfo.Username + resp.UserDisplayName = userInfo.DisplayName + resp.UserAvatar = userInfo.Avatar + resp.UserStatus = userInfo.Status + } + return resp, cs.commentRepo.UpdateComment(ctx, comment) } // GetComment get comment one From f80af9ffbebddb2bcae1cc1dd072e1f03a9ba2a1 Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:10:43 +0800 Subject: [PATCH 2/2] update header x-frame-options --- internal/controller/template_controller.go | 1 + internal/service/answer_service.go | 4 ++-- internal/service/question_common/question.go | 4 ++-- internal/service/revision_service.go | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/controller/template_controller.go b/internal/controller/template_controller.go index 894b4930..ccff3b1d 100644 --- a/internal/controller/template_controller.go +++ b/internal/controller/template_controller.go @@ -451,6 +451,7 @@ func (tc *TemplateController) html(ctx *gin.Context, code int, tpl string, siteI if !ok { data["path"] = "" } + ctx.Header("X-Frame-Options", "DENY") ctx.HTML(code, tpl, data) } diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go index 974482c9..bcadbdc6 100644 --- a/internal/service/answer_service.go +++ b/internal/service/answer_service.go @@ -164,7 +164,7 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) ( if err != nil { log.Error("UpdateLastAnswer error", err.Error()) } - err = as.questionCommon.UpdataPostTime(ctx, req.QuestionID) + err = as.questionCommon.UpdatePostTime(ctx, req.QuestionID) if err != nil { return insertData.ID, err } @@ -268,7 +268,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq if err = as.answerRepo.UpdateAnswer(ctx, insertData, []string{"original_text", "parsed_text", "updated_at", "last_edit_user_id"}); err != nil { return "", err } - err = as.questionCommon.UpdataPostTime(ctx, req.QuestionID) + err = as.questionCommon.UpdatePostTime(ctx, req.QuestionID) if err != nil { return insertData.ID, err } diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.go index 23424d2c..79dbdc63 100644 --- a/internal/service/question_common/question.go +++ b/internal/service/question_common/question.go @@ -112,14 +112,14 @@ func (qs *QuestionCommon) UpdateLastAnswer(ctx context.Context, questionID, Answ return qs.questionRepo.UpdateLastAnswer(ctx, question) } -func (qs *QuestionCommon) UpdataPostTime(ctx context.Context, questionID string) error { +func (qs *QuestionCommon) UpdatePostTime(ctx context.Context, questionID string) error { questioninfo := &entity.Question{} now := time.Now() questioninfo.ID = questionID questioninfo.PostUpdateTime = now return qs.questionRepo.UpdateQuestion(ctx, questioninfo, []string{"post_update_time"}) } -func (qs *QuestionCommon) UpdataPostSetTime(ctx context.Context, questionID string, setTime time.Time) error { +func (qs *QuestionCommon) UpdatePostSetTime(ctx context.Context, questionID string, setTime time.Time) error { questioninfo := &entity.Question{} questioninfo.ID = questionID questioninfo.PostUpdateTime = setTime diff --git a/internal/service/revision_service.go b/internal/service/revision_service.go index bb65db32..3e8c3605 100644 --- a/internal/service/revision_service.go +++ b/internal/service/revision_service.go @@ -191,7 +191,7 @@ func (rs *RevisionService) revisionAuditAnswer(ctx context.Context, revisionitem if saveerr != nil { return saveerr } - saveerr = rs.questionCommon.UpdataPostSetTime(ctx, answerinfo.QuestionID, PostUpdateTime) + saveerr = rs.questionCommon.UpdatePostSetTime(ctx, answerinfo.QuestionID, PostUpdateTime) if saveerr != nil { return saveerr }