Skip to content

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(comment): Add comment modification time limit condition

LinkinStar92a82e16733c
diff --git a/i18n/en_US.yaml b/i18n/en_US.yaml
index e0e16aa0..38ee5422 100644
--- a/i18n/en_US.yaml
+++ b/i18n/en_US.yaml
@@ -52,6 +52,8 @@ backend:
other: "Comment are not allowed to edit."
not_found:
other: "Comment not found."
+ cannot_edit_after_deadline:
+ other: "The comment time has been too long to modify."
email:
duplicate:
other: "Email already exists."
diff --git a/internal/base/constant/comment.go b/internal/base/constant/comment.go
new file mode 100644
index 00000000..6a0c7137
--- /dev/null
+++ b/internal/base/constant/comment.go
@@ -0,0 +1,7 @@
+package constant
+
+import "time"
+
+const (
+ CommentEditDeadline = time.Minute * 5
+)
diff --git a/internal/base/reason/reason.go b/internal/base/reason/reason.go
index 9d2880e0..af4b5b71 100644
--- a/internal/base/reason/reason.go
+++ b/internal/base/reason/reason.go
@@ -16,6 +16,7 @@ const (
const (
EmailOrPasswordWrong = "error.object.email_or_password_incorrect"
CommentNotFound = "error.comment.not_found"
+ CommentCannotEditAfterDeadline = "error.comment.cannot_edit_after_deadline"
QuestionNotFound = "error.question.not_found"
QuestionCannotDeleted = "error.question.cannot_deleted"
QuestionCannotClose = "error.question.cannot_close"
diff --git a/internal/controller/comment_controller.go b/internal/controller/comment_controller.go
index eb025139..79ed9681 100644
--- a/internal/controller/comment_controller.go
+++ b/internal/controller/comment_controller.go
@@ -111,6 +111,7 @@ func (cc *CommentController) UpdateComment(ctx *gin.Context) {
}
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+ req.IsAdmin = middleware.GetIsAdminFromContext(ctx)
can, err := cc.rankService.CheckOperationPermission(ctx, req.UserID, permission.CommentEdit, req.CommentID)
if err != nil {
handler.HandleResponse(ctx, err, nil)
diff --git a/internal/schema/comment_schema.go b/internal/schema/comment_schema.go
index 05b3e18a..a6b893ba 100644
--- a/internal/schema/comment_schema.go
+++ b/internal/schema/comment_schema.go
@@ -51,7 +51,8 @@ type UpdateCommentReq struct {
// parsed comment content
ParsedText string `validate:"omitempty" json:"parsed_text"`
// user id
- UserID string `json:"-"`
+ UserID string `json:"-"`
+ IsAdmin 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 92d9a194..b308a808 100644
--- a/internal/service/comment/comment_service.go
+++ b/internal/service/comment/comment_service.go
@@ -134,7 +134,8 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
resp = &schema.GetCommentResp{}
resp.SetFromComment(comment)
- resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID, req.CanEdit, req.CanDelete)
+ resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
+ time.Now(), req.CanEdit, req.CanDelete)
// get reply user info
if len(resp.ReplyUserID) > 0 {
@@ -185,6 +186,19 @@ 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) {
+ old, exist, err := cs.commentCommonRepo.GetComment(ctx, req.CommentID)
+ if err != nil {
+ return
+ }
+ if !exist {
+ return 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)
+ }
+
comment := &entity.Comment{}
_ = copier.Copy(comment, req)
comment.ID = req.CommentID
@@ -198,7 +212,7 @@ func (cs *CommentService) GetComment(ctx context.Context, req *schema.GetComment
return
}
if !exist {
- return nil, errors.BadRequest(reason.UnknownError)
+ return nil, errors.BadRequest(reason.CommentNotFound)
}
resp = &schema.GetCommentResp{
@@ -243,7 +257,8 @@ func (cs *CommentService) GetComment(ctx context.Context, req *schema.GetComment
// check if current user vote this comment
resp.IsVote = cs.checkIsVote(ctx, req.UserID, resp.CommentID)
- resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID, req.CanEdit, req.CanDelete)
+ resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
+ comment.CreatedAt, req.CanEdit, req.CanDelete)
return resp, nil
}
@@ -339,7 +354,7 @@ func (cs *CommentService) convertCommentEntity2Resp(ctx context.Context, req *sc
commentResp.IsVote = cs.checkIsVote(ctx, req.UserID, commentResp.CommentID)
commentResp.MemberActions = permission.GetCommentPermission(ctx,
- req.UserID, commentResp.UserID, req.CanEdit, req.CanDelete)
+ req.UserID, commentResp.UserID, comment.CreatedAt, req.CanEdit, req.CanDelete)
return commentResp, nil
}
diff --git a/internal/service/permission/comment_permission.go b/internal/service/permission/comment_permission.go
index 026b885a..7f8839ae 100644
--- a/internal/service/permission/comment_permission.go
+++ b/internal/service/permission/comment_permission.go
@@ -2,13 +2,15 @@ package permission
import (
"context"
+ "time"
+ "github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/schema"
)
// GetCommentPermission get comment permission
-func GetCommentPermission(ctx context.Context, userID string, creatorUserID string, canEdit, canDelete bool) (
- actions []*schema.PermissionMemberAction) {
+func GetCommentPermission(ctx context.Context, userID string, creatorUserID string,
+ createdAt time.Time, canEdit, canDelete bool) (actions []*schema.PermissionMemberAction) {
actions = make([]*schema.PermissionMemberAction, 0)
if len(userID) > 0 {
actions = append(actions, &schema.PermissionMemberAction{
@@ -17,7 +19,8 @@ func GetCommentPermission(ctx context.Context, userID string, creatorUserID stri
Type: "reason",
})
}
- if canEdit || userID == creatorUserID {
+ deadline := createdAt.Add(constant.CommentEditDeadline)
+ if canEdit || (userID == creatorUserID && time.Now().Before(deadline)) {
actions = append(actions, &schema.PermissionMemberAction{
Action: "edit",
Name: "Edit",