@@ -29,7 +29,7 @@ import (
2929type 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
226226func (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