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.
 
 
 
 

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