golang-im聊天
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

group_app.go 8.1 KiB

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