golang-im聊天
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

299 rader
7.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/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 if description == "客服自动添加" {
  112. return FriendService.AgreeAddFriend(ctx, friendId, userId, "客服")
  113. } else {
  114. if respFriend.User.IsAutoAddedFriends == 1 {
  115. return FriendService.AgreeAddFriend(ctx, friendId, userId, "系统自动添加")
  116. }
  117. }
  118. }
  119. return nil
  120. }
  121. // AgreeAddFriend 同意添加好友
  122. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  123. friend, err := FriendRepo.Get(friendId, userId)
  124. if err != nil {
  125. return err
  126. }
  127. if friend == nil {
  128. return gerrors.ErrBadRequest
  129. }
  130. if friend.Status == FriendStatusAgree {
  131. return nil
  132. }
  133. friend.Status = FriendStatusAgree
  134. err = FriendRepo.Save(friend)
  135. if err != nil {
  136. return err
  137. }
  138. newFriend, err := FriendRepo.Get(userId, friendId)
  139. if newFriend != nil && newFriend.Status != FriendStatusAgree {
  140. newFriend.Status = FriendStatusAgree
  141. err = FriendRepo.Save(newFriend)
  142. } else {
  143. now := time.Now()
  144. err = FriendRepo.Save(&Friend{
  145. UserId: userId,
  146. FriendId: friendId,
  147. Remarks: remarks,
  148. Status: FriendStatusAgree,
  149. CreateTime: now,
  150. UpdateTime: now,
  151. })
  152. }
  153. if err != nil {
  154. return err
  155. }
  156. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: friendId})
  157. if err != nil {
  158. return err
  159. }
  160. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  161. FriendId: friendId,
  162. Nickname: resp.User.Nickname,
  163. AvatarUrl: resp.User.AvatarUrl,
  164. }, true)
  165. utils.FilePutContents("push_AgreeAddFriend", utils.SerializeStr(map[string]interface{}{
  166. "friend_id": friendId,
  167. "friend_nick_name": resp.User.Nickname,
  168. "friend_avatar_url": resp.User.AvatarUrl,
  169. "err": err,
  170. }))
  171. if err != nil {
  172. return err
  173. }
  174. return nil
  175. }
  176. // SendToFriend 消息发送至好友
  177. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  178. //TODO::判断是否为好友
  179. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  180. if err != nil {
  181. return 0, err
  182. }
  183. if friend == nil || friend.Status != FriendStatusAgree {
  184. return 0, gerrors.ErrNotIsFriend
  185. }
  186. //utils.FilePutContents("sendToFriend", utils.SerializeStr(map[string]interface{}{
  187. // "send": sender,
  188. // "req": req,
  189. //}))
  190. // 发给发送者
  191. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  192. if err != nil {
  193. return 0, err
  194. }
  195. // 发给接收者
  196. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  197. if err != nil {
  198. return 0, err
  199. }
  200. return seq, nil
  201. }
  202. // RecallMessageSendToFriend 撤回消息发送至好友
  203. func (*friendService) RecallMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  204. //TODO::判断是否为好友
  205. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  206. if err != nil {
  207. return 0, err
  208. }
  209. if friend == nil || friend.Status != FriendStatusAgree {
  210. return 0, gerrors.ErrNotIsFriend
  211. }
  212. utils.FilePutContents("RecallMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  213. "send": sender,
  214. "req": req,
  215. }))
  216. //查询到对应seq的消息
  217. msg := &pb.RECALL{}
  218. err = proto.Unmarshal(req.MessageContent, msg)
  219. if err != nil {
  220. return 0, err
  221. }
  222. message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq)
  223. if err != nil {
  224. return 0, err
  225. }
  226. // 发给发送者
  227. seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime)
  228. if err != nil {
  229. return 0, err
  230. }
  231. // 发给接收者
  232. _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, message.SendTime)
  233. if err != nil {
  234. return 0, err
  235. }
  236. return seq, nil
  237. }
  238. // RedPackageMessageSendToFriend 红包发送至好友
  239. func (*friendService) RedPackageMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  240. //TODO::判断是否为好友
  241. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  242. if err != nil {
  243. return 0, err
  244. }
  245. if friend == nil || friend.Status != FriendStatusAgree {
  246. return 0, gerrors.ErrNotIsFriend
  247. }
  248. utils.FilePutContents("RedPackageMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  249. "send": sender,
  250. "req": req,
  251. }))
  252. // 发给发送者
  253. seq, err := proxy.MessageProxy.SendRedPackageToUser(ctx, sender, sender.SenderId, req)
  254. if err != nil {
  255. return 0, err
  256. }
  257. // 发给接收者
  258. _, err = proxy.MessageProxy.SendRedPackageToUser(ctx, sender, req.ReceiverId, req)
  259. if err != nil {
  260. return 0, err
  261. }
  262. return seq, nil
  263. }