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.
 
 
 
 

401 rivejä
11 KiB

  1. package app
  2. import (
  3. "context"
  4. "database/sql"
  5. "egg-im/internal/logic/domain/group/model"
  6. "egg-im/internal/logic/domain/group/repo"
  7. "egg-im/pkg/db"
  8. "egg-im/pkg/grpclib"
  9. "egg-im/pkg/pb"
  10. "errors"
  11. "strings"
  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. group := model.CreateGroup(userId, in)
  18. err := repo.GroupRepo.Save(group)
  19. if err != nil {
  20. return 0, err
  21. }
  22. in.MemberIds = append(in.MemberIds, userId)
  23. err = group.PushAddMember(ctx, userId, in.MemberIds, false)
  24. return group.Id, nil
  25. }
  26. // GetGroup 获取群组信息
  27. func (*groupApp) GetGroup(ctx context.Context, groupId int64) (*pb.Group, pb.MemberType, pb.GroupUserStatusType, error) {
  28. group, err := repo.GroupRepo.Get(groupId)
  29. memberType := pb.MemberType_GMT_UNKNOWN
  30. userStatusType := pb.GroupUserStatusType_GROUP_USER_STATUS_NORMAL
  31. if err != nil {
  32. return nil, memberType, userStatusType, err
  33. }
  34. if err != nil {
  35. return nil, memberType, userStatusType, err
  36. }
  37. userId, _, err := grpclib.GetCtxData(ctx)
  38. if err != nil {
  39. return nil, memberType, userStatusType, err
  40. }
  41. if !strings.Contains(group.Name, "官方") && !strings.Contains(group.Name, "运营中心") {
  42. for _, member := range group.Members {
  43. if member.UserId == userId && member.MemberType != int(pb.MemberType_GMT_ADMIN) {
  44. group.Name = "我的推荐人群"
  45. }
  46. }
  47. }
  48. for _, member := range group.Members {
  49. if member.UserId == userId {
  50. memberType = pb.MemberType(member.MemberType)
  51. userStatusType = pb.GroupUserStatusType(member.Status)
  52. }
  53. }
  54. return group.ToProto(), memberType, userStatusType, nil
  55. }
  56. // GetUserGroups 获取用户加入的群组列表
  57. func (*groupApp) GetUserGroups(ctx context.Context, userId int64) ([]*pb.Group, error) {
  58. groups, err := repo.GroupUserRepo.ListByUserId(userId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. pbGroups := make([]*pb.Group, len(groups))
  63. for i := range groups {
  64. pbGroups[i] = groups[i].ToProto()
  65. groupUser, err := repo.GroupUserRepo.Get(pbGroups[i].GroupId, userId)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if !strings.Contains(pbGroups[i].Name, "官方") && !strings.Contains(pbGroups[i].Name, "运营中心") {
  70. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) {
  71. pbGroups[i].Name = "我的推荐人群"
  72. }
  73. }
  74. }
  75. return pbGroups, nil
  76. }
  77. // Update 更新群组
  78. func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
  79. group, err := repo.GroupRepo.Get(update.GroupId)
  80. if err != nil {
  81. return err
  82. }
  83. var isUpdateIntroduction = false
  84. if group.Introduction != update.Introduction {
  85. isUpdateIntroduction = true
  86. }
  87. err = group.Update(ctx, update)
  88. if err != nil {
  89. return err
  90. }
  91. err = repo.GroupRepo.Save(group)
  92. if err != nil {
  93. return err
  94. }
  95. err = group.PushUpdate(ctx, userId, isUpdateIntroduction)
  96. if err != nil {
  97. return err
  98. }
  99. return nil
  100. }
  101. // AddMembers 添加群组成员
  102. func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
  103. group, err := repo.GroupRepo.Get(groupId)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if int64(group.UserNum)+int64(len(userIds)) <= 500 {
  108. return nil, errors.New("群人员数量已满!")
  109. }
  110. existIds, addedIds, err := group.AddMembers(ctx, userIds)
  111. if err != nil {
  112. return nil, err
  113. }
  114. err = repo.GroupRepo.Save(group)
  115. if err != nil {
  116. return nil, err
  117. }
  118. err = group.PushAddMember(ctx, userId, addedIds, true)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return existIds, nil
  123. }
  124. // UpdateMember 更新群组用户
  125. func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq, optId int64) error {
  126. group, err := repo.GroupRepo.Get(in.GroupId)
  127. if err != nil {
  128. return err
  129. }
  130. err = group.UpdateMember(ctx, in)
  131. if err != nil {
  132. return err
  133. }
  134. err = repo.GroupRepo.Save(group)
  135. if err != nil {
  136. return err
  137. }
  138. err = group.PushUpdateMember(ctx, optId, in.UserId, int32(in.MemberType))
  139. if err != nil {
  140. return err
  141. }
  142. return nil
  143. }
  144. // DeleteMember 删除群组成员
  145. func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
  146. group, err := repo.GroupRepo.Get(groupId)
  147. if err != nil {
  148. return err
  149. }
  150. err = group.DeleteMember(ctx, userId)
  151. if err != nil {
  152. return err
  153. }
  154. group.UserNum-- //群组人数减一
  155. err = repo.GroupRepo.Save(group)
  156. if err != nil {
  157. return err
  158. }
  159. if userId == optId { //自己退出群聊,无需发送消息
  160. return nil
  161. }
  162. err = group.PushDeleteMember(ctx, optId, userId)
  163. if err != nil {
  164. return err
  165. }
  166. return nil
  167. }
  168. // GetMembers 获取群组成员
  169. func (*groupApp) GetMembers(ctx context.Context, groupId, limit int64) ([]*pb.GroupMember, error) {
  170. if limit == 0 {
  171. group, err := repo.GroupRepo.Get(groupId)
  172. if err != nil {
  173. return nil, err
  174. }
  175. return group.GetMembers(ctx)
  176. } else {
  177. group, err := repo.GroupRepo.GetLimit(groupId, limit)
  178. if err != nil {
  179. return nil, err
  180. }
  181. return group.GetMembers(ctx)
  182. }
  183. }
  184. // SendMessage 发送群组消息
  185. func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  186. group, err := repo.GroupRepo.Get(req.ReceiverId)
  187. if err != nil {
  188. return 0, err
  189. }
  190. return group.SendMessage(ctx, sender, req)
  191. }
  192. // GetBannedMembers 获取群组禁言成员
  193. func (*groupApp) GetBannedMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  194. // 查询被禁言的成员列表
  195. group, err := repo.GroupRepo.GetForBanned(groupId)
  196. if err != nil {
  197. return nil, err
  198. }
  199. return group.GetMembers(ctx)
  200. }
  201. // SetGroupMemberBanned 设置禁言
  202. func (*groupApp) SetGroupMemberBanned(ctx context.Context, optId, groupId int64, userIds []int64, isAllMemberBanned bool) ([]*pb.GroupMember, error) {
  203. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  204. if err != nil {
  205. return nil, err
  206. }
  207. if groupUser == nil {
  208. return nil, errors.New("未查询到群组用户信息")
  209. }
  210. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  211. return nil, errors.New("非管理员操作")
  212. }
  213. group, err := repo.GroupRepo.Get(groupId)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if isAllMemberBanned {
  218. //设置全员禁言
  219. //1、更新 `group` 的 is_all_member_banned 状态
  220. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
  221. err := repo.GroupRepo.Save(group)
  222. if err != nil {
  223. return nil, err
  224. }
  225. //2、发送推送消息
  226. err = group.PushGroupMemberBanned(ctx, optId, 0, isAllMemberBanned)
  227. if err != nil {
  228. return nil, err
  229. }
  230. } else {
  231. //设置成员禁言
  232. //1、更新 `group` 的 is_all_member_banned 状态
  233. if group.IsAllMemberBanned != int32(pb.AllMemberBannedType_NOT_All_Member_Banned) {
  234. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_NOT_All_Member_Banned)
  235. err := repo.GroupRepo.Save(group)
  236. if err != nil {
  237. return nil, err
  238. }
  239. //TODO::推送消息
  240. err = group.PushGroupMemberBanned(ctx, optId, -1, isAllMemberBanned)
  241. if err != nil {
  242. return nil, err
  243. }
  244. }
  245. //2、更新 `group_user` 的 status 状态
  246. if len(userIds) == 0 {
  247. 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}})
  248. } else {
  249. 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}})
  250. 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}})
  251. }
  252. //3、删除缓存
  253. err := repo.GroupCache.Del(groupId)
  254. if err != nil {
  255. return nil, err
  256. }
  257. //4、发送推送消息
  258. for _, u := range userIds {
  259. err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  260. if err != nil {
  261. return nil, err
  262. }
  263. }
  264. }
  265. // 查询被禁言的成员列表
  266. group, err = repo.GroupRepo.GetForBanned(groupId)
  267. if err != nil {
  268. return nil, err
  269. }
  270. return group.GetMembers(ctx)
  271. }
  272. // SetGroupMemberRemoveBanned 设置取消禁言
  273. func (*groupApp) SetGroupMemberRemoveBanned(ctx context.Context, optId, groupId int64, removeUserIds []int64) error {
  274. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  275. if err != nil {
  276. return err
  277. }
  278. if groupUser == nil {
  279. return errors.New("未查询到群组用户信息")
  280. }
  281. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  282. return errors.New("非管理员操作")
  283. }
  284. group, err := repo.GroupRepo.Get(groupId)
  285. if err != nil {
  286. return err
  287. }
  288. if len(removeUserIds) > 0 {
  289. //1、更新 `group_user` 的 status 状态
  290. 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}})
  291. //2、删除缓存
  292. err := repo.GroupCache.Del(groupId)
  293. if err != nil {
  294. return err
  295. }
  296. //3、给取消禁言用户推送消息
  297. for _, u := range removeUserIds {
  298. err = group.PushGroupMemberRemoveBanned(ctx, optId, u)
  299. if err != nil {
  300. return err
  301. }
  302. }
  303. }
  304. return nil
  305. }
  306. // RecallSendMessage 撤回发送消息
  307. func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  308. group, err := repo.GroupRepo.Get(req.ReceiverId)
  309. if err != nil {
  310. return 0, err
  311. }
  312. return group.RecallSendMessage(ctx, sender, req)
  313. }
  314. // SendRedPackage 发送红包消息
  315. func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  316. group, err := repo.GroupRepo.Get(req.ReceiverId)
  317. if err != nil {
  318. return 0, err
  319. }
  320. return group.SendRedPackage(ctx, sender, req)
  321. }
  322. // SetGroupAddFriend 设置是否加好友
  323. func (*groupApp) SetGroupAddFriend(ctx context.Context, optId int64, req *pb.SetGroupAddFriendReq) error {
  324. //1、判断是否为管理员操作
  325. groupUser, err := repo.GroupUserRepo.Get(req.GroupId, optId)
  326. if err != nil {
  327. return err
  328. }
  329. if groupUser == nil {
  330. return errors.New("未查询到群组用户信息")
  331. }
  332. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  333. return errors.New("非管理员操作")
  334. }
  335. //group, err := repo.GroupRepo.Get(req.GroupId)
  336. //if err != nil {
  337. // return err
  338. //}
  339. //2、更改群组信息
  340. db.DB.Model(model.Group{}).Where("id = ?", req.GroupId).Updates(model.Group{IsAllAddFriend: int32(req.IsAllAddFriend)})
  341. //3、删除缓存
  342. err = repo.GroupCache.Del(req.GroupId)
  343. if err != nil {
  344. return err
  345. }
  346. ////4、发送推送消息
  347. //for _, u := range userIds {
  348. // err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  349. // if err != nil {
  350. // return nil, err
  351. // }
  352. //}
  353. return nil
  354. }