golang-im聊天
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

60 satır
1.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. // PushToUser 推送消息给用户
  15. func (*messageApp) PushToUser(ctx context.Context, userId int64, code pb.PushCode, message proto.Message, isPersist bool) error {
  16. return service.PushService.PushToUser(ctx, userId, code, message, isPersist)
  17. }
  18. // PushAll 全服推送
  19. func (*messageApp) PushAll(ctx context.Context, req *pb.PushAllReq) error {
  20. return service.PushService.PushAll(ctx, req)
  21. }
  22. // Sync 消息同步
  23. func (*messageApp) Sync(ctx context.Context, userId, seq int64) (*pb.SyncResp, error) {
  24. return service.MessageService.Sync(ctx, userId, seq)
  25. }
  26. // MessageAck 收到消息回执
  27. func (*messageApp) MessageAck(ctx context.Context, userId, deviceId, ack int64) error {
  28. return service.DeviceAckService.Update(ctx, userId, deviceId, ack)
  29. }
  30. // SendMessage 发送消息
  31. func (s *messageApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  32. // 如果发送者是用户,需要补充用户的信息
  33. service.MessageService.AddSenderInfo(sender)
  34. switch req.ReceiverType {
  35. // 消息接收者为用户
  36. case pb.ReceiverType_RT_USER:
  37. // 发送者为用户
  38. if sender.SenderType == pb.SenderType_ST_USER {
  39. return FriendApp.SendToFriend(ctx, sender, req)
  40. } else {
  41. return s.SendToUser(ctx, sender, req.ReceiverId, req)
  42. }
  43. // 消息接收者是群组
  44. case pb.ReceiverType_RT_GROUP:
  45. return GroupApp.SendMessage(ctx, sender, req)
  46. }
  47. return 0, nil
  48. }