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.
 
 
 
 

212 líneas
5.0 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. if isAutoAddFriends == "1" {
  99. respFriend, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: friendId})
  100. if err != nil {
  101. return err
  102. }
  103. utils.FilePutContents("addFriend", utils.SerializeStr(map[string]interface{}{
  104. "is_auto_add_friends": isAutoAddFriends,
  105. "user": resp.User,
  106. }))
  107. if respFriend.User.IsAutoAddedFriends == 1 {
  108. return FriendService.AgreeAddFriend(ctx, friendId, userId, "系统自动添加")
  109. }
  110. }
  111. return nil
  112. }
  113. // AgreeAddFriend 同意添加好友
  114. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  115. friend, err := FriendRepo.Get(friendId, userId)
  116. if err != nil {
  117. return err
  118. }
  119. if friend == nil {
  120. return gerrors.ErrBadRequest
  121. }
  122. if friend.Status == FriendStatusAgree {
  123. return nil
  124. }
  125. friend.Status = FriendStatusAgree
  126. err = FriendRepo.Save(friend)
  127. if err != nil {
  128. return err
  129. }
  130. newFriend, err := FriendRepo.Get(userId, friendId)
  131. if newFriend != nil && newFriend.Status != FriendStatusAgree {
  132. newFriend.Status = FriendStatusAgree
  133. err = FriendRepo.Save(newFriend)
  134. } else {
  135. now := time.Now()
  136. err = FriendRepo.Save(&Friend{
  137. UserId: userId,
  138. FriendId: friendId,
  139. Remarks: remarks,
  140. Status: FriendStatusAgree,
  141. CreateTime: now,
  142. UpdateTime: now,
  143. })
  144. }
  145. if err != nil {
  146. return err
  147. }
  148. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  149. if err != nil {
  150. return err
  151. }
  152. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  153. FriendId: userId,
  154. Nickname: resp.User.Nickname,
  155. AvatarUrl: resp.User.AvatarUrl,
  156. }, true)
  157. if err != nil {
  158. return err
  159. }
  160. return nil
  161. }
  162. // SendToFriend 消息发送至好友
  163. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  164. //TODO::判断是否为好友
  165. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  166. if err != nil {
  167. return 0, err
  168. }
  169. if friend == nil || friend.Status != FriendStatusAgree {
  170. return 0, gerrors.ErrNotIsFriend
  171. }
  172. //utils.FilePutContents("sendToFriend", utils.SerializeStr(map[string]interface{}{
  173. // "send": sender,
  174. // "req": req,
  175. //}))
  176. // 发给发送者
  177. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  178. if err != nil {
  179. return 0, err
  180. }
  181. // 发给接收者
  182. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  183. if err != nil {
  184. return 0, err
  185. }
  186. return seq, nil
  187. }