golang-im聊天
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

198 lines
4.5 KiB

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