golang-im聊天
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

365 lignes
9.9 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. }
  84. return pbGroups, nil
  85. }
  86. // Update 更新群组
  87. func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
  88. group, err := repo.GroupRepo.Get(update.GroupId)
  89. if err != nil {
  90. return err
  91. }
  92. var isUpdateIntroduction = false
  93. if group.Introduction != update.Introduction {
  94. isUpdateIntroduction = true
  95. }
  96. err = group.Update(ctx, update)
  97. if err != nil {
  98. return err
  99. }
  100. err = repo.GroupRepo.Save(group)
  101. if err != nil {
  102. return err
  103. }
  104. err = group.PushUpdate(ctx, userId, isUpdateIntroduction)
  105. if err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. // AddMembers 添加群组成员
  111. func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
  112. group, err := repo.GroupRepo.Get(groupId)
  113. if err != nil {
  114. return nil, err
  115. }
  116. existIds, addedIds, err := group.AddMembers(ctx, userIds)
  117. if err != nil {
  118. return nil, err
  119. }
  120. err = repo.GroupRepo.Save(group)
  121. if err != nil {
  122. return nil, err
  123. }
  124. err = group.PushAddMember(ctx, userId, addedIds, true)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return existIds, nil
  129. }
  130. // UpdateMember 更新群组用户
  131. func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq, optId int64) error {
  132. group, err := repo.GroupRepo.Get(in.GroupId)
  133. if err != nil {
  134. return err
  135. }
  136. err = group.UpdateMember(ctx, in)
  137. if err != nil {
  138. return err
  139. }
  140. err = repo.GroupRepo.Save(group)
  141. if err != nil {
  142. return err
  143. }
  144. err = group.PushUpdateMember(ctx, optId, in.UserId, int32(in.MemberType))
  145. if err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. // DeleteMember 删除群组成员
  151. func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
  152. group, err := repo.GroupRepo.Get(groupId)
  153. if err != nil {
  154. return err
  155. }
  156. err = group.DeleteMember(ctx, userId)
  157. if err != nil {
  158. return err
  159. }
  160. group.UserNum-- //群组人数减一
  161. err = repo.GroupRepo.Save(group)
  162. if err != nil {
  163. return err
  164. }
  165. err = group.PushDeleteMember(ctx, optId, userId)
  166. if err != nil {
  167. return err
  168. }
  169. return nil
  170. }
  171. // GetMembers 获取群组成员
  172. func (*groupApp) GetMembers(ctx context.Context, groupId, limit int64) ([]*pb.GroupMember, error) {
  173. if limit == 0 {
  174. group, err := repo.GroupRepo.Get(groupId)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return group.GetMembers(ctx)
  179. } else {
  180. group, err := repo.GroupRepo.GetLimit(groupId, limit)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return group.GetMembers(ctx)
  185. }
  186. }
  187. // SendMessage 发送群组消息
  188. func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
  189. group, err := repo.GroupRepo.Get(req.ReceiverId)
  190. if err != nil {
  191. return 0, err
  192. }
  193. return group.SendMessage(ctx, sender, req)
  194. }
  195. // GetBannedMembers 获取群组禁言成员
  196. func (*groupApp) GetBannedMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
  197. // 查询被禁言的成员列表
  198. group, err := repo.GroupRepo.GetForBanned(groupId)
  199. if err != nil {
  200. return nil, err
  201. }
  202. return group.GetMembers(ctx)
  203. }
  204. // SetGroupMemberBanned 设置禁言
  205. func (*groupApp) SetGroupMemberBanned(ctx context.Context, optId, groupId int64, userIds []int64, isAllMemberBanned bool) ([]*pb.GroupMember, error) {
  206. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  207. if err != nil {
  208. return nil, err
  209. }
  210. if groupUser == nil {
  211. return nil, errors.New("未查询到群组用户信息")
  212. }
  213. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  214. return nil, errors.New("非管理员操作")
  215. }
  216. group, err := repo.GroupRepo.Get(groupId)
  217. if err != nil {
  218. return nil, err
  219. }
  220. if isAllMemberBanned {
  221. //设置全员禁言
  222. //1、更新 `group` 的 is_all_member_banned 状态
  223. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
  224. err := repo.GroupRepo.Save(group)
  225. if err != nil {
  226. return nil, err
  227. }
  228. //2、发送推送消息
  229. err = group.PushGroupMemberBanned(ctx, optId, 0, isAllMemberBanned)
  230. if err != nil {
  231. return nil, err
  232. }
  233. } else {
  234. //设置成员禁言
  235. //1、更新 `group` 的 is_all_member_banned 状态
  236. if group.IsAllMemberBanned != int32(pb.AllMemberBannedType_NOT_All_Member_Banned) {
  237. group.IsAllMemberBanned = int32(pb.AllMemberBannedType_NOT_All_Member_Banned)
  238. err := repo.GroupRepo.Save(group)
  239. if err != nil {
  240. return nil, err
  241. }
  242. //TODO::推送消息
  243. err = group.PushGroupMemberBanned(ctx, optId, -1, isAllMemberBanned)
  244. if err != nil {
  245. return nil, err
  246. }
  247. }
  248. //2、更新 `group_user` 的 status 状态
  249. if len(userIds) == 0 {
  250. 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}})
  251. } else {
  252. 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}})
  253. 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}})
  254. }
  255. //3、删除缓存
  256. err := repo.GroupCache.Del(groupId)
  257. if err != nil {
  258. return nil, err
  259. }
  260. //4、发送推送消息
  261. for _, u := range userIds {
  262. err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
  263. if err != nil {
  264. return nil, err
  265. }
  266. }
  267. }
  268. // 查询被禁言的成员列表
  269. group, err = repo.GroupRepo.GetForBanned(groupId)
  270. if err != nil {
  271. return nil, err
  272. }
  273. return group.GetMembers(ctx)
  274. }
  275. // SetGroupMemberRemoveBanned 设置取消禁言
  276. func (*groupApp) SetGroupMemberRemoveBanned(ctx context.Context, optId, groupId int64, removeUserIds []int64) error {
  277. groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
  278. if err != nil {
  279. return err
  280. }
  281. if groupUser == nil {
  282. return errors.New("未查询到群组用户信息")
  283. }
  284. if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) && groupUser.MemberType != int(pb.MemberType_GMT_MANAGE) {
  285. return errors.New("非管理员操作")
  286. }
  287. group, err := repo.GroupRepo.Get(groupId)
  288. if err != nil {
  289. return err
  290. }
  291. if len(removeUserIds) > 0 {
  292. //1、更新 `group_user` 的 status 状态
  293. 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}})
  294. //2、删除缓存
  295. err := repo.GroupCache.Del(groupId)
  296. if err != nil {
  297. return err
  298. }
  299. //3、给取消禁言用户推送消息
  300. for _, u := range removeUserIds {
  301. err = group.PushGroupMemberRemoveBanned(ctx, optId, u)
  302. if err != nil {
  303. return err
  304. }
  305. }
  306. }
  307. return nil
  308. }
  309. // RecallSendMessage 撤回发送消息
  310. func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
  311. group, err := repo.GroupRepo.Get(req.ReceiverId)
  312. if err != nil {
  313. return 0, err
  314. }
  315. return group.RecallSendMessage(ctx, sender, req)
  316. }
  317. // SendRedPackage 发送红包消息
  318. func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
  319. group, err := repo.GroupRepo.Get(req.ReceiverId)
  320. if err != nil {
  321. return 0, err
  322. }
  323. return group.SendRedPackage(ctx, sender, req)
  324. }