golang-im聊天
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

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