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.
 
 
 
 

243 líneas
6.5 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. func (s *LogicExtServer) SendRedPacket(ctx context.Context, in *pb.SendRedPacketReq) (*pb.SendRedPacketResp, error) {
  11. userId, deviceId, err := grpclib.GetCtxData(ctx)
  12. if err != nil {
  13. return nil, err
  14. }
  15. if in.MessageContentBack != "" {
  16. buf, err := proto.Marshal(&pb.Text{
  17. Text: in.MessageContentBack,
  18. })
  19. if err != nil {
  20. return nil, err
  21. }
  22. in.MessageContent = buf
  23. }
  24. sender := pb.Sender{
  25. SenderType: pb.SenderType_ST_USER,
  26. SenderId: userId,
  27. DeviceId: deviceId,
  28. }
  29. seq, err := app.MessageApp.SendRedPackage(ctx, &sender, in)
  30. if err != nil {
  31. return nil, err
  32. }
  33. return &pb.SendRedPacketResp{Seq: seq}, nil
  34. }
  35. func (s *LogicExtServer) RecallMessage(ctx context.Context, in *pb.RecallMessageReq) (*pb.RecallMessageResp, error) {
  36. userId, deviceId, err := grpclib.GetCtxData(ctx)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if in.MessageContentBack != "" {
  41. buf, err := proto.Marshal(&pb.Text{
  42. Text: in.MessageContentBack,
  43. })
  44. if err != nil {
  45. return nil, err
  46. }
  47. in.MessageContent = buf
  48. }
  49. sender := pb.Sender{
  50. SenderType: pb.SenderType_ST_USER,
  51. SenderId: userId,
  52. DeviceId: deviceId,
  53. }
  54. seq, err := app.MessageApp.RecallMessage(ctx, &sender, in)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &pb.RecallMessageResp{Seq: seq}, nil
  59. }
  60. // RegisterDevice 注册设备
  61. func (*LogicExtServer) RegisterDevice(ctx context.Context, in *pb.RegisterDeviceReq) (*pb.RegisterDeviceResp, error) {
  62. deviceId, err := app.DeviceApp.Register(ctx, in)
  63. return &pb.RegisterDeviceResp{DeviceId: deviceId}, err
  64. }
  65. // SendMessage 发送消息
  66. func (*LogicExtServer) SendMessage(ctx context.Context, in *pb.SendMessageReq) (*pb.SendMessageResp, error) {
  67. userId, deviceId, err := grpclib.GetCtxData(ctx)
  68. if err != nil {
  69. return nil, err
  70. }
  71. if in.MessageContentBack != "" {
  72. buf, err := proto.Marshal(&pb.Text{
  73. Text: in.MessageContentBack,
  74. })
  75. if err != nil {
  76. return nil, err
  77. }
  78. in.MessageContent = buf
  79. }
  80. sender := pb.Sender{
  81. SenderType: pb.SenderType_ST_USER,
  82. SenderId: userId,
  83. DeviceId: deviceId,
  84. }
  85. seq, err := app.MessageApp.SendMessage(ctx, &sender, in)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &pb.SendMessageResp{Seq: seq}, nil
  90. }
  91. // PushRoom 推送房间
  92. func (s *LogicExtServer) PushRoom(ctx context.Context, req *pb.PushRoomReq) (*pb.Empty, error) {
  93. userId, deviceId, err := grpclib.GetCtxData(ctx)
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &pb.Empty{}, app.RoomApp.Push(ctx, &pb.Sender{
  98. SenderType: pb.SenderType_ST_USER,
  99. SenderId: userId,
  100. DeviceId: deviceId,
  101. }, req)
  102. }
  103. func (s *LogicExtServer) AddFriend(ctx context.Context, in *pb.AddFriendReq) (*pb.Empty, error) {
  104. userId, _, err := grpclib.GetCtxData(ctx)
  105. if err != nil {
  106. return nil, err
  107. }
  108. err = app.FriendApp.AddFriend(ctx, userId, in.FriendId, in.Remarks, in.Description)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &pb.Empty{}, nil
  113. }
  114. func (s *LogicExtServer) AgreeAddFriend(ctx context.Context, in *pb.AgreeAddFriendReq) (*pb.Empty, error) {
  115. userId, _, err := grpclib.GetCtxData(ctx)
  116. if err != nil {
  117. return nil, err
  118. }
  119. err = app.FriendApp.AgreeAddFriend(ctx, userId, in.UserId, in.Remarks)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return &pb.Empty{}, nil
  124. }
  125. func (s *LogicExtServer) DeleteFriend(ctx context.Context, in *pb.DeleteFriendReq) (*pb.Empty, error) {
  126. userId, _, err := grpclib.GetCtxData(ctx)
  127. if err != nil {
  128. return nil, err
  129. }
  130. err = app.FriendApp.DeleteFriend(ctx, userId, in.UserId)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return &pb.Empty{}, nil
  135. }
  136. func (s *LogicExtServer) SetFriend(ctx context.Context, req *pb.SetFriendReq) (*pb.SetFriendResp, error) {
  137. userId, _, err := grpclib.GetCtxData(ctx)
  138. if err != nil {
  139. return nil, err
  140. }
  141. app.FriendApp.SetFriend(ctx, userId, req)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return &pb.SetFriendResp{}, nil
  146. }
  147. func (s *LogicExtServer) GetFriends(ctx context.Context, in *pb.Empty) (*pb.GetFriendsResp, error) {
  148. userId, _, err := grpclib.GetCtxData(ctx)
  149. if err != nil {
  150. return nil, err
  151. }
  152. friends, err := app.FriendApp.List(ctx, userId)
  153. return &pb.GetFriendsResp{Friends: friends}, err
  154. }
  155. // CreateGroup 创建群组
  156. func (*LogicExtServer) CreateGroup(ctx context.Context, in *pb.CreateGroupReq) (*pb.CreateGroupResp, error) {
  157. userId, _, err := grpclib.GetCtxData(ctx)
  158. if err != nil {
  159. return nil, err
  160. }
  161. groupId, err := app.GroupApp.CreateGroup(ctx, userId, in)
  162. return &pb.CreateGroupResp{GroupId: groupId}, err
  163. }
  164. // UpdateGroup 更新群组
  165. func (*LogicExtServer) UpdateGroup(ctx context.Context, in *pb.UpdateGroupReq) (*pb.Empty, error) {
  166. userId, _, err := grpclib.GetCtxData(ctx)
  167. if err != nil {
  168. return nil, err
  169. }
  170. return &pb.Empty{}, app.GroupApp.Update(ctx, userId, in)
  171. }
  172. // GetGroup 获取群组信息
  173. func (*LogicExtServer) GetGroup(ctx context.Context, in *pb.GetGroupReq) (*pb.GetGroupResp, error) {
  174. group, err := app.GroupApp.GetGroup(ctx, in.GroupId)
  175. return &pb.GetGroupResp{Group: group}, err
  176. }
  177. // GetGroups 获取用户加入的所有群组
  178. func (*LogicExtServer) GetGroups(ctx context.Context, in *pb.Empty) (*pb.GetGroupsResp, error) {
  179. userId, _, err := grpclib.GetCtxData(ctx)
  180. if err != nil {
  181. return nil, err
  182. }
  183. groups, err := app.GroupApp.GetUserGroups(ctx, userId)
  184. return &pb.GetGroupsResp{Groups: groups}, err
  185. }
  186. func (s *LogicExtServer) AddGroupMembers(ctx context.Context, in *pb.AddGroupMembersReq) (*pb.AddGroupMembersResp, error) {
  187. userId, _, err := grpclib.GetCtxData(ctx)
  188. if err != nil {
  189. return nil, err
  190. }
  191. userIds, err := app.GroupApp.AddMembers(ctx, userId, in.GroupId, in.UserIds)
  192. return &pb.AddGroupMembersResp{UserIds: userIds}, err
  193. }
  194. // UpdateGroupMember 更新群组成员信息
  195. func (*LogicExtServer) UpdateGroupMember(ctx context.Context, in *pb.UpdateGroupMemberReq) (*pb.Empty, error) {
  196. return &pb.Empty{}, app.GroupApp.UpdateMember(ctx, in)
  197. }
  198. // DeleteGroupMember 添加群组成员
  199. func (*LogicExtServer) DeleteGroupMember(ctx context.Context, in *pb.DeleteGroupMemberReq) (*pb.Empty, error) {
  200. userId, _, err := grpclib.GetCtxData(ctx)
  201. if err != nil {
  202. return nil, err
  203. }
  204. err = app.GroupApp.DeleteMember(ctx, in.GroupId, in.UserId, userId)
  205. return &pb.Empty{}, err
  206. }
  207. // GetGroupMembers 获取群组成员信息
  208. func (s *LogicExtServer) GetGroupMembers(ctx context.Context, in *pb.GetGroupMembersReq) (*pb.GetGroupMembersResp, error) {
  209. members, err := app.GroupApp.GetMembers(ctx, in.GroupId)
  210. return &pb.GetGroupMembersResp{Members: members}, err
  211. }