|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- package app
-
- import (
- "context"
- "database/sql"
- "errors"
- "gim/internal/logic/domain/group/model"
- "gim/internal/logic/domain/group/repo"
- "gim/pkg/db"
- "gim/pkg/pb"
- )
-
- type groupApp struct{}
-
- var GroupApp = new(groupApp)
-
- // CreateGroup 创建群组
- func (*groupApp) CreateGroup(ctx context.Context, userId int64, in *pb.CreateGroupReq) (int64, error) {
- group := model.CreateGroup(userId, in)
- err := repo.GroupRepo.Save(group)
- if err != nil {
- return 0, err
- }
- return group.Id, nil
- }
-
- // GetGroup 获取群组信息
- func (*groupApp) GetGroup(ctx context.Context, groupId int64) (*pb.Group, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
-
- return group.ToProto(), nil
- }
-
- // GetUserGroups 获取用户加入的群组列表
- func (*groupApp) GetUserGroups(ctx context.Context, userId int64) ([]*pb.Group, error) {
- groups, err := repo.GroupUserRepo.ListByUserId(userId)
- if err != nil {
- return nil, err
- }
-
- pbGroups := make([]*pb.Group, len(groups))
- for i := range groups {
- pbGroups[i] = groups[i].ToProto()
- }
- return pbGroups, nil
- }
-
- // Update 更新群组
- func (*groupApp) Update(ctx context.Context, userId int64, update *pb.UpdateGroupReq) error {
- group, err := repo.GroupRepo.Get(update.GroupId)
- if err != nil {
- return err
- }
- var isUpdateIntroduction = false
- if group.Introduction != update.Introduction {
- isUpdateIntroduction = true
- }
- err = group.Update(ctx, update)
- if err != nil {
- return err
- }
-
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
-
- err = group.PushUpdate(ctx, userId, isUpdateIntroduction)
- if err != nil {
- return err
- }
-
- return nil
- }
-
- // AddMembers 添加群组成员
- func (*groupApp) AddMembers(ctx context.Context, userId, groupId int64, userIds []int64) ([]int64, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
- existIds, addedIds, err := group.AddMembers(ctx, userIds)
- if err != nil {
- return nil, err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return nil, err
- }
-
- err = group.PushAddMember(ctx, userId, addedIds)
- if err != nil {
- return nil, err
- }
- return existIds, nil
- }
-
- // UpdateMember 更新群组用户
- func (*groupApp) UpdateMember(ctx context.Context, in *pb.UpdateGroupMemberReq) error {
- group, err := repo.GroupRepo.Get(in.GroupId)
- if err != nil {
- return err
- }
- err = group.UpdateMember(ctx, in)
- if err != nil {
- return err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
- return nil
- }
-
- // DeleteMember 删除群组成员
- func (*groupApp) DeleteMember(ctx context.Context, groupId int64, userId int64, optId int64) error {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return err
- }
- err = group.DeleteMember(ctx, userId)
- if err != nil {
- return err
- }
- err = repo.GroupRepo.Save(group)
- if err != nil {
- return err
- }
-
- err = group.PushDeleteMember(ctx, optId, userId)
- if err != nil {
- return err
- }
- return nil
- }
-
- // GetMembers 获取群组成员
- func (*groupApp) GetMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
- return group.GetMembers(ctx)
- }
-
- // SendMessage 发送群组消息
- func (*groupApp) SendMessage(ctx context.Context, sender *pb.Sender, req *pb.SendMessageReq) (int64, error) {
- group, err := repo.GroupRepo.Get(req.ReceiverId)
- if err != nil {
- return 0, err
- }
-
- return group.SendMessage(ctx, sender, req)
- }
-
- // GetBannedMembers 获取群组禁言成员
- func (*groupApp) GetBannedMembers(ctx context.Context, groupId int64) ([]*pb.GroupMember, error) {
- // 查询被禁言的成员列表
- group, err := repo.GroupRepo.GetForBanned(groupId)
- if err != nil {
- return nil, err
- }
- return group.GetMembers(ctx)
- }
-
- // SetGroupMemberBanned 设置禁言
- func (*groupApp) SetGroupMemberBanned(ctx context.Context, optId, groupId int64, userIds []int64, isAllMemberBanned bool) ([]*pb.GroupMember, error) {
- groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
- if err != nil {
- return nil, err
- }
- if groupUser == nil {
- return nil, errors.New("未查询到群组用户信息")
- }
- if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) {
- return nil, errors.New("非管理员操作")
- }
-
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return nil, err
- }
- if isAllMemberBanned {
- //设置全员禁言
- //1、更新 `group` 的 is_all_member_banned 状态
- group.IsAllMemberBanned = int32(pb.AllMemberBannedType_YES_All_Member_Banned)
- err := repo.GroupRepo.Save(group)
- if err != nil {
- return nil, err
- }
-
- //2、发送推送消息
- err = group.PushGroupMemberBanned(ctx, optId, 0, isAllMemberBanned)
- if err != nil {
- return nil, err
- }
- } else {
- //设置成员禁言
- //1、更新 `group` 的 is_all_member_banned 状态
- group.IsAllMemberBanned = int32(pb.AllMemberBannedType_NOT_All_Member_Banned)
- err := repo.GroupRepo.Save(group)
- if err != nil {
- return nil, err
- }
-
- //2、更新 `group_user` 的 status 状态
- if len(userIds) == 0 {
- 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}})
- } else {
- 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}})
- 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}})
- }
-
- //3、发送推送消息
- for _, u := range userIds {
- err = group.PushGroupMemberBanned(ctx, optId, u, isAllMemberBanned)
- if err != nil {
- return nil, err
- }
- }
- }
-
- // 查询被禁言的成员列表
- group, err = repo.GroupRepo.GetForBanned(groupId)
- if err != nil {
- return nil, err
- }
- return group.GetMembers(ctx)
- }
-
- // SetGroupMemberRemoveBanned 设置取消禁言
- func (*groupApp) SetGroupMemberRemoveBanned(ctx context.Context, optId, groupId int64, removeUserIds []int64) error {
- groupUser, err := repo.GroupUserRepo.Get(groupId, optId)
- if err != nil {
- return err
- }
- if groupUser == nil {
- return errors.New("未查询到群组用户信息")
- }
- if groupUser.MemberType != int(pb.MemberType_GMT_ADMIN) {
- return errors.New("非管理员操作")
- }
-
- group, err := repo.GroupRepo.Get(groupId)
- if err != nil {
- return err
- }
-
- if len(removeUserIds) > 0 {
- //1、更新 `group_user` 的 status 状态
- 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}})
-
- //2、给取消禁言用户推送消息
- for _, u := range removeUserIds {
- err = group.PushGroupMemberRemoveBanned(ctx, optId, u)
- if err != nil {
- return err
- }
- }
- }
-
- return nil
- }
-
- // RecallSendMessage 撤回发送消息
- func (*groupApp) RecallSendMessage(ctx context.Context, sender *pb.Sender, req *pb.RecallMessageReq) (int64, error) {
- group, err := repo.GroupRepo.Get(req.ReceiverId)
- if err != nil {
- return 0, err
- }
-
- return group.RecallSendMessage(ctx, sender, req)
- }
-
- // SendRedPackage 发送红包消息
- func (*groupApp) SendRedPackage(ctx context.Context, sender *pb.Sender, req *pb.SendRedPacketReq) (int64, error) {
- group, err := repo.GroupRepo.Get(req.ReceiverId)
- if err != nil {
- return 0, err
- }
-
- return group.SendRedPackage(ctx, sender, req)
- }
|