golang-im聊天
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

200 行
4.6 KiB

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