golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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