getpostcommentlistlogic.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package logic
  2. import (
  3. "context"
  4. "slowwild/internal/constants"
  5. "slowwild/internal/errorx"
  6. "slowwild/internal/model"
  7. "slowwild/internal/svc"
  8. "strconv"
  9. "strings"
  10. "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
  11. "github.com/spf13/cast"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetPostCommentListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetPostCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostCommentListLogic {
  20. return &GetPostCommentListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // 获取评论列表
  27. func (l *GetPostCommentListLogic) GetPostCommentList(in *slowwildserver.GetPostCommentListReq) (*slowwildserver.GetPostCommentListRsp, error) {
  28. userId, _ := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
  29. if in.PostId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
  30. return nil, errorx.ErrInvalidParam
  31. }
  32. // 获取评论列表
  33. comments, err := l.svcCtx.CommentModel.GetCommentList(l.ctx, in.PostId, int(in.Page), int(in.PageSize))
  34. if err != nil {
  35. l.Logger.Errorf("获取评论列表失败: %v", err)
  36. return nil, errorx.ErrCommentQueryFailed
  37. }
  38. // 收集所有用户ID(评论用户和被@用户)
  39. userIds := make(map[int64]bool)
  40. for _, comment := range comments {
  41. userIds[comment.UserId] = true
  42. if comment.WithUserIds != "" {
  43. for _, idStr := range strings.Split(comment.WithUserIds, ",") {
  44. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  45. userIds[userId] = true
  46. }
  47. }
  48. }
  49. }
  50. // 批量获取用户信息
  51. var userIdList []int64
  52. for userId := range userIds {
  53. userIdList = append(userIdList, userId)
  54. }
  55. users, err := l.svcCtx.UserModel.FindByIds(l.ctx, userIdList)
  56. if err != nil {
  57. l.Logger.Errorf("批量获取用户信息失败: %v", err)
  58. return nil, errorx.ErrUserQueryFailed
  59. }
  60. // 构建用户信息map
  61. userMap := make(map[int64]*model.User)
  62. for _, user := range users {
  63. userMap[user.ID] = user
  64. }
  65. // 构建响应
  66. resp := &slowwildserver.GetPostCommentListRsp{
  67. List: make([]*slowwildserver.CommentItem, 0, len(comments)),
  68. }
  69. for _, comment := range comments {
  70. // 获取评论用户信息
  71. user := userMap[comment.UserId]
  72. if user == nil {
  73. continue
  74. }
  75. // 获取@用户信息
  76. var withUsers []*slowwildserver.UserInfo
  77. if comment.WithUserIds != "" {
  78. for _, idStr := range strings.Split(comment.WithUserIds, ",") {
  79. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  80. if atUser := userMap[userId]; atUser != nil {
  81. withUsers = append(withUsers, &slowwildserver.UserInfo{
  82. Id: atUser.ID,
  83. Nickname: atUser.Nickname,
  84. Avatar: atUser.Avatar,
  85. Sex: int32(atUser.Sex),
  86. })
  87. }
  88. }
  89. }
  90. }
  91. // 获取最新的3条回复
  92. replies, err := l.svcCtx.CommentModel.GetLatestReplies(l.ctx, comment.ID, 3)
  93. if err != nil {
  94. l.Logger.Errorf("获取评论回复失败: %v", err)
  95. continue
  96. }
  97. var isUpvoted bool
  98. if userId > 0 {
  99. // 获取评论点赞状态
  100. isUpvoted, err = l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, userId, comment.ID)
  101. if err != nil {
  102. l.Logger.Errorf("获取评论点赞状态失败: %v", err)
  103. continue
  104. }
  105. }
  106. // 构建回复列表
  107. var replyList []*slowwildserver.RepliesItem
  108. for _, reply := range replies {
  109. replyUser := userMap[reply.UserId]
  110. if replyUser == nil {
  111. continue
  112. }
  113. // 获取回复中的@用户信息
  114. var replyWithUsers []*slowwildserver.UserInfo
  115. if reply.WithUserIds != "" {
  116. for _, idStr := range strings.Split(reply.WithUserIds, ",") {
  117. if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
  118. if atUser := userMap[userId]; atUser != nil {
  119. replyWithUsers = append(replyWithUsers, &slowwildserver.UserInfo{
  120. Id: atUser.ID,
  121. Nickname: atUser.Nickname,
  122. Avatar: atUser.Avatar,
  123. Sex: int32(atUser.Sex),
  124. })
  125. }
  126. }
  127. }
  128. }
  129. var isReplyUpvoted bool
  130. if userId > 0 {
  131. // 获取回复点赞状态
  132. isReplyUpvoted, err = l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, userId, reply.ID)
  133. if err != nil {
  134. l.Logger.Errorf("获取回复点赞状态失败: %v", err)
  135. continue
  136. }
  137. }
  138. replyList = append(replyList, &slowwildserver.RepliesItem{
  139. Id: reply.ID,
  140. User: &slowwildserver.UserInfo{
  141. Id: replyUser.ID,
  142. Nickname: replyUser.Nickname,
  143. Avatar: replyUser.Avatar,
  144. Sex: int32(replyUser.Sex),
  145. },
  146. Content: reply.Content,
  147. WithUser: replyWithUsers,
  148. UpvoteCount: reply.ThumbsUpCount,
  149. IpLoc: reply.IpLoc,
  150. CreatedOn: reply.CreatedOn,
  151. IsMine: userId == reply.UserId,
  152. IsLiked: isReplyUpvoted,
  153. })
  154. }
  155. resp.List = append(resp.List, &slowwildserver.CommentItem{
  156. Id: comment.ID,
  157. User: &slowwildserver.UserInfo{
  158. Id: user.ID,
  159. Nickname: user.Nickname,
  160. Avatar: user.Avatar,
  161. Sex: int32(user.Sex),
  162. },
  163. Content: comment.Content,
  164. WithUser: withUsers,
  165. ReplyCount: comment.ReplyCount,
  166. UpvoteCount: comment.ThumbsUpCount,
  167. IpLoc: comment.IpLoc,
  168. CreatedOn: comment.CreatedOn,
  169. IsMine: userId == comment.UserId,
  170. ReplyItem: replyList,
  171. IsLiked: isUpvoted,
  172. })
  173. }
  174. return resp, nil
  175. }