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.9 KiB

1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package friend
  2. import (
  3. "context"
  4. svc "egg-im/internal/business/comm/service"
  5. "egg-im/internal/business/comm/utils"
  6. "egg-im/internal/logic/domain/message/repo"
  7. "egg-im/internal/logic/proxy"
  8. "egg-im/pkg/db"
  9. "egg-im/pkg/gerrors"
  10. "egg-im/pkg/pb"
  11. "egg-im/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. if userId == friendId {
  60. return gerrors.ErrNotAllowAddSelf
  61. }
  62. isAutoAddFriends := svc.SysCfgGet("is_auto_add_friends")
  63. friend, err := FriendRepo.Get(userId, friendId)
  64. if err != nil {
  65. return err
  66. }
  67. if friend != nil {
  68. if friend.Status == FriendStatusApply {
  69. if isAutoAddFriends != "1" {
  70. return nil
  71. }
  72. //TODO::防止第一次加好友失败
  73. db.DB.Where("user_id = ? and friend_id = ?", userId, friendId).Delete(&friend)
  74. }
  75. if friend.Status == FriendStatusAgree {
  76. return gerrors.ErrAlreadyIsFriend
  77. }
  78. }
  79. now := time.Now()
  80. err = FriendRepo.Save(&Friend{
  81. UserId: userId,
  82. FriendId: friendId,
  83. Remarks: remarks,
  84. Status: FriendStatusApply,
  85. CreateTime: now,
  86. UpdateTime: now,
  87. })
  88. if err != nil {
  89. return err
  90. }
  91. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  92. if err != nil {
  93. return err
  94. }
  95. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_ADD_FRIEND, &pb.AddFriendPush{
  96. FriendId: userId,
  97. Nickname: resp.User.Nickname,
  98. AvatarUrl: resp.User.AvatarUrl,
  99. Description: description,
  100. }, true)
  101. if err != nil {
  102. return err
  103. }
  104. //TODO::判断是否需要`自动添加好友`
  105. //if isAutoAddFriends == "1" && resp.User.IsAutoAddedFriends == 1 {
  106. if isAutoAddFriends == "1" {
  107. respFriend, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: friendId})
  108. if err != nil {
  109. return err
  110. }
  111. utils.FilePutContents("addFriend", utils.SerializeStr(map[string]interface{}{
  112. "is_auto_add_friends": isAutoAddFriends,
  113. "user": resp.User,
  114. }))
  115. if description == "社交人脉圈添加好友" {
  116. return FriendService.AgreeAddFriend(ctx, friendId, userId, description)
  117. } else if description == "客服自动添加" {
  118. return FriendService.AgreeAddFriend(ctx, friendId, userId, "客服")
  119. } else {
  120. if respFriend.User.IsAutoAddedFriends == 1 {
  121. return FriendService.AgreeAddFriend(ctx, friendId, userId, "系统自动添加")
  122. }
  123. }
  124. }
  125. return nil
  126. }
  127. // AgreeAddFriend 同意添加好友
  128. func (*friendService) AgreeAddFriend(ctx context.Context, userId, friendId int64, remarks string) error {
  129. friend, err := FriendRepo.Get(friendId, userId)
  130. if err != nil {
  131. return err
  132. }
  133. if friend == nil {
  134. utils.FilePutContents("AgreeAddFriend", utils.SerializeStr(map[string]interface{}{
  135. "userId": userId,
  136. "friendId": friendId,
  137. }))
  138. return gerrors.ErrBadRequest
  139. }
  140. if friend.Status == FriendStatusAgree {
  141. return nil
  142. }
  143. friend.Status = FriendStatusAgree
  144. err = FriendRepo.Save(friend)
  145. if err != nil {
  146. return err
  147. }
  148. newFriend, err := FriendRepo.Get(userId, friendId)
  149. if newFriend != nil && newFriend.Status != FriendStatusAgree {
  150. newFriend.Status = FriendStatusAgree
  151. err = FriendRepo.Save(newFriend)
  152. } else {
  153. now := time.Now()
  154. err = FriendRepo.Save(&Friend{
  155. UserId: userId,
  156. FriendId: friendId,
  157. Remarks: remarks,
  158. Status: FriendStatusAgree,
  159. CreateTime: now,
  160. UpdateTime: now,
  161. })
  162. }
  163. if err != nil {
  164. return err
  165. }
  166. resp, err := rpc.GetBusinessIntClient().GetUser(ctx, &pb.GetUserReq{UserId: userId})
  167. if err != nil {
  168. return err
  169. }
  170. err = proxy.MessageProxy.PushToUser(ctx, friendId, pb.PushCode_PC_AGREE_ADD_FRIEND, &pb.AgreeAddFriendPush{
  171. FriendId: userId,
  172. Nickname: resp.User.Nickname,
  173. AvatarUrl: resp.User.AvatarUrl,
  174. }, true)
  175. utils.FilePutContents("push_AgreeAddFriend", utils.SerializeStr(map[string]interface{}{
  176. "friend_id": userId,
  177. "friend_nick_name": resp.User.Nickname,
  178. "friend_avatar_url": resp.User.AvatarUrl,
  179. "err": err,
  180. }))
  181. if err != nil {
  182. return err
  183. }
  184. return nil
  185. }
  186. // SendToFriend 消息发送至好友
  187. func (*friendService) SendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  188. //TODO::判断是否为好友
  189. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  190. if err != nil {
  191. return 0, err
  192. }
  193. if friend == nil || friend.Status != FriendStatusAgree {
  194. return 0, gerrors.ErrNotIsFriend
  195. }
  196. //utils.FilePutContents("sendToFriend", utils.SerializeStr(map[string]interface{}{
  197. // "send": sender,
  198. // "req": req,
  199. //}))
  200. // 发给发送者
  201. seq, err := proxy.MessageProxy.SendToUser(ctx, sender, sender.SenderId, req)
  202. if err != nil {
  203. return 0, err
  204. }
  205. // 发给接收者
  206. _, err = proxy.MessageProxy.SendToUser(ctx, sender, req.ReceiverId, req)
  207. if err != nil {
  208. return 0, err
  209. }
  210. return seq, nil
  211. }
  212. // RecallMessageSendToFriend 撤回消息发送至好友
  213. func (*friendService) RecallMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  214. //TODO::判断是否为好友
  215. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  216. if err != nil {
  217. return 0, err
  218. }
  219. if friend == nil || friend.Status != FriendStatusAgree {
  220. return 0, gerrors.ErrNotIsFriend
  221. }
  222. utils.FilePutContents("RecallMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  223. "send": sender,
  224. "req": req,
  225. }))
  226. //查询到对应seq的消息
  227. msg := &pb.RECALL{}
  228. err = proto.Unmarshal(req.MessageContent, msg)
  229. if err != nil {
  230. return 0, err
  231. }
  232. message, err := repo.MessageRepo.GetMessage(sender.SenderId, msg.RecallSeq)
  233. if err != nil {
  234. return 0, err
  235. }
  236. // 发给发送者
  237. seq, err := proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, sender.SenderId, req, message.SendTime)
  238. if err != nil {
  239. return 0, err
  240. }
  241. // 发给接收者
  242. _, err = proxy.MessageProxy.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, message.SendTime)
  243. if err != nil {
  244. return 0, err
  245. }
  246. return seq, nil
  247. }
  248. // RedPackageMessageSendToFriend 红包发送至好友
  249. func (*friendService) RedPackageMessageSendToFriend(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  250. //TODO::判断是否为好友
  251. friend, err := FriendRepo.Get(sender.SenderId, req.ReceiverId)
  252. if err != nil {
  253. return 0, err
  254. }
  255. if friend == nil || friend.Status != FriendStatusAgree {
  256. return 0, gerrors.ErrNotIsFriend
  257. }
  258. utils.FilePutContents("RedPackageMessageSendToFriend", utils.SerializeStr(map[string]interface{}{
  259. "send": sender,
  260. "req": req,
  261. }))
  262. // 发给发送者
  263. seq, err := proxy.MessageProxy.SendRedPackageToUser(ctx, sender, sender.SenderId, req)
  264. if err != nil {
  265. return 0, err
  266. }
  267. // 发给接收者
  268. _, err = proxy.MessageProxy.SendRedPackageToUser(ctx, sender, req.ReceiverId, req)
  269. if err != nil {
  270. return 0, err
  271. }
  272. return seq, nil
  273. }