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

305 行
8.1 KiB

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