golang-im聊天
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

group_app.go 6.0 KiB

há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 2 anos
há 1 ano
há 1 ano
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package app
  2. import (
  3. "context"
  4. "errors"
  5. "gim/internal/logic/domain/group/model"
  6. "gim/internal/logic/domain/group/repo"
  7. "gim/pkg/db"
  8. "gim/pkg/pb"
  9. )
  10. type groupApp struct{}
  11. var GroupApp = new(groupApp)
  12. // CreateGroup 创建群组
  13. func (*groupApp) CreateGroup(ctx context.Context, userId int64, in *pb.CreateGroupReq) (int64, error) {
  14. group := model.CreateGroup(userId, in)
  15. err := repo.GroupRepo.Save(group)
  16. if err != nil {
  17. return 0, err
  18. }
  19. return group.Id, nil
  20. }
  21. // GetGroup 获取群组信息
  22. func (*groupApp) GetGroup(ctx context.Context, groupId int64) (*pb.Group, error) {
  23. group, err := repo.GroupRepo.Get(groupId)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return group.ToProto(), nil
  28. }
  29. // GetUserGroups 获取用户加入的群组列表
  30. func (*groupApp) GetUserGroups(ctx context.Context, userId int64) ([]*pb.Group, error) {
  31. groups, err := repo.GroupUserRepo.ListByUserId(userId)
  32. if err != nil {
  33. return nil, err
  34. }
  35. pbGroups := make([]*pb.Group, len(groups))
  36. for i := range groups {
  37. pbGroups[i] = groups[i].ToProto()
  38. }
  39. return pbGroups, nil
  40. }
  41. // Update 更新群组
  42. func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
  43. group, err := repo.GroupRepo.Get(update.GroupId)
  44. if err != nil {
  45. return err
  46. }
  47. var isUpdateIntroduction = false
  48. if group.Introduction != update.Introduction {
  49. isUpdateIntroduction = true
  50. }
  51. err = group.Update(ctx, update)
  52. if err != nil {
  53. return err
  54. }
  55. err = repo.GroupRepo.Save(group)
  56. if err != nil {
  57. return err
  58. }
  59. err = group.PushUpdate(ctx, userId, isUpdateIntroduction)
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // AddMembers 添加群组成员
  66. func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
  67. group, err := repo.GroupRepo.Get(groupId)
  68. if err != nil {
  69. return nil, err
  70. }
  71. existIds, addedIds, err := group.AddMembers(ctx, userIds)
  72. if err != nil {
  73. return nil, err
  74. }
  75. err = repo.GroupRepo.Save(group)
  76. if err != nil {
  77. return nil, err
  78. }
  79. err = group.PushAddMember(ctx, userId, addedIds)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return existIds, nil
  84. }
  85. // UpdateMember 更新群组用户
  86. func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq) error {
  87. group, err := repo.GroupRepo.Get(in.GroupId)
  88. if err != nil {
  89. return err
  90. }
  91. err = group.UpdateMember(ctx, in)
  92. if err != nil {
  93. return err
  94. }
  95. err = repo.GroupRepo.Save(group)
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. // DeleteMember 删除群组成员
  102. func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
  103. group, err := repo.GroupRepo.Get(groupId)
  104. if err != nil {
  105. return err
  106. }
  107. err = group.DeleteMember(ctx, userId)
  108. if err != nil {
  109. return err
  110. }
  111. err = repo.GroupRepo.Save(group)
  112. if err != nil {
  113. return err
  114. }
  115. err = group.PushDeleteMember(ctx, optId, userId)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }
  121. // GetMembers 获取群组成员
  122. func (*groupApp) GetMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  123. group, err := repo.GroupRepo.Get(groupId)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return group.GetMembers(ctx)
  128. }
  129. // SendMessage 发送群组消息
  130. func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  131. group, err := repo.GroupRepo.Get(req.ReceiverId)
  132. if err != nil {
  133. return 0, err
  134. }
  135. return group.SendMessage(ctx, sender, req)
  136. }
  137. // GetBannedMembers 获取群组禁言成员
  138. func (*groupApp) GetBannedMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  139. // 查询被禁言的成员列表
  140. group, err := repo.GroupRepo.GetForBanned(groupId)
  141. if err != nil {
  142. return nil, err
  143. }
  144. return group.GetMembers(ctx)
  145. }
  146. // SetGroupMemberBanned 设置禁言
  147. func (*groupApp) SetGroupMemberBanned(ctx context.Context, optId, groupId int64, userIds []int64, isAllMemberBanned bool) ([]*pb.GroupMember, error) {
  148. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  149. if err != nil {
  150. return nil, err
  151. }
  152. if groupUser == nil {
  153. return nil, errors.New("未查询到群组用户信息")
  154. }
  155. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) {
  156. return nil, errors.New("非管理员操作")
  157. }
  158. group, err := repo.GroupRepo.Get(groupId)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if isAllMemberBanned {
  163. //设置全员禁言
  164. //1、更新 `group` 的 is_all_member_banned 状态
  165. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
  166. err := repo.GroupRepo.Save(group)
  167. if err != nil {
  168. return nil, err
  169. }
  170. //2、发送推送消息
  171. err = group.PushGroupMemberBanned(ctx, optId, 0, isAllMemberBanned)
  172. if err != nil {
  173. return nil, err
  174. }
  175. } else {
  176. //设置成员禁言
  177. //1、更新 `group` 的 is_all_member_banned 状态
  178. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
  179. err := repo.GroupRepo.Save(group)
  180. if err != nil {
  181. return nil, err
  182. }
  183. //2、更新 `group_user` 的 status 状态
  184. db.DB.Model(model.GroupUser{}).Where("group_id = ?", groupId).Where("user_id IN ?", userIds).Updates(model.GroupUser{Status: int(pb.GroupUserStatusType_GROUP_USER_STATUS_Banned)})
  185. //3、发送推送消息
  186. for _, u := range userIds {
  187. err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  188. if err != nil {
  189. return nil, err
  190. }
  191. }
  192. }
  193. // 查询被禁言的成员列表
  194. group, err = repo.GroupRepo.Get(groupId)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return group.GetMembers(ctx)
  199. }
  200. // RecallSendMessage 撤回发送消息
  201. func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  202. group, err := repo.GroupRepo.Get(req.ReceiverId)
  203. if err != nil {
  204. return 0, err
  205. }
  206. return group.RecallSendMessage(ctx, sender, req)
  207. }
  208. // SendRedPackage 发送红包消息
  209. func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  210. group, err := repo.GroupRepo.Get(req.ReceiverId)
  211. if err != nil {
  212. return 0, err
  213. }
  214. return group.SendRedPackage(ctx, sender, req)
  215. }