golang-im聊天
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

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