Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

404 rader
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 group.Kind == int32(pb.GroupKindType_CREATE_SYSTEM) && !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 pbGroups[i].Kind == pb.GroupKindType_CREATE_SYSTEM && !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)) > 1000 {
  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, userIds []int64, optId int64) error {
  146. group, err := repo.GroupRepo.Get(groupId)
  147. if err != nil {
  148. return err
  149. }
  150. for _, userId := range userIds {
  151. err = group.DeleteMember(ctx, userId)
  152. if err != nil {
  153. return err
  154. }
  155. group.UserNum-- //群组人数减一
  156. err = repo.GroupRepo.Save(group)
  157. if err != nil {
  158. return err
  159. }
  160. if userId == optId { //自己退出群聊,无需发送消息
  161. return nil
  162. }
  163. err = group.PushDeleteMember(ctx, optId, userId)
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. return nil
  169. }
  170. // GetMembers 获取群组成员
  171. func (*groupApp) GetMembers(ctx context.Context, groupId, limit int64) ([]*pb.GroupMember, error) {
  172. if limit == 0 {
  173. group, err := repo.GroupRepo.Get(groupId)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return group.GetMembers(ctx)
  178. } else {
  179. group, err := repo.GroupRepo.GetLimit(groupId, limit)
  180. if err != nil {
  181. return nil, err
  182. }
  183. return group.GetMembers(ctx)
  184. }
  185. }
  186. // SendMessage 发送群组消息
  187. func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  188. group, err := repo.GroupRepo.Get(req.ReceiverId)
  189. if err != nil {
  190. return 0, err
  191. }
  192. return group.SendMessage(ctx, sender, req)
  193. }
  194. // GetBannedMembers 获取群组禁言成员
  195. func (*groupApp) GetBannedMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  196. // 查询被禁言的成员列表
  197. group, err := repo.GroupRepo.GetForBanned(groupId)
  198. if err != nil {
  199. return nil, err
  200. }
  201. return group.GetMembers(ctx)
  202. }
  203. // SetGroupMemberBanned 设置禁言
  204. func (*groupApp) SetGroupMemberBanned(ctx context.Context, optId, groupId int64, userIds []int64, isAllMemberBanned bool) ([]*pb.GroupMember, error) {
  205. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if groupUser == nil {
  210. return nil, errors.New("未查询到群组用户信息")
  211. }
  212. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  213. return nil, errors.New("非管理员操作")
  214. }
  215. group, err := repo.GroupRepo.Get(groupId)
  216. if err != nil {
  217. return nil, err
  218. }
  219. if isAllMemberBanned {
  220. //设置全员禁言
  221. //1、更新 `group` 的 is_all_member_banned 状态
  222. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
  223. err := repo.GroupRepo.Save(group)
  224. if err != nil {
  225. return nil, err
  226. }
  227. //2、发送推送消息
  228. err = group.PushGroupMemberBanned(ctx, optId, 0, isAllMemberBanned)
  229. if err != nil {
  230. return nil, err
  231. }
  232. } else {
  233. //设置成员禁言
  234. //1、更新 `group` 的 is_all_member_banned 状态
  235. if group.IsAllMemberBanned != int32(pb.AllMemberBannedType_NOT_All_Member_Banned) {
  236. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_NOT_All_Member_Banned)
  237. err := repo.GroupRepo.Save(group)
  238. if err != nil {
  239. return nil, err
  240. }
  241. //TODO::推送消息
  242. err = group.PushGroupMemberBanned(ctx, optId, -1, isAllMemberBanned)
  243. if err != nil {
  244. return nil, err
  245. }
  246. }
  247. //2、更新 `group_user` 的 status 状态
  248. if len(userIds) == 0 {
  249. 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}})
  250. } else {
  251. 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}})
  252. 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}})
  253. }
  254. //3、删除缓存
  255. err := repo.GroupCache.Del(groupId)
  256. if err != nil {
  257. return nil, err
  258. }
  259. //4、发送推送消息
  260. for _, u := range userIds {
  261. err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  262. if err != nil {
  263. return nil, err
  264. }
  265. }
  266. }
  267. // 查询被禁言的成员列表
  268. group, err = repo.GroupRepo.GetForBanned(groupId)
  269. if err != nil {
  270. return nil, err
  271. }
  272. return group.GetMembers(ctx)
  273. }
  274. // SetGroupMemberRemoveBanned 设置取消禁言
  275. func (*groupApp) SetGroupMemberRemoveBanned(ctx context.Context, optId, groupId int64, removeUserIds []int64) error {
  276. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  277. if err != nil {
  278. return err
  279. }
  280. if groupUser == nil {
  281. return errors.New("未查询到群组用户信息")
  282. }
  283. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  284. return errors.New("非管理员操作")
  285. }
  286. group, err := repo.GroupRepo.Get(groupId)
  287. if err != nil {
  288. return err
  289. }
  290. if len(removeUserIds) > 0 {
  291. //1、更新 `group_user` 的 status 状态
  292. 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}})
  293. //2、删除缓存
  294. err := repo.GroupCache.Del(groupId)
  295. if err != nil {
  296. return err
  297. }
  298. //3、给取消禁言用户推送消息
  299. for _, u := range removeUserIds {
  300. err = group.PushGroupMemberRemoveBanned(ctx, optId, u)
  301. if err != nil {
  302. return err
  303. }
  304. }
  305. }
  306. return nil
  307. }
  308. // RecallSendMessage 撤回发送消息
  309. func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  310. group, err := repo.GroupRepo.Get(req.ReceiverId)
  311. if err != nil {
  312. return 0, err
  313. }
  314. return group.RecallSendMessage(ctx, sender, req)
  315. }
  316. // SendRedPackage 发送红包消息
  317. func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  318. group, err := repo.GroupRepo.Get(req.ReceiverId)
  319. if err != nil {
  320. return 0, err
  321. }
  322. return group.SendRedPackage(ctx, sender, req)
  323. }
  324. // SetGroupAddFriend 设置是否加好友
  325. func (*groupApp) SetGroupAddFriend(ctx context.Context, optId int64, req *pb.SetGroupAddFriendReq) error {
  326. //1、判断是否为管理员操作
  327. groupUser, err := repo.GroupUserRepo.Get(req.GroupId, optId)
  328. if err != nil {
  329. return err
  330. }
  331. if groupUser == nil {
  332. return errors.New("未查询到群组用户信息")
  333. }
  334. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  335. return errors.New("非管理员操作")
  336. }
  337. //group, err := repo.GroupRepo.Get(req.GroupId)
  338. //if err != nil {
  339. // return err
  340. //}
  341. //2、更改群组信息
  342. db.DB.Model(model.Group{}).Where("id = ?", req.GroupId).Updates(model.Group{IsAllAddFriend: int32(req.IsAllAddFriend)})
  343. //3、删除缓存
  344. err = repo.GroupCache.Del(req.GroupId)
  345. if err != nil {
  346. return err
  347. }
  348. ////4、发送推送消息
  349. //for _, u := range userIds {
  350. // err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  351. // if err != nil {
  352. // return nil, err
  353. // }
  354. //}
  355. return nil
  356. }