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.
 
 
 
 

110 line
3.8 KiB

  1. package app
  2. import (
  3. "context"
  4. "gim/internal/logic/domain/message/service"
  5. "gim/pkg/pb"
  6. "google.golang.org/protobuf/proto"
  7. )
  8. type messageApp struct{}
  9. var MessageApp = new(messageApp)
  10. // SendToUser 发送消息给用户
  11. func (*messageApp) SendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.SendMessageReq) (int64, error) {
  12. return service.MessageService.SendToUser(ctx, sender, toUserId, req)
  13. }
  14. // RecallMessageSendToUser 撤回消息发送给用户
  15. func (*messageApp) RecallMessageSendToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.RecallMessageReq, isRecallMessageUser bool) (int64, error) {
  16. return service.MessageService.RecallMessageSendToUser(ctx, sender, toUserId, req, isRecallMessageUser)
  17. }
  18. // SendRedPackageToUser 发送红包给用户
  19. func (*messageApp) SendRedPackageToUser(ctx context.Context, sender *pb.Sender, toUserId int64, req *pb.SendRedPacketReq) (int64, error) {
  20. return service.MessageService.SendRedPackageToUser(ctx, sender, toUserId, req)
  21. }
  22. // PushToUser 推送消息给用户
  23. func (*messageApp) PushToUser(ctx context.Context, userId int64, code pb.PushCode, message proto.Message, isPersist bool) error {
  24. return service.PushService.PushToUser(ctx, userId, code, message, isPersist)
  25. }
  26. // PushAll 全服推送
  27. func (*messageApp) PushAll(ctx context.Context, req *pb.PushAllReq) error {
  28. return service.PushService.PushAll(ctx, req)
  29. }
  30. // Sync 消息同步
  31. func (*messageApp) Sync(ctx context.Context, userId, seq int64) (*pb.SyncResp, error) {
  32. return service.MessageService.Sync(ctx, userId, seq)
  33. }
  34. // MessageAck 收到消息回执
  35. func (*messageApp) MessageAck(ctx context.Context, userId, deviceId, ack int64) error {
  36. return service.DeviceAckService.Update(ctx, userId, deviceId, ack)
  37. }
  38. // SendMessage 发送消息
  39. func (s *messageApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  40. // 如果发送者是用户,需要补充用户的信息
  41. service.MessageService.AddSenderInfo(sender)
  42. switch req.ReceiverType {
  43. // 消息接收者为用户
  44. case pb.ReceiverType_RT_USER:
  45. // 发送者为用户
  46. if sender.SenderType == pb.SenderType_ST_USER {
  47. return FriendApp.SendToFriend(ctx, sender, req)
  48. } else {
  49. return s.SendToUser(ctx, sender, req.ReceiverId, req)
  50. }
  51. // 消息接收者是群组
  52. case pb.ReceiverType_RT_GROUP:
  53. return GroupApp.SendMessage(ctx, sender, req)
  54. }
  55. return 0, nil
  56. }
  57. // RecallMessage 撤回消息
  58. func (s *messageApp) RecallMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  59. // 如果发送者是用户,需要补充用户的信息
  60. service.MessageService.AddSenderInfo(sender)
  61. switch req.ReceiverType {
  62. // 消息接收者为用户
  63. case pb.ReceiverType_RT_USER:
  64. // 发送者为用户
  65. if sender.SenderType == pb.SenderType_ST_USER {
  66. return FriendApp.RecallMessageSendToFriend(ctx, sender, req)
  67. } else {
  68. return s.RecallMessageSendToUser(ctx, sender, req.ReceiverId, req, true)
  69. }
  70. // 消息接收者是群组
  71. case pb.ReceiverType_RT_GROUP:
  72. return GroupApp.RecallSendMessage(ctx, sender, req)
  73. }
  74. return 0, nil
  75. }
  76. // SendRedPackage 发送红包消息
  77. func (s *messageApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  78. // 如果发送者是用户,需要补充用户的信息
  79. service.MessageService.AddSenderInfo(sender)
  80. switch req.ReceiverType {
  81. // 消息接收者为用户
  82. case pb.ReceiverType_RT_USER:
  83. // 发送者为用户
  84. if sender.SenderType == pb.SenderType_ST_USER {
  85. return FriendApp.RedPackageMessageSendToFriend(ctx, sender, req)
  86. } else {
  87. return s.SendRedPackageToUser(ctx, sender, req.ReceiverId, req)
  88. }
  89. // 消息接收者是群组
  90. case pb.ReceiverType_RT_GROUP:
  91. return GroupApp.SendRedPackage(ctx, sender, req)
  92. }
  93. return 0, nil
  94. }