Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion internal/controller/comment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package controller

import (
"net/http"

"github.com/apache/answer/internal/base/handler"
"github.com/apache/answer/internal/base/middleware"
"github.com/apache/answer/internal/base/reason"
Expand All @@ -34,7 +36,6 @@ import (
"github.com/apache/answer/pkg/uid"
"github.com/gin-gonic/gin"
"github.com/segmentfault/pacman/errors"
"net/http"
)

// CommentController comment controller
Expand Down Expand Up @@ -120,6 +121,9 @@ func (cc *CommentController) AddComment(ctx *gin.Context) {
return
}

req.UserAgent = ctx.GetHeader("User-Agent")
req.IP = ctx.ClientIP()

resp, err := cc.commentService.AddComment(ctx, req)
if !isAdmin || !linkUrlLimitUser {
cc.actionService.ActionRecordAdd(ctx, entity.CaptchaActionComment, req.UserID)
Expand Down
5 changes: 5 additions & 0 deletions internal/entity/comment_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ func (c *Comment) SetReplyCommentID(str string) {
c.ReplyCommentID = sql.NullInt64{Valid: false}
}
}

// GetMentionUsernameList get mention username list
func (c *Comment) GetMentionUsernameList() []string {
return converter.GetMentionUsernameList(c.OriginalText)
}
11 changes: 11 additions & 0 deletions internal/repo/comment/comment_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ func (cr *commentRepo) UpdateCommentContent(
return
}

// UpdateCommentStatus update comment status
func (cr *commentRepo) UpdateCommentStatus(ctx context.Context, commentID string, status int) (err error) {
_, err = cr.data.DB.Context(ctx).ID(commentID).Update(&entity.Comment{
Status: status,
})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}

// GetComment get comment one
func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
comment *entity.Comment, exist bool, err error) {
Expand Down
3 changes: 3 additions & 0 deletions internal/schema/comment_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type AddCommentReq struct {
CanEdit bool `json:"-"`
// whether user can delete it
CanDelete bool `json:"-"`

IP string `json:"-"`
UserAgent string `json:"-"`
}

func (req *AddCommentReq) Check() (errFields []*validator.FormErrorField, err error) {
Expand Down
20 changes: 17 additions & 3 deletions internal/service/comment/comment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ package comment

import (
"context"

"github.com/apache/answer/internal/service/event_queue"
"github.com/apache/answer/internal/service/review"

"time"

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

// NewCommentService new comment service
Expand All @@ -103,6 +108,7 @@ func NewCommentService(
externalNotificationQueueService notice_queue.ExternalNotificationQueueService,
activityQueueService activity_queue.ActivityQueueService,
eventQueueService event_queue.EventQueueService,
reviewService *review.ReviewService,
) *CommentService {
return &CommentService{
commentRepo: commentRepo,
Expand All @@ -116,6 +122,7 @@ func NewCommentService(
externalNotificationQueueService: externalNotificationQueueService,
activityQueueService: activityQueueService,
eventQueueService: eventQueueService,
reviewService: reviewService,
}
}

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

comment.Status = cs.reviewService.AddCommentReview(ctx, comment, req.IP, req.UserAgent)
if err := cs.commentRepo.UpdateCommentStatus(ctx, comment.ID, comment.Status); err != nil {
return nil, err
}

resp = &schema.GetCommentResp{}
resp.SetFromComment(comment)
resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID,
time.Now(), req.CanEdit, req.CanDelete)

commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
if err != nil {
return commentResp, err
if comment.Status == entity.CommentStatusAvailable {
commentResp, err := cs.addCommentNotification(ctx, req, resp, comment, objInfo)
if err != nil {
return commentResp, err
}
}

// get user info
Expand Down
1 change: 1 addition & 0 deletions internal/service/comment_common/comment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CommentCommonRepo interface {
GetCommentWithoutStatus(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
GetCommentCount(ctx context.Context) (count int64, err error)
RemoveAllUserComment(ctx context.Context, userID string) (err error)
UpdateCommentStatus(ctx context.Context, commentID string, status int) (err error)
}

// CommentCommonService user service
Expand Down
Loading