Merge branch 'feat/0.5.0/comment' into test

# Conflicts:
#	internal/service/comment/comment_service.go
This commit is contained in:
LinkinStar 2022-11-29 10:27:00 +08:00
commit b446a7c58d
2 changed files with 77 additions and 42 deletions

View File

@ -75,6 +75,8 @@ type GetCommentWithPageReq struct {
PageSize int `validate:"omitempty,min=1" form:"page_size"`
// object id
ObjectID string `validate:"required" form:"object_id"`
// comment id
CommentID string `validate:"omitempty" form:"comment_id"`
// query condition
QueryCond string `validate:"omitempty,oneof=vote" form:"query_cond"`
// user id

View File

@ -255,54 +255,87 @@ func (cs *CommentService) GetCommentWithPage(ctx context.Context, req *schema.Ge
}
resp := make([]*schema.GetCommentResp, 0)
for _, comment := range commentList {
commentResp := &schema.GetCommentResp{
CommentID: comment.ID,
CreatedAt: comment.CreatedAt.Unix(),
UserID: comment.UserID,
ReplyUserID: comment.GetReplyUserID(),
ReplyCommentID: comment.GetReplyCommentID(),
ObjectID: comment.ObjectID,
VoteCount: comment.VoteCount,
OriginalText: comment.OriginalText,
ParsedText: comment.ParsedText,
commentResp, err := cs.convertCommentEntity2Resp(ctx, req.UserID, comment)
if err != nil {
return nil, err
}
// get comment user info
if len(commentResp.UserID) > 0 {
commentUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, commentResp.UserID)
if err != nil {
return nil, err
}
if exist {
commentResp.Username = commentUser.Username
commentResp.UserDisplayName = commentUser.DisplayName
commentResp.UserAvatar = commentUser.Avatar
commentResp.UserStatus = commentUser.Status
}
}
// get reply user info
if len(commentResp.ReplyUserID) > 0 {
replyUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, commentResp.ReplyUserID)
if err != nil {
return nil, err
}
if exist {
commentResp.ReplyUsername = replyUser.Username
commentResp.ReplyUserDisplayName = replyUser.DisplayName
commentResp.ReplyUserStatus = replyUser.Status
}
}
// check if current user vote this comment
commentResp.IsVote = cs.checkIsVote(ctx, req.UserID, commentResp.CommentID)
commentResp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, commentResp.UserID, req.CanEdit, req.CanDelete)
resp = append(resp, commentResp)
}
// if user request the specific comment, add it if not exist.
if len(req.CommentID) > 0 {
commentExist := false
for _, t := range resp {
if t.CommentID == req.CommentID {
commentExist = true
break
}
}
if !commentExist {
comment, exist, err := cs.commentCommonRepo.GetComment(ctx, req.CommentID)
if err != nil {
return nil, err
}
if exist {
commentResp, err := cs.convertCommentEntity2Resp(ctx, req.UserID, comment)
if err != nil {
return nil, err
}
resp = append(resp, commentResp)
}
}
}
return pager.NewPageModel(total, resp), nil
}
func (cs *CommentService) convertCommentEntity2Resp(ctx context.Context, userID string, comment *entity.Comment) (
commentResp *schema.GetCommentResp, err error) {
commentResp = &schema.GetCommentResp{
CommentID: comment.ID,
CreatedAt: comment.CreatedAt.Unix(),
UserID: comment.UserID,
ReplyUserID: comment.GetReplyUserID(),
ReplyCommentID: comment.GetReplyCommentID(),
ObjectID: comment.ObjectID,
VoteCount: comment.VoteCount,
OriginalText: comment.OriginalText,
ParsedText: comment.ParsedText,
}
// get comment user info
if len(commentResp.UserID) > 0 {
commentUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, commentResp.UserID)
if err != nil {
return nil, err
}
if exist {
commentResp.Username = commentUser.Username
commentResp.UserDisplayName = commentUser.DisplayName
commentResp.UserAvatar = commentUser.Avatar
commentResp.UserStatus = commentUser.Status
}
}
// get reply user info
if len(commentResp.ReplyUserID) > 0 {
replyUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, commentResp.ReplyUserID)
if err != nil {
return nil, err
}
if exist {
commentResp.ReplyUsername = replyUser.Username
commentResp.ReplyUserDisplayName = replyUser.DisplayName
commentResp.ReplyUserStatus = replyUser.Status
}
}
// check if current user vote this comment
commentResp.IsVote = cs.checkIsVote(ctx, userID, commentResp.CommentID)
commentResp.MemberActions = permission.GetCommentPermission(userID, commentResp.UserID)
return commentResp, nil
}
func (cs *CommentService) checkCommentWhetherOwner(ctx context.Context, userID, commentID string) error {
// check comment if user self
comment, exist, err := cs.commentCommonRepo.GetComment(ctx, commentID)