golang-im聊天
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

friend_service.go 3.7 KiB

hace 2 años
hace 2 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. now := time.Now()
  111. err = FriendRepo.Save(&Friend{
  112. UserId: userId,
  113. FriendId: friendId,
  114. Remarks: remarks,
  115. Status: FriendStatusAgree,
  116. CreateTime: now,
  117. UpdateTime: now,
  118. })
  119. if err != nil {
  120. return err
  121. }
  122. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  123. if err != nil {
  124. return err
  125. }
  126. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  127. FriendId: userId,
  128. Nickname: resp.User.Nickname,
  129. AvatarUrl: resp.User.AvatarUrl,
  130. }, true)
  131. if err != nil {
  132. return err
  133. }
  134. return nil
  135. }
  136. // SendToFriend 消息发送至好友
  137. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  138. // 发给发送者
  139. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  140. if err != nil {
  141. return 0, err
  142. }
  143. // 发给接收者
  144. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  145. if err != nil {
  146. return 0, err
  147. }
  148. return seq, nil
  149. }