golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

161 lines
3.5 KiB

  1. package friend
  2. import (
  3. "context"
  4. "gim/internal/logic/proxy"
  5. "gim/pkg/gerrors"
  6. "gim/pkg/pb"
  7. "gim/pkg/rpc"
  8. "time"
  9. )
  10. type friendService struct{}
  11. var FriendService = new(friendService)
  12. // List 获取好友列表
  13. func (s *friendService) List(ctx context.Context, userId int64) ([]*pb.Friend, error) {
  14. friends, err := FriendRepo.List(userId, FriendStatusAgree)
  15. if err != nil {
  16. return nil, err
  17. }
  18. userIds := make(map[int64]int32, len(friends))
  19. for i := range friends {
  20. userIds[friends[i].FriendId] = 0
  21. }
  22. resp, err := rpc.GetBusinessIntClient().GetUsers(ctx, &pb.GetUsersReq{UserIds: userIds})
  23. if err != nil {
  24. return nil, err
  25. }
  26. var infos = make([]*pb.Friend, len(friends))
  27. for i := range friends {
  28. friend := pb.Friend{
  29. UserId: friends[i].FriendId,
  30. Remarks: friends[i].Remarks,
  31. Extra: friends[i].Extra,
  32. }
  33. user, ok := resp.Users[friends[i].FriendId]
  34. if ok {
  35. friend.Nickname = user.Nickname
  36. friend.Sex = user.Sex
  37. friend.AvatarUrl = user.AvatarUrl
  38. friend.UserExtra = user.Extra
  39. }
  40. infos[i] = &friend
  41. }
  42. return infos, nil
  43. }
  44. // AddFriend 添加好友
  45. func (*friendService) AddFriend(ctx context.Context, userId, friendId int64, remarks, description string) error {
  46. friend, err := FriendRepo.Get(userId, friendId)
  47. if err != nil {
  48. return err
  49. }
  50. if friend != nil {
  51. if friend.Status == FriendStatusApply {
  52. return nil
  53. }
  54. if friend.Status == FriendStatusAgree {
  55. return gerrors.ErrAlreadyIsFriend
  56. }
  57. }
  58. now := time.Now()
  59. err = FriendRepo.Save(&Friend{
  60. UserId: userId,
  61. FriendId: friendId,
  62. Remarks: remarks,
  63. Status: FriendStatusApply,
  64. CreateTime: now,
  65. UpdateTime: now,
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  71. if err != nil {
  72. return err
  73. }
  74. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_ADD_FRIEND, &pb.AddFriendPush{
  75. FriendId: userId,
  76. Nickname: resp.User.Nickname,
  77. AvatarUrl: resp.User.AvatarUrl,
  78. Description: description,
  79. }, true)
  80. if err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. // AgreeAddFriend 同意添加好友
  86. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  87. friend, err := FriendRepo.Get(friendId, userId)
  88. if err != nil {
  89. return err
  90. }
  91. if friend == nil {
  92. return gerrors.ErrBadRequest
  93. }
  94. if friend.Status == FriendStatusAgree {
  95. return nil
  96. }
  97. friend.Status = FriendStatusAgree
  98. err = FriendRepo.Save(friend)
  99. if err != nil {
  100. return err
  101. }
  102. now := time.Now()
  103. err = FriendRepo.Save(&Friend{
  104. UserId: userId,
  105. FriendId: friendId,
  106. Remarks: remarks,
  107. Status: FriendStatusAgree,
  108. CreateTime: now,
  109. UpdateTime: now,
  110. })
  111. if err != nil {
  112. return err
  113. }
  114. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  115. if err != nil {
  116. return err
  117. }
  118. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  119. FriendId: userId,
  120. Nickname: resp.User.Nickname,
  121. AvatarUrl: resp.User.AvatarUrl,
  122. }, true)
  123. if err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128. // SendToFriend 消息发送至好友
  129. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  130. // 发给发送者
  131. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  132. if err != nil {
  133. return 0, err
  134. }
  135. // 发给接收者
  136. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  137. if err != nil {
  138. return 0, err
  139. }
  140. return seq, nil
  141. }