golang-im聊天
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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