| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package logic
- import (
- "context"
- "slowwild/internal/constants"
- "slowwild/internal/errorx"
- "slowwild/internal/model"
- "slowwild/internal/svc"
- "strconv"
- "strings"
- "git.banshen.xyz/huangguangrong/slow_wild_protobuff/slowwild/slowwildserver"
- "github.com/spf13/cast"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetReplyListLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewGetReplyListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetReplyListLogic {
- return &GetReplyListLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- // 获取回复列表
- func (l *GetReplyListLogic) GetReplyList(in *slowwildserver.GetReplyListReq) (*slowwildserver.GetReplyListRsp, error) {
- // 从context中获取userId
- userId, _ := cast.ToInt64E(l.ctx.Value(constants.UserIDKey))
- if in.CommentId <= 0 || in.Page <= 0 || in.PageSize <= 0 {
- return nil, errorx.ErrInvalidParam
- }
- // 获取回复列表
- replies, err := l.svcCtx.CommentModel.GetReplyList(l.ctx, in.CommentId, int(in.Page), int(in.PageSize))
- if err != nil {
- l.Logger.Errorf("获取回复列表失败: %v", err)
- return nil, errorx.ErrCommentQueryFailed
- }
- // 收集所有用户ID(回复用户和被@用户)
- userIds := make(map[int64]bool)
- for _, reply := range replies {
- userIds[reply.UserId] = true
- if reply.WithUserIds != "" {
- for _, idStr := range strings.Split(reply.WithUserIds, ",") {
- if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
- userIds[userId] = true
- }
- }
- }
- }
- // 批量获取用户信息
- var userIdList []int64
- for userId := range userIds {
- userIdList = append(userIdList, userId)
- }
- users, err := l.svcCtx.UserModel.FindByIds(l.ctx, userIdList)
- if err != nil {
- l.Logger.Errorf("批量获取用户信息失败: %v", err)
- return nil, errorx.ErrUserQueryFailed
- }
- // 构建用户信息map
- userMap := make(map[int64]*model.User)
- for _, user := range users {
- userMap[user.ID] = user
- }
- // 构建响应
- resp := &slowwildserver.GetReplyListRsp{
- List: make([]*slowwildserver.RepliesItem, 0, len(replies)),
- }
- for _, reply := range replies {
- // 获取回复用户信息
- user := userMap[reply.UserId]
- if user == nil {
- continue
- }
- // 获取@用户信息
- var withUsers []*slowwildserver.UserInfo
- if reply.WithUserIds != "" {
- for _, idStr := range strings.Split(reply.WithUserIds, ",") {
- if userId, err := strconv.ParseInt(idStr, 10, 64); err == nil {
- if atUser := userMap[userId]; atUser != nil {
- withUsers = append(withUsers, &slowwildserver.UserInfo{
- Id: atUser.ID,
- Nickname: atUser.Nickname,
- Avatar: atUser.Avatar,
- Sex: int32(atUser.Sex),
- })
- }
- }
- }
- }
- var isUpvoted bool
- // 获取回复点赞状态
- if userId > 0 {
- isUpvoted, err = l.svcCtx.CommentModel.IsCommentUpvotedByUser(l.ctx, userId, reply.ID)
- if err != nil {
- l.Logger.Errorf("获取回复点赞状态失败: %v", err)
- continue
- }
- }
- resp.List = append(resp.List, &slowwildserver.RepliesItem{
- Id: reply.ID,
- User: &slowwildserver.UserInfo{
- Id: user.ID,
- Nickname: user.Nickname,
- Avatar: user.Avatar,
- Sex: int32(user.Sex),
- },
- Content: reply.Content,
- WithUser: withUsers,
- UpvoteCount: reply.ThumbsUpCount,
- IpLoc: reply.IpLoc,
- CreatedOn: reply.CreatedOn,
- IsMine: userId == reply.UserId,
- IsLiked: isUpvoted,
- })
- }
- return resp, nil
- }
|