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.

logic_ext.go 4.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api
  2. import (
  3. "context"
  4. "gim/internal/logic/app"
  5. "gim/pkg/grpclib"
  6. "gim/pkg/pb"
  7. "google.golang.org/protobuf/proto"
  8. )
  9. type LogicExtServer struct{}
  10. // RegisterDevice 注册设备
  11. func (*LogicExtServer) RegisterDevice(ctx context.Context, in *pb.RegisterDeviceReq) (*pb.RegisterDeviceResp, error) {
  12. deviceId, err := app.DeviceApp.Register(ctx, in)
  13. return &pb.RegisterDeviceResp{DeviceId: deviceId}, err
  14. }
  15. // SendMessage 发送消息
  16. func (*LogicExtServer) SendMessage(ctx context.Context, in *pb.SendMessageReq) (*pb.SendMessageResp, error) {
  17. userId, deviceId, err := grpclib.GetCtxData(ctx)
  18. if err != nil {
  19. return nil, err
  20. }
  21. if in.MessageContentBack != "" {
  22. buf, err := proto.Marshal(&pb.Text{
  23. Text: "hello world!",
  24. })
  25. if err != nil {
  26. return nil, err
  27. }
  28. in.MessageContent = buf
  29. }
  30. sender := pb.Sender{
  31. SenderType: pb.SenderType_ST_USER,
  32. SenderId: userId,
  33. DeviceId: deviceId,
  34. }
  35. seq, err := app.MessageApp.SendMessage(ctx, &sender, in)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &pb.SendMessageResp{Seq: seq}, nil
  40. }
  41. // PushRoom 推送房间
  42. func (s *LogicExtServer) PushRoom(ctx context.Context, req *pb.PushRoomReq) (*pb.Empty, error) {
  43. userId, deviceId, err := grpclib.GetCtxData(ctx)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &pb.Empty{}, app.RoomApp.Push(ctx, &pb.Sender{
  48. SenderType: pb.SenderType_ST_USER,
  49. SenderId: userId,
  50. DeviceId: deviceId,
  51. }, req)
  52. }
  53. func (s *LogicExtServer) AddFriend(ctx context.Context, in *pb.AddFriendReq) (*pb.Empty, error) {
  54. userId, _, err := grpclib.GetCtxData(ctx)
  55. if err != nil {
  56. return nil, err
  57. }
  58. err = app.FriendApp.AddFriend(ctx, userId, in.FriendId, in.Remarks, in.Description)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &pb.Empty{}, nil
  63. }
  64. func (s *LogicExtServer) AgreeAddFriend(ctx context.Context, in *pb.AgreeAddFriendReq) (*pb.Empty, error) {
  65. userId, _, err := grpclib.GetCtxData(ctx)
  66. if err != nil {
  67. return nil, err
  68. }
  69. err = app.FriendApp.AgreeAddFriend(ctx, userId, in.UserId, in.Remarks)
  70. if err != nil {
  71. return nil, err
  72. }
  73. return &pb.Empty{}, nil
  74. }
  75. func (s *LogicExtServer) SetFriend(ctx context.Context, req *pb.SetFriendReq) (*pb.SetFriendResp, error) {
  76. userId, _, err := grpclib.GetCtxData(ctx)
  77. if err != nil {
  78. return nil, err
  79. }
  80. app.FriendApp.SetFriend(ctx, userId, req)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &pb.SetFriendResp{}, nil
  85. }
  86. func (s *LogicExtServer) GetFriends(ctx context.Context, in *pb.Empty) (*pb.GetFriendsResp, error) {
  87. userId, _, err := grpclib.GetCtxData(ctx)
  88. if err != nil {
  89. return nil, err
  90. }
  91. friends, err := app.FriendApp.List(ctx, userId)
  92. return &pb.GetFriendsResp{Friends: friends}, err
  93. }
  94. // CreateGroup 创建群组
  95. func (*LogicExtServer) CreateGroup(ctx context.Context, in *pb.CreateGroupReq) (*pb.CreateGroupResp, error) {
  96. userId, _, err := grpclib.GetCtxData(ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. groupId, err := app.GroupApp.CreateGroup(ctx, userId, in)
  101. return &pb.CreateGroupResp{GroupId: groupId}, err
  102. }
  103. // UpdateGroup 更新群组
  104. func (*LogicExtServer) UpdateGroup(ctx context.Context, in *pb.UpdateGroupReq) (*pb.Empty, error) {
  105. userId, _, err := grpclib.GetCtxData(ctx)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return &pb.Empty{}, app.GroupApp.Update(ctx, userId, in)
  110. }
  111. // GetGroup 获取群组信息
  112. func (*LogicExtServer) GetGroup(ctx context.Context, in *pb.GetGroupReq) (*pb.GetGroupResp, error) {
  113. group, err := app.GroupApp.GetGroup(ctx, in.GroupId)
  114. return &pb.GetGroupResp{Group: group}, err
  115. }
  116. // GetGroups 获取用户加入的所有群组
  117. func (*LogicExtServer) GetGroups(ctx context.Context, in *pb.Empty) (*pb.GetGroupsResp, error) {
  118. userId, _, err := grpclib.GetCtxData(ctx)
  119. if err != nil {
  120. return nil, err
  121. }
  122. groups, err := app.GroupApp.GetUserGroups(ctx, userId)
  123. return &pb.GetGroupsResp{Groups: groups}, err
  124. }
  125. func (s *LogicExtServer) AddGroupMembers(ctx context.Context, in *pb.AddGroupMembersReq) (*pb.AddGroupMembersResp, error) {
  126. userId, _, err := grpclib.GetCtxData(ctx)
  127. if err != nil {
  128. return nil, err
  129. }
  130. userIds, err := app.GroupApp.AddMembers(ctx, userId, in.GroupId, in.UserIds)
  131. return &pb.AddGroupMembersResp{UserIds: userIds}, err
  132. }
  133. // UpdateGroupMember 更新群组成员信息
  134. func (*LogicExtServer) UpdateGroupMember(ctx context.Context, in *pb.UpdateGroupMemberReq) (*pb.Empty, error) {
  135. return &pb.Empty{}, app.GroupApp.UpdateMember(ctx, in)
  136. }
  137. // DeleteGroupMember 添加群组成员
  138. func (*LogicExtServer) DeleteGroupMember(ctx context.Context, in *pb.DeleteGroupMemberReq) (*pb.Empty, error) {
  139. userId, _, err := grpclib.GetCtxData(ctx)
  140. if err != nil {
  141. return nil, err
  142. }
  143. err = app.GroupApp.DeleteMember(ctx, in.GroupId, in.UserId, userId)
  144. return &pb.Empty{}, err
  145. }
  146. // GetGroupMembers 获取群组成员信息
  147. func (s *LogicExtServer) GetGroupMembers(ctx context.Context, in *pb.GetGroupMembersReq) (*pb.GetGroupMembersResp, error) {
  148. members, err := app.GroupApp.GetMembers(ctx, in.GroupId)
  149. return &pb.GetGroupMembersResp{Members: members}, err
  150. }