diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 46792cb93..192939c59 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -197,6 +197,11 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR log.NewInfo(req.OperationID, "rpc CreateGroup return ", resp.String()) if req.GroupInfo.GroupType != constant.SuperGroup { chat.GroupCreatedNotification(req.OperationID, req.OpUserID, groupId, okUserIDList) + } else { + for _, userID := range okUserIDList { + chat.SuperGroupNotification(req.OperationID, req.OpUserID, userID) + } + } return resp, nil } else { @@ -377,6 +382,10 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite if groupInfo.GroupType != constant.SuperGroup { chat.MemberInvitedNotification(req.OperationID, req.GroupID, req.OpUserID, req.Reason, okUserIDList) + } else { + for _, userID := range okUserIDList { + chat.SuperGroupNotification(req.OperationID, req.OpUserID, userID) + } } log.NewInfo(req.OperationID, "InviteUserToGroup rpc return ") @@ -562,6 +571,10 @@ func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGrou } if groupInfo.GroupType != constant.SuperGroup { chat.MemberKickedNotification(req, okUserIDList) + } else { + for _, userID := range okUserIDList { + chat.SuperGroupNotification(req.OperationID, req.OpUserID, userID) + } } log.NewInfo(req.OperationID, "GetGroupMemberList rpc return ", resp.String()) return &resp, nil diff --git a/internal/rpc/group/super_group.go b/internal/rpc/group/super_group.go index 9819664ef..675c14fd6 100644 --- a/internal/rpc/group/super_group.go +++ b/internal/rpc/group/super_group.go @@ -1,20 +1,38 @@ package group import ( + "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" + imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model" "Open_IM/pkg/common/log" pbGroup "Open_IM/pkg/proto/group" + commonPb "Open_IM/pkg/proto/sdk_ws" "Open_IM/pkg/utils" "context" ) func (s *groupServer) GetJoinedSuperGroupList(ctx context.Context, req *pbGroup.GetJoinedSuperGroupListReq) (*pbGroup.GetJoinedSuperGroupListResp, error) { log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String()) - resp := &pbGroup.GetJoinedSuperGroupListResp{} - _, err := db.DB.GetSuperGroupByUserID(req.UserID) + resp := &pbGroup.GetJoinedSuperGroupListResp{CommonResp: &pbGroup.CommonResp{}} + userToSuperGroup, err := db.DB.GetSuperGroupByUserID(req.UserID) if err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetSuperGroupByUserID failed", err.Error()) + resp.CommonResp.ErrCode = constant.ErrDB.ErrCode + resp.CommonResp.ErrMsg = constant.ErrDB.ErrMsg return resp, nil } + for _, groupID := range userToSuperGroup.GroupIDList { + groupInfoDB, err := imdb.GetGroupInfoByGroupID(groupID) + if err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupInfoByGroupID failed", groupID, err.Error()) + continue + } + groupInfo := &commonPb.GroupInfo{} + if err := utils.CopyStructFields(groupInfo, groupInfoDB); err != nil { + log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error()) + } + resp.GroupList = append(resp.GroupList, groupInfo) + } log.NewError(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String()) return resp, nil } diff --git a/internal/rpc/msg/super_group_notification.go b/internal/rpc/msg/super_group_notification.go new file mode 100644 index 000000000..56fad1ebd --- /dev/null +++ b/internal/rpc/msg/super_group_notification.go @@ -0,0 +1,36 @@ +package msg + +import ( + "Open_IM/pkg/common/constant" + "Open_IM/pkg/common/log" + sdk "Open_IM/pkg/proto/sdk_ws" + "Open_IM/pkg/utils" + "github.com/golang/protobuf/jsonpb" + "github.com/golang/protobuf/proto" +) + +func SuperGroupNotification(operationID, sendID, recvID string) { + //var tips sdk.TipsComm + //var err error + //marshaler := jsonpb.Marshaler{ + // OrigName: true, + // EnumsAsInts: false, + // EmitDefaults: false, + //} + //tips.JsonDetail, _ = marshaler.MarshalToString(m) + n := &NotificationMsg{ + SendID: sendID, + RecvID: recvID, + MsgFrom: constant.UserMsgType, + ContentType: constant.SuperGroupUpdateNotification, + SessionType: constant.SingleChatType, + OperationID: operationID, + } + //n.Content, err = proto.Marshal(&tips) + //if err != nil { + // log.NewError(operationID, utils.GetSelfFuncName(), "proto.Marshal failed") + // return + //} + log.NewInfo(operationID, utils.GetSelfFuncName(), string(n.Content)) + Notification(n) +} diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 05906924a..ea650cb4c 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -94,7 +94,10 @@ const ( WorkMomentNotificationBegin = 1900 WorkMomentNotification = 1901 - NotificationEnd = 2000 + SuperGroupUpdateNotificationBegin = 2000 + SuperGroupUpdateNotification = 2001 + + NotificationEnd = 3000 //status MsgNormal = 1 diff --git a/pkg/common/db/mongoModel.go b/pkg/common/db/mongoModel.go index d96c1494a..5915e42af 100644 --- a/pkg/common/db/mongoModel.go +++ b/pkg/common/db/mongoModel.go @@ -895,11 +895,36 @@ type UserToSuperGroup struct { func (d *DataBases) CreateSuperGroup(groupID string, initMemberIDList []string, memberNumCount int) error { ctx, _ := context.WithTimeout(context.Background(), time.Duration(config.Config.Mongo.DBTimeout)*time.Second) c := d.mongoClient.Database(config.Config.Mongo.DBDatabase).Collection(cSuperGroup) + session, err := d.mongoClient.StartSession() + if err != nil { + return utils.Wrap(err, "start session failed") + } + defer session.EndSession(ctx) + sCtx := mongo.NewSessionContext(ctx, session) + if err != nil { + return utils.Wrap(err, "start transaction failed") + } superGroup := SuperGroup{ GroupID: groupID, MemberIDList: initMemberIDList, } - _, err := c.InsertOne(ctx, superGroup) + _, err = c.InsertOne(sCtx, superGroup) + if err != nil { + session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + var users []UserToSuperGroup + for _, v := range initMemberIDList { + users = append(users, UserToSuperGroup{ + UserID: v, + }) + } + _, err = c.UpdateOne(sCtx, users, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}) + if err != nil { + session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } + session.CommitTransaction(ctx) return err } @@ -954,6 +979,10 @@ func (d *DataBases) RemoverUserFromSuperGroup(groupID string, userIDList []strin defer session.EndSession(ctx) sCtx := mongo.NewSessionContext(ctx, session) _, err = c.UpdateOne(ctx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDList}}}) + if err != nil { + session.AbortTransaction(ctx) + return utils.Wrap(err, "transaction failed") + } err = d.RemoveGroupFromUser(ctx, sCtx, groupID, userIDList) if err != nil { session.AbortTransaction(ctx)