golang-im聊天
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

195 líneas
5.4 KiB

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