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.
 
 
 
 

287 lines
7.5 KiB

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