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.
 
 
 
 

299 Zeilen
7.9 KiB

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