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.
 
 
 
 

171 rinda
3.8 KiB

  1. package app
  2. import (
  3. "context"
  4. "gim/internal/logic/domain/group/model"
  5. "gim/internal/logic/domain/group/repo"
  6. "gim/pkg/pb"
  7. )
  8. type groupApp struct{}
  9. var GroupApp = new(groupApp)
  10. // CreateGroup 创建群组
  11. func (*groupApp) CreateGroup(ctx context.Context, userId int64, in *pb.CreateGroupReq) (int64, error) {
  12. group := model.CreateGroup(userId, in)
  13. err := repo.GroupRepo.Save(group)
  14. if err != nil {
  15. return 0, err
  16. }
  17. return group.Id, nil
  18. }
  19. // GetGroup 获取群组信息
  20. func (*groupApp) GetGroup(ctx context.Context, groupId int64) (*pb.Group, error) {
  21. group, err := repo.GroupRepo.Get(groupId)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return group.ToProto(), nil
  26. }
  27. // GetUserGroups 获取用户加入的群组列表
  28. func (*groupApp) GetUserGroups(ctx context.Context, userId int64) ([]*pb.Group, error) {
  29. groups, err := repo.GroupUserRepo.ListByUserId(userId)
  30. if err != nil {
  31. return nil, err
  32. }
  33. pbGroups := make([]*pb.Group, len(groups))
  34. for i := range groups {
  35. pbGroups[i] = groups[i].ToProto()
  36. }
  37. return pbGroups, nil
  38. }
  39. // Update 更新群组
  40. func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
  41. group, err := repo.GroupRepo.Get(update.GroupId)
  42. if err != nil {
  43. return err
  44. }
  45. err = group.Update(ctx, update)
  46. if err != nil {
  47. return err
  48. }
  49. err = repo.GroupRepo.Save(group)
  50. if err != nil {
  51. return err
  52. }
  53. err = group.PushUpdate(ctx, userId)
  54. if err != nil {
  55. return err
  56. }
  57. return nil
  58. }
  59. // AddMembers 添加群组成员
  60. func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
  61. group, err := repo.GroupRepo.Get(groupId)
  62. if err != nil {
  63. return nil, err
  64. }
  65. existIds, addedIds, err := group.AddMembers(ctx, userIds)
  66. if err != nil {
  67. return nil, err
  68. }
  69. err = repo.GroupRepo.Save(group)
  70. if err != nil {
  71. return nil, err
  72. }
  73. err = group.PushAddMember(ctx, userId, addedIds)
  74. if err != nil {
  75. return nil, err
  76. }
  77. return existIds, nil
  78. }
  79. // UpdateMember 更新群组用户
  80. func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq) error {
  81. group, err := repo.GroupRepo.Get(in.GroupId)
  82. if err != nil {
  83. return err
  84. }
  85. err = group.UpdateMember(ctx, in)
  86. if err != nil {
  87. return err
  88. }
  89. err = repo.GroupRepo.Save(group)
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. // DeleteMember 删除群组成员
  96. func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
  97. group, err := repo.GroupRepo.Get(groupId)
  98. if err != nil {
  99. return err
  100. }
  101. err = group.DeleteMember(ctx, userId)
  102. if err != nil {
  103. return err
  104. }
  105. err = repo.GroupRepo.Save(group)
  106. if err != nil {
  107. return err
  108. }
  109. err = group.PushDeleteMember(ctx, optId, userId)
  110. if err != nil {
  111. return err
  112. }
  113. return nil
  114. }
  115. // GetMembers 获取群组成员
  116. func (*groupApp) GetMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  117. group, err := repo.GroupRepo.Get(groupId)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return group.GetMembers(ctx)
  122. }
  123. // SendMessage 获取群组成员
  124. func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  125. group, err := repo.GroupRepo.Get(req.ReceiverId)
  126. if err != nil {
  127. return 0, err
  128. }
  129. return group.SendMessage(ctx, sender, req)
  130. }
  131. // RecallSendMessage 撤回发送消息
  132. func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  133. group, err := repo.GroupRepo.Get(req.ReceiverId)
  134. if err != nil {
  135. return 0, err
  136. }
  137. return group.RecallSendMessage(ctx, sender, req)
  138. }
  139. // SendRedPackage 发送红包消息
  140. func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  141. group, err := repo.GroupRepo.Get(req.ReceiverId)
  142. if err != nil {
  143. return 0, err
  144. }
  145. return group.SendRedPackage(ctx, sender, req)
  146. }