golang-im聊天
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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