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.
 
 
 
 

191 righe
5.2 KiB

  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: in.MessageContentBack,
  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) DeleteFriend(ctx context.Context, in *pb.DeleteFriendReq) (*pb.Empty, error) {
  76. userId, _, err := grpclib.GetCtxData(ctx)
  77. if err != nil {
  78. return nil, err
  79. }
  80. err = app.FriendApp.DeleteFriend(ctx, userId, in.UserId)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &pb.Empty{}, nil
  85. }
  86. func (s *LogicExtServer) SetFriend(ctx context.Context, req *pb.SetFriendReq) (*pb.SetFriendResp, error) {
  87. userId, _, err := grpclib.GetCtxData(ctx)
  88. if err != nil {
  89. return nil, err
  90. }
  91. app.FriendApp.SetFriend(ctx, userId, req)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return &pb.SetFriendResp{}, nil
  96. }
  97. func (s *LogicExtServer) GetFriends(ctx context.Context, in *pb.Empty) (*pb.GetFriendsResp, error) {
  98. userId, _, err := grpclib.GetCtxData(ctx)
  99. if err != nil {
  100. return nil, err
  101. }
  102. friends, err := app.FriendApp.List(ctx, userId)
  103. return &pb.GetFriendsResp{Friends: friends}, err
  104. }
  105. // CreateGroup 创建群组
  106. func (*LogicExtServer) CreateGroup(ctx context.Context, in *pb.CreateGroupReq) (*pb.CreateGroupResp, error) {
  107. userId, _, err := grpclib.GetCtxData(ctx)
  108. if err != nil {
  109. return nil, err
  110. }
  111. groupId, err := app.GroupApp.CreateGroup(ctx, userId, in)
  112. return &pb.CreateGroupResp{GroupId: groupId}, err
  113. }
  114. // UpdateGroup 更新群组
  115. func (*LogicExtServer) UpdateGroup(ctx context.Context, in *pb.UpdateGroupReq) (*pb.Empty, error) {
  116. userId, _, err := grpclib.GetCtxData(ctx)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return &pb.Empty{}, app.GroupApp.Update(ctx, userId, in)
  121. }
  122. // GetGroup 获取群组信息
  123. func (*LogicExtServer) GetGroup(ctx context.Context, in *pb.GetGroupReq) (*pb.GetGroupResp, error) {
  124. group, err := app.GroupApp.GetGroup(ctx, in.GroupId)
  125. return &pb.GetGroupResp{Group: group}, err
  126. }
  127. // GetGroups 获取用户加入的所有群组
  128. func (*LogicExtServer) GetGroups(ctx context.Context, in *pb.Empty) (*pb.GetGroupsResp, error) {
  129. userId, _, err := grpclib.GetCtxData(ctx)
  130. if err != nil {
  131. return nil, err
  132. }
  133. groups, err := app.GroupApp.GetUserGroups(ctx, userId)
  134. return &pb.GetGroupsResp{Groups: groups}, err
  135. }
  136. func (s *LogicExtServer) AddGroupMembers(ctx context.Context, in *pb.AddGroupMembersReq) (*pb.AddGroupMembersResp, error) {
  137. userId, _, err := grpclib.GetCtxData(ctx)
  138. if err != nil {
  139. return nil, err
  140. }
  141. userIds, err := app.GroupApp.AddMembers(ctx, userId, in.GroupId, in.UserIds)
  142. return &pb.AddGroupMembersResp{UserIds: userIds}, err
  143. }
  144. // UpdateGroupMember 更新群组成员信息
  145. func (*LogicExtServer) UpdateGroupMember(ctx context.Context, in *pb.UpdateGroupMemberReq) (*pb.Empty, error) {
  146. return &pb.Empty{}, app.GroupApp.UpdateMember(ctx, in)
  147. }
  148. // DeleteGroupMember 添加群组成员
  149. func (*LogicExtServer) DeleteGroupMember(ctx context.Context, in *pb.DeleteGroupMemberReq) (*pb.Empty, error) {
  150. userId, _, err := grpclib.GetCtxData(ctx)
  151. if err != nil {
  152. return nil, err
  153. }
  154. err = app.GroupApp.DeleteMember(ctx, in.GroupId, in.UserId, userId)
  155. return &pb.Empty{}, err
  156. }
  157. // GetGroupMembers 获取群组成员信息
  158. func (s *LogicExtServer) GetGroupMembers(ctx context.Context, in *pb.GetGroupMembersReq) (*pb.GetGroupMembersResp, error) {
  159. members, err := app.GroupApp.GetMembers(ctx, in.GroupId)
  160. return &pb.GetGroupMembersResp{Members: members}, err
  161. }