golang-im聊天
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

428 Zeilen
12 KiB

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