Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

311 řádky
9.0 KiB

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