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.
 
 
 
 

189 lines
4.2 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. // DeleteFriend 删除好友
  45. func (*friendService) DeleteFriend(ctx context.Context, userId, friendId int64) error {
  46. err := FriendRepo.Delete(userId, friendId)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. // AddFriend 添加好友
  53. func (*friendService) AddFriend(ctx context.Context, userId, friendId int64, remarks, description string) error {
  54. friend, err := FriendRepo.Get(userId, friendId)
  55. if err != nil {
  56. return err
  57. }
  58. if friend != nil {
  59. if friend.Status == FriendStatusApply {
  60. return nil
  61. }
  62. if friend.Status == FriendStatusAgree {
  63. return gerrors.ErrAlreadyIsFriend
  64. }
  65. }
  66. now := time.Now()
  67. err = FriendRepo.Save(&Friend{
  68. UserId: userId,
  69. FriendId: friendId,
  70. Remarks: remarks,
  71. Status: FriendStatusApply,
  72. CreateTime: now,
  73. UpdateTime: now,
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  79. if err != nil {
  80. return err
  81. }
  82. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_ADD_FRIEND, &pb.AddFriendPush{
  83. FriendId: userId,
  84. Nickname: resp.User.Nickname,
  85. AvatarUrl: resp.User.AvatarUrl,
  86. Description: description,
  87. }, true)
  88. if err != nil {
  89. return err
  90. }
  91. return nil
  92. }
  93. // AgreeAddFriend 同意添加好友
  94. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  95. friend, err := FriendRepo.Get(friendId, userId)
  96. if err != nil {
  97. return err
  98. }
  99. if friend == nil {
  100. return gerrors.ErrBadRequest
  101. }
  102. if friend.Status == FriendStatusAgree {
  103. return nil
  104. }
  105. friend.Status = FriendStatusAgree
  106. err = FriendRepo.Save(friend)
  107. if err != nil {
  108. return err
  109. }
  110. newFriend, err := FriendRepo.Get(friendId, userId)
  111. if newFriend != nil && newFriend.Status != FriendStatusAgree {
  112. newFriend.Status = FriendStatusAgree
  113. err = FriendRepo.Save(newFriend)
  114. if err != nil {
  115. return err
  116. }
  117. } else {
  118. now := time.Now()
  119. err = FriendRepo.Save(&Friend{
  120. UserId: userId,
  121. FriendId: friendId,
  122. Remarks: remarks,
  123. Status: FriendStatusAgree,
  124. CreateTime: now,
  125. UpdateTime: now,
  126. })
  127. }
  128. if err != nil {
  129. return err
  130. }
  131. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  132. if err != nil {
  133. return err
  134. }
  135. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  136. FriendId: userId,
  137. Nickname: resp.User.Nickname,
  138. AvatarUrl: resp.User.AvatarUrl,
  139. }, true)
  140. if err != nil {
  141. return err
  142. }
  143. return nil
  144. }
  145. // SendToFriend 消息发送至好友
  146. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  147. //TODO::判断是否为好友
  148. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  149. if err != nil {
  150. return 0, err
  151. }
  152. if friend == nil || friend.Status != FriendStatusAgree {
  153. return 0, gerrors.ErrNotIsFriend
  154. }
  155. // 发给发送者
  156. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  157. if err != nil {
  158. return 0, err
  159. }
  160. // 发给接收者
  161. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  162. if err != nil {
  163. return 0, err
  164. }
  165. return seq, nil
  166. }