golang-im聊天
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

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