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: When the user requests the specified comment, if it does not exist in the comment list then query and return it.

LinkinStarf693077ae53d
diff --git a/internal/schema/comment_schema.go b/internal/schema/comment_schema.go
index 53ebf445..de4a44b2 100644
--- a/internal/schema/comment_schema.go
+++ b/internal/schema/comment_schema.go
@@ -69,6 +69,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
diff --git a/internal/service/comment/comment_service.go b/internal/service/comment/comment_service.go
index a7156c76..7d93e4da 100644
--- a/internal/service/comment/comment_service.go
+++ b/internal/service/comment/comment_service.go
@@ -240,52 +240,85 @@ 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
}
+ resp = append(resp, commentResp)
+ }
- // 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
+ // 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
}
}
-
- // get reply user info
- if len(commentResp.ReplyUserID) > 0 {
- replyUser, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, commentResp.ReplyUserID)
+ if !commentExist {
+ comment, exist, err := cs.commentCommonRepo.GetComment(ctx, req.CommentID)
if err != nil {
return nil, err
}
if exist {
- commentResp.ReplyUsername = replyUser.Username
- commentResp.ReplyUserDisplayName = replyUser.DisplayName
- commentResp.ReplyUserStatus = replyUser.Status
+ commentResp, err := cs.convertCommentEntity2Resp(ctx, req.UserID, comment)
+ if err != nil {
+ return nil, err
+ }
+ resp = append(resp, commentResp)
}
}
+ }
+ return pager.NewPageModel(total, resp), nil
+}
- // check if current user vote this comment
- commentResp.IsVote = cs.checkIsVote(ctx, req.UserID, commentResp.CommentID)
+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,
+ }
- commentResp.MemberActions = permission.GetCommentPermission(req.UserID, commentResp.UserID)
- resp = append(resp, commentResp)
+ // 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
+ }
}
- return pager.NewPageModel(total, resp), nil
+
+ // 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 {