Skip to content

Commit cd1c146

Browse files
committed
feat: Add user comment moderation
1 parent 7a8afbb commit cd1c146

File tree

9 files changed

+333
-8
lines changed

9 files changed

+333
-8
lines changed

cmd/wire_gen.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/controller/comment_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
package controller
2121

2222
import (
23+
"net/http"
24+
2325
"github.com/apache/answer/internal/base/handler"
2426
"github.com/apache/answer/internal/base/middleware"
2527
"github.com/apache/answer/internal/base/reason"
@@ -34,7 +36,6 @@ import (
3436
"github.com/apache/answer/pkg/uid"
3537
"github.com/gin-gonic/gin"
3638
"github.com/segmentfault/pacman/errors"
37-
"net/http"
3839
)
3940

4041
// CommentController comment controller
@@ -120,6 +121,9 @@ func (cc *CommentController) AddComment(ctx *gin.Context) {
120121
return
121122
}
122123

124+
req.UserAgent = ctx.GetHeader("User-Agent")
125+
req.IP = ctx.ClientIP()
126+
123127
resp, err := cc.commentService.AddComment(ctx, req)
124128
if !isAdmin || !linkUrlLimitUser {
125129
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionComment, req.UserID)

internal/entity/comment_entity.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,8 @@ func (c *Comment) SetReplyCommentID(str string) {
8787
c.ReplyCommentID = sql.NullInt64{Valid: false}
8888
}
8989
}
90+
91+
// GetMentionUsernameList get mention username list
92+
func (c *Comment) GetMentionUsernameList() []string {
93+
return converter.GetMentionUsernameList(c.OriginalText)
94+
}

internal/repo/comment/comment_repo.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ func (cr *commentRepo) UpdateCommentContent(
9292
return
9393
}
9494

95+
// UpdateCommentStatus update comment status
96+
func (cr *commentRepo) UpdateCommentStatus(ctx context.Context, commentID string, status int) (err error) {
97+
_, err = cr.data.DB.Context(ctx).ID(commentID).Update(&entity.Comment{
98+
Status: status,
99+
})
100+
if err != nil {
101+
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
102+
}
103+
return
104+
}
105+
95106
// GetComment get comment one
96107
func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
97108
comment *entity.Comment, exist bool, err error) {

internal/schema/comment_schema.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type AddCommentReq struct {
5151
CanEdit bool `json:"-"`
5252
// whether user can delete it
5353
CanDelete bool `json:"-"`
54+
55+
IP string `json:"-"`
56+
UserAgent string `json:"-"`
5457
}
5558

5659
func (req *AddCommentReq) Check() (errFields []*validator.FormErrorField, err error) {

internal/service/comment/comment_service.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ package comment
2121

2222
import (
2323
"context"
24+
2425
"github.com/apache/answer/internal/service/event_queue"
26+
"github.com/apache/answer/internal/service/review"
27+
2528
"time"
2629

2730
"github.com/apache/answer/internal/base/constant"
@@ -50,6 +53,7 @@ type CommentRepo interface {
5053
AddComment(ctx context.Context, comment *entity.Comment) (err error)
5154
RemoveComment(ctx context.Context, commentID string) (err error)
5255
UpdateCommentContent(ctx context.Context, commentID string, original string, parsedText string) (err error)
56+
UpdateCommentStatus(ctx context.Context, commentID string, status int) (err error)
5357
GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
5458
GetCommentPage(ctx context.Context, commentQuery *CommentQuery) (
5559
comments []*entity.Comment, total int64, err error)
@@ -88,6 +92,7 @@ type CommentService struct {
8892
externalNotificationQueueService notice_queue.ExternalNotificationQueueService
8993
activityQueueService activity_queue.ActivityQueueService
9094
eventQueueService event_queue.EventQueueService
95+
reviewService *review.ReviewService
9196
}
9297

9398
// NewCommentService new comment service
@@ -103,6 +108,7 @@ func NewCommentService(
103108
externalNotificationQueueService notice_queue.ExternalNotificationQueueService,
104109
activityQueueService activity_queue.ActivityQueueService,
105110
eventQueueService event_queue.EventQueueService,
111+
reviewService *review.ReviewService,
106112
) *CommentService {
107113
return &CommentService{
108114
commentRepo: commentRepo,
@@ -116,6 +122,7 @@ func NewCommentService(
116122
externalNotificationQueueService: externalNotificationQueueService,
117123
activityQueueService: activityQueueService,
118124
eventQueueService: eventQueueService,
125+
reviewService: reviewService,
119126
}
120127
}
121128

@@ -160,14 +167,21 @@ func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddComment
160167
return nil, err
161168
}
162169

170+
comment.Status = cs.reviewService.AddCommentReview(ctx, comment, req.IP, req.UserAgent)
171+
if err := cs.commentRepo.UpdateCommentStatus(ctx, comment.ID, comment.Status); err != nil {
172+
return nil, err
173+
}
174+
163175
resp = &schema.GetCommentResp{}
164176
resp.SetFromComment(comment)
165177
resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
166178
time.Now(), req.CanEdit, req.CanDelete)
167179

168-
commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
169-
if err != nil {
170-
return commentResp, err
180+
if comment.Status == entity.CommentStatusAvailable {
181+
commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
182+
if err != nil {
183+
return commentResp, err
184+
}
171185
}
172186

173187
// get user info

internal/service/comment_common/comment_service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type CommentCommonRepo interface {
3434
GetCommentWithoutStatus(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
3535
GetCommentCount(ctx context.Context) (count int64, err error)
3636
RemoveAllUserComment(ctx context.Context, userID string) (err error)
37+
UpdateCommentStatus(ctx context.Context, commentID string, status int) (err error)
3738
}
3839

3940
// CommentCommonService user service

0 commit comments

Comments
 (0)