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.
 
 
 
 

247 lines
6.0 KiB

  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. }