mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-06-15 07:09:26 +08:00
CreateGroup rpc
This commit is contained in:
parent
74f5663963
commit
e0241d73de
@ -115,27 +115,21 @@ func (s *groupServer) Run() {
|
|||||||
log.NewInfo("", "group rpc success")
|
log.NewInfo("", "group rpc success")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (*pbGroup.CreateGroupResp, error) {
|
func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) (resp *pbGroup.CreateGroupResp, _ error) {
|
||||||
log.NewInfo(req.OperationID, "CreateGroup, args ", req.String())
|
resp = &pbGroup.CreateGroupResp{CommonResp: &open_im_sdk.CommonResp{}}
|
||||||
if !token_verify.CheckAccess(req.OpUserID, req.OwnerUserID) {
|
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
|
||||||
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.OwnerUserID)
|
trace_log.SetContextInfo(ctx, utils.GetSelfFuncName(), nil, "req", req, "resp", resp)
|
||||||
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
|
defer trace_log.ShowLog(ctx)
|
||||||
|
if !token_verify.CheckAccess(ctx, req.OpUserID, req.OwnerUserID) {
|
||||||
|
SetErr(ctx, "CheckAccess", constant.ErrAccess, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
callbackResp := callbackBeforeCreateGroup(req)
|
if err := callbackBeforeCreateGroup(ctx, req); err != nil {
|
||||||
if callbackResp.ErrCode != 0 {
|
//trace_log.SetContextInfo(ctx, "callbackBeforeCreateGroup", err, "req", req)
|
||||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg resp: ", callbackResp)
|
SetErr(ctx, "CallbackBeforeMemberJoinGroup", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "userID", req.OwnerUserID)
|
||||||
|
return
|
||||||
|
//log.NewError(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg resp: ", callbackResp)
|
||||||
}
|
}
|
||||||
if callbackResp.ActionCode != constant.ActionAllow {
|
|
||||||
if callbackResp.ErrCode == 0 {
|
|
||||||
callbackResp.ErrCode = 201
|
|
||||||
}
|
|
||||||
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg result", "end rpc and return", callbackResp)
|
|
||||||
return &pbGroup.CreateGroupResp{
|
|
||||||
ErrCode: int32(callbackResp.ErrCode),
|
|
||||||
ErrMsg: callbackResp.ErrMsg,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
groupId := req.GroupInfo.GroupID
|
groupId := req.GroupInfo.GroupID
|
||||||
if groupId == "" {
|
if groupId == "" {
|
||||||
groupId = utils.Md5(req.OperationID + strconv.FormatInt(time.Now().UnixNano(), 10))
|
groupId = utils.Md5(req.OperationID + strconv.FormatInt(time.Now().UnixNano(), 10))
|
||||||
@ -152,13 +146,10 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
|||||||
if groupInfo.NotificationUpdateTime.Unix() < 0 {
|
if groupInfo.NotificationUpdateTime.Unix() < 0 {
|
||||||
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
|
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
|
||||||
}
|
}
|
||||||
err := imdb.InsertIntoGroup(groupInfo)
|
if err := (*imdb.Group)(nil).Create(ctx, []*imdb.Group{&groupInfo}); err != nil {
|
||||||
if err != nil {
|
SetErr(ctx, "Create", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
|
||||||
log.NewError(req.OperationID, "InsertIntoGroup failed, ", err.Error(), groupInfo)
|
return
|
||||||
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
|
|
||||||
}
|
}
|
||||||
var okUserIDList []string
|
|
||||||
resp := &pbGroup.CreateGroupResp{GroupInfo: &open_im_sdk.GroupInfo{}}
|
|
||||||
groupMember := imdb.GroupMember{}
|
groupMember := imdb.GroupMember{}
|
||||||
us := &imdb.User{}
|
us := &imdb.User{}
|
||||||
if req.OwnerUserID != "" {
|
if req.OwnerUserID != "" {
|
||||||
@ -168,90 +159,68 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
|||||||
}
|
}
|
||||||
userIDList = append(userIDList, req.OwnerUserID)
|
userIDList = append(userIDList, req.OwnerUserID)
|
||||||
if err := s.DelGroupAndUserCache(req.OperationID, "", userIDList); err != nil {
|
if err := s.DelGroupAndUserCache(req.OperationID, "", userIDList); err != nil {
|
||||||
log.NewError(req.OperationID, "DelGroupAndUserCache failed, ", err.Error(), userIDList)
|
SetErr(ctx, "DelGroupAndUserCache", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
|
||||||
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: err.Error()}, nil
|
return
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
us, err = imdb.GetUserByUserID(req.OwnerUserID)
|
us, err = imdb.GetUserByUserID(req.OwnerUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), req.OwnerUserID)
|
SetErr(ctx, "GetUserByUserID", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "userID", req.OwnerUserID)
|
||||||
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
|
return
|
||||||
}
|
}
|
||||||
//to group member
|
//to group member
|
||||||
groupMember = imdb.GroupMember{GroupID: groupId, RoleLevel: constant.GroupOwner, OperatorUserID: req.OpUserID, JoinSource: constant.JoinByInvitation, InviterUserID: req.OpUserID}
|
groupMember = imdb.GroupMember{GroupID: groupId, RoleLevel: constant.GroupOwner, OperatorUserID: req.OpUserID, JoinSource: constant.JoinByInvitation, InviterUserID: req.OpUserID}
|
||||||
utils.CopyStructFields(&groupMember, us)
|
utils.CopyStructFields(&groupMember, us)
|
||||||
callbackResp := CallbackBeforeMemberJoinGroup(req.OperationID, &groupMember, groupInfo.Ex)
|
if err := CallbackBeforeMemberJoinGroup(ctx, req.OperationID, &groupMember, groupInfo.Ex); err != nil {
|
||||||
if callbackResp.ErrCode != 0 {
|
SetErr(ctx, "CallbackBeforeMemberJoinGroup", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "userID", req.OwnerUserID)
|
||||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup resp: ", callbackResp)
|
return
|
||||||
}
|
}
|
||||||
if callbackResp.ActionCode != constant.ActionAllow {
|
if err := (*imdb.GroupMember)(nil).Create(ctx, []*imdb.GroupMember{&groupMember}); err != nil {
|
||||||
if callbackResp.ErrCode == 0 {
|
SetErr(ctx, "Create", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "userID", req.OwnerUserID, "args", groupMember)
|
||||||
callbackResp.ErrCode = 201
|
return
|
||||||
}
|
|
||||||
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "CallbackBeforeMemberJoinGroup result", "end rpc and return", callbackResp)
|
|
||||||
return &pbGroup.CreateGroupResp{
|
|
||||||
ErrCode: int32(callbackResp.ErrCode),
|
|
||||||
ErrMsg: callbackResp.ErrMsg,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err = imdb.InsertIntoGroupMember(groupMember)
|
|
||||||
if err != nil {
|
|
||||||
log.NewError(req.OperationID, "InsertIntoGroupMember failed ", err.Error(), groupMember)
|
|
||||||
return &pbGroup.CreateGroupResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var okUserIDList []string
|
||||||
if req.GroupInfo.GroupType != constant.SuperGroup {
|
if req.GroupInfo.GroupType != constant.SuperGroup {
|
||||||
//to group member
|
//to group member
|
||||||
|
var groupMembers []*imdb.GroupMember
|
||||||
for _, user := range req.InitMemberList {
|
for _, user := range req.InitMemberList {
|
||||||
us, err := rocksCache.GetUserInfoFromCache(user.UserID)
|
us, err := rocksCache.GetUserInfoFromCache(user.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetUserByUserID failed ", err.Error(), user.UserID)
|
trace_log.SetContextInfo(ctx, "GetUserInfoFromCache", err, "userID", user.UserID)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if user.RoleLevel == constant.GroupOwner {
|
if user.RoleLevel == constant.GroupOwner {
|
||||||
log.NewError(req.OperationID, "only one owner, failed ", user)
|
trace_log.SetContextInfo(ctx, "GetUserInfoFromCache", nil, "userID", user.UserID, "msg", "only one owner, failed ")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
groupMember.RoleLevel = user.RoleLevel
|
groupMember.RoleLevel = user.RoleLevel
|
||||||
groupMember.JoinSource = constant.JoinByInvitation
|
groupMember.JoinSource = constant.JoinByInvitation
|
||||||
groupMember.InviterUserID = req.OpUserID
|
groupMember.InviterUserID = req.OpUserID
|
||||||
utils.CopyStructFields(&groupMember, us)
|
utils.CopyStructFields(&groupMember, us)
|
||||||
callbackResp := CallbackBeforeMemberJoinGroup(req.OperationID, &groupMember, groupInfo.Ex)
|
if err := CallbackBeforeMemberJoinGroup(ctx, req.OperationID, &groupMember, groupInfo.Ex); err != nil {
|
||||||
if callbackResp.ErrCode != 0 {
|
SetErr(ctx, "CallbackBeforeMemberJoinGroup", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "groupMember", groupMember, "groupEx", groupInfo.Ex)
|
||||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg resp: ", callbackResp)
|
return
|
||||||
}
|
|
||||||
if callbackResp.ActionCode != constant.ActionAllow {
|
|
||||||
if callbackResp.ErrCode == 0 {
|
|
||||||
callbackResp.ErrCode = 201
|
|
||||||
}
|
|
||||||
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg result", "end rpc and return", callbackResp)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
err = imdb.InsertIntoGroupMember(groupMember)
|
|
||||||
if err != nil {
|
|
||||||
log.NewError(req.OperationID, "InsertIntoGroupMember failed ", err.Error(), groupMember)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
groupMembers = append(groupMembers, &groupMember)
|
||||||
okUserIDList = append(okUserIDList, user.UserID)
|
okUserIDList = append(okUserIDList, user.UserID)
|
||||||
}
|
}
|
||||||
group, err := rocksCache.GetGroupInfoFromCache(groupId)
|
if err := (*imdb.GroupMember)(nil).Create(ctx, groupMembers); err != nil {
|
||||||
|
SetErr(ctx, "Create", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "groupMembers", groupMembers)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
group, err := rocksCache.GetGroupInfoFromCache(ctx, groupId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetGroupInfoByGroupID failed ", err.Error(), groupId)
|
SetErr(ctx, "GetGroupInfoFromCache", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "groupID", groupId)
|
||||||
resp.ErrCode = constant.ErrDB.ErrCode
|
return
|
||||||
resp.ErrMsg = err.Error()
|
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
utils.CopyStructFields(resp.GroupInfo, group)
|
utils.CopyStructFields(resp.GroupInfo, group)
|
||||||
memberCount, err := rocksCache.GetGroupMemberNumFromCache(groupId)
|
memberCount, err := rocksCache.GetGroupMemberNumFromCache(groupId)
|
||||||
resp.GroupInfo.MemberCount = uint32(memberCount)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetGroupMemberNumByGroupID failed ", err.Error(), groupId)
|
SetErr(ctx, "GetGroupMemberNumFromCache", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "groupID", groupId)
|
||||||
resp.ErrCode = constant.ErrDB.ErrCode
|
return
|
||||||
resp.ErrMsg = err.Error()
|
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
|
resp.GroupInfo.MemberCount = uint32(memberCount)
|
||||||
if req.OwnerUserID != "" {
|
if req.OwnerUserID != "" {
|
||||||
resp.GroupInfo.OwnerUserID = req.OwnerUserID
|
resp.GroupInfo.OwnerUserID = req.OwnerUserID
|
||||||
okUserIDList = append(okUserIDList, req.OwnerUserID)
|
okUserIDList = append(okUserIDList, req.OwnerUserID)
|
||||||
@ -262,21 +231,19 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
|||||||
okUserIDList = append(okUserIDList, v.UserID)
|
okUserIDList = append(okUserIDList, v.UserID)
|
||||||
}
|
}
|
||||||
if err := db.DB.CreateSuperGroup(groupId, okUserIDList, len(okUserIDList)); err != nil {
|
if err := db.DB.CreateSuperGroup(groupId, okUserIDList, len(okUserIDList)); err != nil {
|
||||||
log.NewError(req.OperationID, "GetGroupMemberNumByGroupID failed ", err.Error(), groupId)
|
SetErr(ctx, "CreateSuperGroup", err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg, "groupID", groupId, "userIDList", okUserIDList)
|
||||||
resp.ErrCode = constant.ErrDB.ErrCode
|
return
|
||||||
resp.ErrMsg = err.Error() + ": CreateSuperGroup failed"
|
|
||||||
return resp, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(okUserIDList) != 0 {
|
if len(okUserIDList) != 0 {
|
||||||
log.NewInfo(req.OperationID, "rpc CreateGroup return ", resp.String())
|
|
||||||
if req.GroupInfo.GroupType != constant.SuperGroup {
|
if req.GroupInfo.GroupType != constant.SuperGroup {
|
||||||
chat.GroupCreatedNotification(req.OperationID, req.OpUserID, groupId, okUserIDList)
|
chat.GroupCreatedNotification(req.OperationID, req.OpUserID, groupId, okUserIDList)
|
||||||
} else {
|
} else {
|
||||||
for _, userID := range okUserIDList {
|
for _, userID := range okUserIDList {
|
||||||
if err := rocksCache.DelJoinedSuperGroupIDListFromCache(userID); err != nil {
|
if err := rocksCache.DelJoinedSuperGroupIDListFromCache(userID); err != nil {
|
||||||
log.NewWarn(req.OperationID, utils.GetSelfFuncName(), userID, err.Error())
|
trace_log.SetContextInfo(ctx, "DelJoinedSuperGroupIDListFromCache", err, "userID", userID)
|
||||||
|
//log.NewWarn(req.OperationID, utils.GetSelfFuncName(), userID, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
@ -285,10 +252,10 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
return resp, nil
|
return
|
||||||
} else {
|
} else {
|
||||||
log.NewInfo(req.OperationID, "rpc CreateGroup return ", resp.String())
|
//log.NewInfo(req.OperationID, "rpc CreateGroup return ", resp.String())
|
||||||
return resp, nil
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,6 +265,7 @@ func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJo
|
|||||||
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.FromUserID)
|
log.NewError(req.OperationID, "CheckAccess false ", req.OpUserID, req.FromUserID)
|
||||||
return &pbGroup.GetJoinedGroupListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
|
return &pbGroup.GetJoinedGroupListResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
|
||||||
}
|
}
|
||||||
|
utils.GetFuncName(1)
|
||||||
|
|
||||||
joinedGroupList, err := rocksCache.GetJoinedGroupIDListFromCache(req.FromUserID)
|
joinedGroupList, err := rocksCache.GetJoinedGroupIDListFromCache(req.FromUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -943,7 +911,7 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
|
|||||||
member.InviterUserID = request.InviterUserID
|
member.InviterUserID = request.InviterUserID
|
||||||
member.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
member.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
||||||
err = CallbackBeforeMemberJoinGroup(ctx, req.OperationID, &member, groupInfo.Ex)
|
err = CallbackBeforeMemberJoinGroup(ctx, req.OperationID, &member, groupInfo.Ex)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
|
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user