From 87d0e296c11c1cb0d1895693352778baea9943a9 Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Tue, 17 Jan 2023 15:28:25 +0800 Subject: [PATCH] Error code standardization --- internal/rpc/group/callback.go | 4 ++-- internal/rpc/group/group.go | 27 +++++++-------------------- internal/utils/convert.go | 2 +- pkg/utils/strings.go | 4 ++-- 4 files changed, 12 insertions(+), 25 deletions(-) diff --git a/internal/rpc/group/callback.go b/internal/rpc/group/callback.go index d3bfdc694..df14f5b7d 100644 --- a/internal/rpc/group/callback.go +++ b/internal/rpc/group/callback.go @@ -4,7 +4,7 @@ import ( cbApi "Open_IM/pkg/callback_struct" "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" - "Open_IM/pkg/common/db/mysql_model/im_mysql_model" + imdb "Open_IM/pkg/common/db/mysql" "Open_IM/pkg/common/http" "Open_IM/pkg/common/log" "Open_IM/pkg/common/trace_log" @@ -77,7 +77,7 @@ func callbackBeforeCreateGroup(ctx context.Context, req *pbGroup.CreateGroupReq) return err } -func CallbackBeforeMemberJoinGroup(ctx context.Context, operationID string, groupMember *im_mysql_model.GroupMember, groupEx string) (err error) { +func CallbackBeforeMemberJoinGroup(ctx context.Context, operationID string, groupMember *imdb.GroupMember, groupEx string) (err error) { defer func() { trace_log.SetCtxInfo(ctx, utils.GetFuncName(1), err, "groupMember", *groupMember, "groupEx", groupEx) }() diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 0109743f4..a232ca147 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -6,15 +6,15 @@ import ( "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db" - imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model" - rocksCache "Open_IM/pkg/common/db/rocks_cache" + imdb "Open_IM/pkg/common/db/mysql" "Open_IM/pkg/common/log" "Open_IM/pkg/common/middleware" promePkg "Open_IM/pkg/common/prometheus" "Open_IM/pkg/common/token_verify" "Open_IM/pkg/common/tools" "Open_IM/pkg/common/trace_log" - cp "Open_IM/pkg/common/utils" + + cp "Open_IM/internal/utils" "Open_IM/pkg/getcdv3" pbCache "Open_IM/pkg/proto/cache" pbConversation "Open_IM/pkg/proto/conversation" @@ -131,39 +131,26 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR for _, adminUserID := range req.AdminUserIDs { userIDs = append(userIDs, adminUserID) } - if utils.IsRepeatID(userIDs) { + if utils.IsDuplicateID(userIDs) { return nil, constant.ErrArgs.Wrap("group member is repeated") } - users, err := getUsersInfo(ctx, userIDs) if err != nil { return nil, err } - userMap := make(map[string]*open_im_sdk.UserInfo) for i, user := range users { userMap[user.UserID] = users[i] } - if err := callbackBeforeCreateGroup(ctx, req); err != nil { return nil, err } - groupId := genGroupID(ctx, req.GroupInfo.GroupID) - - groupInfo := imdb.Group{} - utils.CopyStructFields(&groupInfo, req.GroupInfo) - groupInfo.CreatorUserID = tools.OpUserID(ctx) - groupInfo.GroupID = groupId - groupInfo.CreateTime = time.Now() - if groupInfo.NotificationUpdateTime.Unix() < 0 { - groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0) - } - + groupInfo, err := (&cp.PBGroup{req.GroupInfo}).Convert() + groupInfo.GroupID = genGroupID(ctx, req.GroupInfo.GroupID) if req.GroupInfo.GroupType != constant.SuperGroup { - var groupMembers []*imdb.GroupMember joinGroup := func(userID string, roleLevel int32) error { - groupMember := &imdb.GroupMember{GroupID: groupId, RoleLevel: roleLevel, OperatorUserID: tools.OpUserID(ctx), JoinSource: constant.JoinByInvitation, InviterUserID: tools.OpUserID(ctx)} + groupMember := &imdb.GroupMember{GroupID: groupInfo.GroupID, RoleLevel: roleLevel, OperatorUserID: tools.OpUserID(ctx), JoinSource: constant.JoinByInvitation, InviterUserID: tools.OpUserID(ctx)} user := userMap[userID] utils.CopyStructFields(&groupMember, user) if err := CallbackBeforeMemberJoinGroup(ctx, tools.OperationID(ctx), groupMember, groupInfo.Ex); err != nil { diff --git a/internal/utils/convert.go b/internal/utils/convert.go index 7e1d057d2..690c6eedc 100644 --- a/internal/utils/convert.go +++ b/internal/utils/convert.go @@ -120,7 +120,7 @@ type PBGroup struct { *sdk.GroupInfo } -func (pb *PBGroup) convert() (*imdb.Group, error) { +func (pb *PBGroup) Convert() (*imdb.Group, error) { dst := &imdb.Group{} utils.CopyStructFields(dst, pb) return dst, nil diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 35bf3c541..f6980613d 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -122,7 +122,7 @@ func RemoveDuplicateElement(idList []string) []string { return result } -func IsRepeatStringSlice(arr []string) bool { +func IsDuplicateStringSlice(arr []string) bool { t := make(map[string]struct{}) for _, s := range arr { if _, ok := t[s]; ok { @@ -133,6 +133,6 @@ func IsRepeatStringSlice(arr []string) bool { return false } -func IsRepeatID(args ...interface{}) bool { +func IsDuplicateID(args ...interface{}) bool { return false }