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.

friend_service.go 7.2 KiB

2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package friend
  2. import (
  3. "context"
  4. svc "gim/internal/business/comm/service"
  5. "gim/internal/business/comm/utils"
  6. "gim/internal/logic/domain/message/repo"
  7. "gim/internal/logic/proxy"
  8. "gim/pkg/gerrors"
  9. "gim/pkg/grpclib"
  10. "gim/pkg/pb"
  11. "gim/pkg/rpc"
  12. "google.golang.org/protobuf/proto"
  13. "time"
  14. )
  15. type friendService struct{}
  16. var FriendService = new(friendService)
  17. // List 获取好友列表
  18. func (s *friendService) List(ctx context.Context, userId int64) ([]*pb.Friend, error) {
  19. friends, err := FriendRepo.List(userId, FriendStatusAgree)
  20. if err != nil {
  21. return nil, err
  22. }
  23. userIds := make(map[int64]int32, len(friends))
  24. for i := range friends {
  25. userIds[friends[i].FriendId] = 0
  26. }
  27. resp, err := rpc.GetBusinessIntClient().GetUsers(ctx, &pb.GetUsersReq{UserIds: userIds})
  28. if err != nil {
  29. return nil, err
  30. }
  31. var infos = make([]*pb.Friend, len(friends))
  32. for i := range friends {
  33. friend := pb.Friend{
  34. UserId: friends[i].FriendId,
  35. Remarks: friends[i].Remarks,
  36. Extra: friends[i].Extra,
  37. }
  38. user, ok := resp.Users[friends[i].FriendId]
  39. if ok {
  40. friend.Nickname = user.Nickname
  41. friend.Sex = user.Sex
  42. friend.AvatarUrl = user.AvatarUrl
  43. friend.UserExtra = user.Extra
  44. }
  45. infos[i] = &friend
  46. }
  47. return infos, nil
  48. }
  49. // DeleteFriend 删除好友
  50. func (*friendService) DeleteFriend(ctx context.Context, userId, friendId int64) error {
  51. err := FriendRepo.Delete(userId, friendId)
  52. if err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. // AddFriend 添加好友
  58. func (*friendService) AddFriend(ctx context.Context, userId, friendId int64, remarks, description string) error {
  59. friend, err := FriendRepo.Get(userId, friendId)
  60. if err != nil {
  61. return err
  62. }
  63. if friend != nil {
  64. if friend.Status == FriendStatusApply {
  65. return nil
  66. }
  67. if friend.Status == FriendStatusAgree {
  68. return gerrors.ErrAlreadyIsFriend
  69. }
  70. }
  71. now := time.Now()
  72. err = FriendRepo.Save(&Friend{
  73. UserId: userId,
  74. FriendId: friendId,
  75. Remarks: remarks,
  76. Status: FriendStatusApply,
  77. CreateTime: now,
  78. UpdateTime: now,
  79. })
  80. if err != nil {
  81. return err
  82. }
  83. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  84. if err != nil {
  85. return err
  86. }
  87. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_ADD_FRIEND, &pb.AddFriendPush{
  88. FriendId: userId,
  89. Nickname: resp.User.Nickname,
  90. AvatarUrl: resp.User.AvatarUrl,
  91. Description: description,
  92. }, true)
  93. if err != nil {
  94. return err
  95. }
  96. //TODO::判断是否需要`自动添加好友`
  97. masterId, _ := grpclib.GetCtxMasterId(ctx)
  98. isAutoAddFriends := svc.SysCfgGet(masterId, "is_auto_add_friends")
  99. //if isAutoAddFriends == "1" && resp.User.IsAutoAddedFriends == 1 {
  100. if isAutoAddFriends == "1" {
  101. respFriend, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: friendId})
  102. if err != nil {
  103. return err
  104. }
  105. utils.FilePutContents("addFriend", utils.SerializeStr(map[string]interface{}{
  106. "is_auto_add_friends": isAutoAddFriends,
  107. "user": resp.User,
  108. }))
  109. if description == "社交人脉圈添加好友" {
  110. return FriendService.AgreeAddFriend(ctx, friendId, userId, description)
  111. } else {
  112. if respFriend.User.IsAutoAddedFriends == 1 {
  113. return FriendService.AgreeAddFriend(ctx, friendId, userId, "系统自动添加")
  114. }
  115. }
  116. }
  117. return nil
  118. }
  119. // AgreeAddFriend 同意添加好友
  120. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  121. friend, err := FriendRepo.Get(friendId, userId)
  122. if err != nil {
  123. return err
  124. }
  125. if friend == nil {
  126. return gerrors.ErrBadRequest
  127. }
  128. if friend.Status == FriendStatusAgree {
  129. return nil
  130. }
  131. friend.Status = FriendStatusAgree
  132. err = FriendRepo.Save(friend)
  133. if err != nil {
  134. return err
  135. }
  136. newFriend, err := FriendRepo.Get(userId, friendId)
  137. if newFriend != nil && newFriend.Status != FriendStatusAgree {
  138. newFriend.Status = FriendStatusAgree
  139. err = FriendRepo.Save(newFriend)
  140. } else {
  141. now := time.Now()
  142. err = FriendRepo.Save(&Friend{
  143. UserId: userId,
  144. FriendId: friendId,
  145. Remarks: remarks,
  146. Status: FriendStatusAgree,
  147. CreateTime: now,
  148. UpdateTime: now,
  149. })
  150. }
  151. if err != nil {
  152. return err
  153. }
  154. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  155. if err != nil {
  156. return err
  157. }
  158. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  159. FriendId: userId,
  160. Nickname: resp.User.Nickname,
  161. AvatarUrl: resp.User.AvatarUrl,
  162. }, true)
  163. if err != nil {
  164. return err
  165. }
  166. return nil
  167. }
  168. // SendToFriend 消息发送至好友
  169. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  170. //TODO::判断是否为好友
  171. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  172. if err != nil {
  173. return 0, err
  174. }
  175. if friend == nil || friend.Status != FriendStatusAgree {
  176. return 0, gerrors.ErrNotIsFriend
  177. }
  178. //utils.FilePutContents("sendToFriend", utils.SerializeStr(map[string]interface{}{
  179. // "send": sender,
  180. // "req": req,
  181. //}))
  182. // 发给发送者
  183. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  184. if err != nil {
  185. return 0, err
  186. }
  187. // 发给接收者
  188. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  189. if err != nil {
  190. return 0, err
  191. }
  192. return seq, nil
  193. }
  194. // RecallMessageSendToFriend 撤回消息发送至好友
  195. func (*friendService) RecallMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  196. //TODO::判断是否为好友
  197. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  198. if err != nil {
  199. return 0, err
  200. }
  201. if friend == nil || friend.Status != FriendStatusAgree {
  202. return 0, gerrors.ErrNotIsFriend
  203. }
  204. utils.FilePutContents("RecallMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  205. "send": sender,
  206. "req": req,
  207. }))
  208. //查询到对应seq的消息
  209. msg := &pb.RECALL{}
  210. err = proto.Unmarshal(req.MessageContent, msg)
  211. if err != nil {
  212. return 0, err
  213. }
  214. message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq)
  215. if err != nil {
  216. return 0, err
  217. }
  218. // 发给发送者
  219. seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime)
  220. if err != nil {
  221. return 0, err
  222. }
  223. // 发给接收者
  224. _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, message.SendTime)
  225. if err != nil {
  226. return 0, err
  227. }
  228. return seq, nil
  229. }
  230. // RedPackageMessageSendToFriend 红包发送至好友
  231. func (*friendService) RedPackageMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  232. //TODO::判断是否为好友
  233. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  234. if err != nil {
  235. return 0, err
  236. }
  237. if friend == nil || friend.Status != FriendStatusAgree {
  238. return 0, gerrors.ErrNotIsFriend
  239. }
  240. utils.FilePutContents("RedPackageMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  241. "send": sender,
  242. "req": req,
  243. }))
  244. // 发给发送者
  245. seq, err := proxy.MessageProxy.SendRedPackageToUser(ctx, sender, sender.SenderId, req)
  246. if err != nil {
  247. return 0, err
  248. }
  249. // 发给接收者
  250. _, err = proxy.MessageProxy.SendRedPackageToUser(ctx, sender, req.ReceiverId, req)
  251. if err != nil {
  252. return 0, err
  253. }
  254. return seq, nil
  255. }