Skip to content

Commit a027376

Browse files
committed
fix(comment): fix admin can't update user's comment.
1 parent 50beae6 commit a027376

5 files changed

Lines changed: 45 additions & 49 deletions

File tree

internal/controller/comment_controller.go

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,23 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) {
157157
}
158158

159159
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
160+
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
160161
canList, err := cc.rankService.CheckOperationPermissions(ctx, req.UserID, []string{
161-
permission.CommentAdd,
162162
permission.CommentEdit,
163-
permission.CommentDelete,
164163
permission.LinkUrlLimit,
165164
})
166165
if err != nil {
167166
handler.HandleResponse(ctx, err, nil)
168167
return
169168
}
170-
linkUrlLimitUser := canList[3]
171-
req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
172-
isAdmin := middleware.GetUserIsAdminModerator(ctx)
173-
if !isAdmin || !linkUrlLimitUser {
169+
req.CanEdit = canList[0] || cc.rankService.CheckOperationObjectOwner(ctx, req.UserID, req.CommentID)
170+
linkUrlLimitUser := canList[1]
171+
if !req.CanEdit {
172+
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
173+
return
174+
}
175+
176+
if !req.IsAdmin || !linkUrlLimitUser {
174177
captchaPass := cc.actionService.ActionRecordVerifyCaptcha(ctx, entity.CaptchaActionEdit, req.UserID, req.CaptchaID, req.CaptchaCode)
175178
if !captchaPass {
176179
errFields := append([]*validator.FormErrorField{}, &validator.FormErrorField{
@@ -182,21 +185,8 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) {
182185
}
183186
}
184187

185-
req.CanAdd = canList[0]
186-
req.CanEdit = canList[1]
187-
req.CanDelete = canList[2]
188-
can, err := cc.rankService.CheckOperationPermission(ctx, req.UserID, permission.CommentEdit, req.CommentID)
189-
if err != nil {
190-
handler.HandleResponse(ctx, err, nil)
191-
return
192-
}
193-
if !can {
194-
handler.HandleResponse(ctx, errors.Forbidden(reason.RankFailToMeetTheCondition), nil)
195-
return
196-
}
197-
198188
resp, err := cc.commentService.UpdateComment(ctx, req)
199-
if !isAdmin || !linkUrlLimitUser {
189+
if !req.IsAdmin || !linkUrlLimitUser {
200190
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionEdit, req.UserID)
201191
}
202192
handler.HandleResponse(ctx, err, resp)

internal/repo/comment/comment_repo.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ func (cr *commentRepo) RemoveComment(ctx context.Context, commentID string) (err
5858
return
5959
}
6060

61-
// UpdateComment update comment
62-
func (cr *commentRepo) UpdateComment(ctx context.Context, comment *entity.Comment) (err error) {
63-
_, err = cr.data.DB.Context(ctx).ID(comment.ID).Where("user_id = ?", comment.UserID).Update(comment)
61+
// UpdateCommentContent update comment
62+
func (cr *commentRepo) UpdateCommentContent(
63+
ctx context.Context, commentID string, originalText string, parsedText string) (err error) {
64+
_, err = cr.data.DB.Context(ctx).ID(commentID).Update(&entity.Comment{
65+
OriginalText: originalText,
66+
ParsedText: parsedText,
67+
})
6468
if err != nil {
6569
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
6670
}
@@ -69,8 +73,7 @@ func (cr *commentRepo) UpdateComment(ctx context.Context, comment *entity.Commen
6973

7074
// GetComment get comment one
7175
func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
72-
comment *entity.Comment, exist bool, err error,
73-
) {
76+
comment *entity.Comment, exist bool, err error) {
7477
comment = &entity.Comment{}
7578
exist, err = cr.data.DB.Context(ctx).ID(commentID).Get(comment)
7679
if err != nil {

internal/repo/repo_test/comment_repo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func Test_commentRepo_UpdateComment(t *testing.T) {
6565
assert.NoError(t, err)
6666

6767
testCommentEntity.ParsedText = "test"
68-
err = commentRepo.UpdateComment(context.TODO(), testCommentEntity)
68+
err = commentRepo.UpdateCommentContent(context.TODO(), testCommentEntity, "", "")
6969
assert.NoError(t, err)
7070

7171
newComment, exist, err := commonCommentRepo.GetComment(context.TODO(), testCommentEntity.ID)

internal/schema/comment_schema.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ type UpdateCommentReq struct {
5858
UserID string `json:"-"`
5959
IsAdmin bool `json:"-"`
6060

61-
CanAdd bool `json:"-"`
6261
// whether user can edit it
6362
CanEdit bool `json:"-"`
63+
6464
// whether user can delete it
65-
CanDelete bool `json:"-"`
6665
CaptchaID string `json:"captcha_id"` // captcha_id
6766
CaptchaCode string `json:"captcha_code"`
6867
}
@@ -72,6 +71,15 @@ func (req *UpdateCommentReq) Check() (errFields []*validator.FormErrorField, err
7271
return nil, nil
7372
}
7473

74+
type UpdateCommentResp struct {
75+
// comment id
76+
CommentID string `json:"comment_id"`
77+
// original comment content
78+
OriginalText string `json:"original_text"`
79+
// parsed comment content
80+
ParsedText string `json:"parsed_text"`
81+
}
82+
7583
// GetCommentListReq get comment list all request
7684
type GetCommentListReq struct {
7785
// user id

internal/service/comment/comment_service.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
type CommentRepo interface {
3030
AddComment(ctx context.Context, comment *entity.Comment) (err error)
3131
RemoveComment(ctx context.Context, commentID string) (err error)
32-
UpdateComment(ctx context.Context, comment *entity.Comment) (err error)
32+
UpdateCommentContent(ctx context.Context, commentID string, original string, parsedText string) (err error)
3333
GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
3434
GetCommentPage(ctx context.Context, commentQuery *CommentQuery) (
3535
comments []*entity.Comment, total int64, err error)
@@ -224,39 +224,34 @@ func (cs *CommentService) RemoveComment(ctx context.Context, req *schema.RemoveC
224224

225225
// UpdateComment update comment
226226
func (cs *CommentService) UpdateComment(ctx context.Context, req *schema.UpdateCommentReq) (
227-
resp *schema.GetCommentResp, err error) {
228-
resp = &schema.GetCommentResp{}
229-
227+
resp *schema.UpdateCommentResp, err error) {
230228
old, exist, err := cs.commentCommonRepo.GetComment(ctx, req.CommentID)
231229
if err != nil {
232-
return
230+
return nil, err
233231
}
234232
if !exist {
235-
return resp, errors.BadRequest(reason.CommentNotFound)
233+
return nil, errors.BadRequest(reason.CommentNotFound)
234+
}
235+
// user can't edit the comment that was posted by others except admin
236+
if !req.IsAdmin && req.UserID != old.UserID {
237+
return nil, errors.BadRequest(reason.CommentNotFound)
236238
}
237239

238240
// user can edit the comment that was posted by himself before deadline.
241+
// admin can edit it at any time
239242
if !req.IsAdmin && (time.Now().After(old.CreatedAt.Add(constant.CommentEditDeadline))) {
240-
return resp, errors.BadRequest(reason.CommentCannotEditAfterDeadline)
243+
return nil, errors.BadRequest(reason.CommentCannotEditAfterDeadline)
241244
}
242245

243-
comment := &entity.Comment{}
244-
_ = copier.Copy(comment, req)
245-
comment.ID = req.CommentID
246-
resp.SetFromComment(comment)
247-
resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
248-
time.Now(), req.CanEdit, req.CanDelete)
249-
userInfo, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.UserID)
250-
if err != nil {
246+
if err = cs.commentRepo.UpdateCommentContent(ctx, old.ID, req.OriginalText, req.ParsedText); err != nil {
251247
return nil, err
252248
}
253-
if exist {
254-
resp.Username = userInfo.Username
255-
resp.UserDisplayName = userInfo.DisplayName
256-
resp.UserAvatar = userInfo.Avatar
257-
resp.UserStatus = userInfo.Status
249+
resp = &schema.UpdateCommentResp{
250+
CommentID: old.ID,
251+
OriginalText: req.OriginalText,
252+
ParsedText: req.ParsedText,
258253
}
259-
return resp, cs.commentRepo.UpdateComment(ctx, comment)
254+
return resp, nil
260255
}
261256

262257
// GetComment get comment one

0 commit comments

Comments
 (0)