From 575106260548b4e3e98937229eb27bbf769bb28d Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Tue, 21 Feb 2023 16:48:10 +0800 Subject: [PATCH] Error code standardization --- internal/rpc/msg/send_pull.go | 2 +- internal/rpc/user/user.go | 14 ++++++++++++-- pkg/common/config/config.go | 6 +++--- pkg/common/constant/constant.go | 4 ---- pkg/common/constant/errors.go | 4 ++++ pkg/common/constant/limit.go | 4 +++- pkg/common/db/controller/msg.go | 2 ++ pkg/common/db/controller/user.go | 20 ++++++++++++++++++++ pkg/common/db/relation/mysql_init.go | 9 --------- pkg/common/db/table/relation/user.go | 2 +- pkg/utils/utils_v2.go | 22 ++++++++++++++++++++++ 11 files changed, 68 insertions(+), 21 deletions(-) diff --git a/internal/rpc/msg/send_pull.go b/internal/rpc/msg/send_pull.go index 09502d4f3..cba7e619c 100644 --- a/internal/rpc/msg/send_pull.go +++ b/internal/rpc/msg/send_pull.go @@ -67,7 +67,7 @@ func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *msg.SendMsgReq) if err != nil { return nil, err } - isSend, err := modifyMessageByUserMessageReceiveOpt(req.MsgData.RecvID, req.MsgData.SendID, constant.SingleChatType, req) + isSend, err := m.modifyMessageByUserMessageReceiveOpt(ctx, req.MsgData.RecvID, req.MsgData.SendID, constant.SingleChatType, req) if err != nil { return nil, err } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 8e53276dd..d29dd01c8 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -4,6 +4,7 @@ import ( "Open_IM/internal/common/check" "Open_IM/internal/common/convert" "Open_IM/internal/common/notification" + "Open_IM/pkg/common/config" "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db/controller" "Open_IM/pkg/common/db/relation" @@ -36,12 +37,21 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error { if err := gormDB.AutoMigrate(&tablerelation.UserModel{}); err != nil { return err } - pbuser.RegisterUserServer(server, &userServer{ + u := &userServer{ UserInterface: controller.NewUserController(controller.NewUserDatabase(relation.NewUserGorm(gormDB))), notification: notification.NewCheck(client), userCheck: check.NewUserCheck(client), RegisterCenter: client, - }) + } + pbuser.RegisterUserServer(server, u) + users := make([]*tablerelation.UserModel, 0) + if len(config.Config.Manager.AppManagerUid) != len(config.Config.Manager.Nickname) { + return constant.ErrConfig.Wrap("len(config.Config.Manager.AppManagerUid) != len(config.Config.Manager.Nickname)") + } + for k, v := range config.Config.Manager.AppManagerUid { + users = append(users, &tablerelation.UserModel{UserID: v, Nickname: config.Config.Manager.Nickname[k]}) + } + u.UserInterface.InitOnce(context.Background(), users) return nil } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index ecf6b7774..b137e96ba 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -213,9 +213,9 @@ type config struct { } } Manager struct { - AppManagerUid []string `yaml:"appManagerUid"` - Secrets []string `yaml:"secrets"` - AppSysNotificationName string `yaml:"appSysNotificationName"` + AppManagerUid []string `yaml:"appManagerUid"` + // AppSysNotificationName string `yaml:"appSysNotificationName"` + Nickname []string `yaml:"nickname"` } Kafka struct { diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 521fbd09e..ced99672a 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -319,8 +319,4 @@ const BigVersion = "v2" const LogFileName = "OpenIM.log" -const StatisticsTimeInterval = 60 - -const MaxNotificationNum = 500 - const CurrentVersion = "v2.3.4-rc0" diff --git a/pkg/common/constant/errors.go b/pkg/common/constant/errors.go index 6bf324906..03b45fa2f 100644 --- a/pkg/common/constant/errors.go +++ b/pkg/common/constant/errors.go @@ -58,6 +58,8 @@ var ( ErrConnArgsErr = &ErrInfo{ConnArgsErr, "args err, need token, sendID, platformID", ""} ErrConnUpdateErr = &ErrInfo{ConnArgsErr, "upgrade http conn err", ""} + + ErrConfig = &ErrInfo{ConfigError, "ConfigError", ""} ) const ( @@ -91,6 +93,8 @@ const ( DataError = 90007 //数据错误 IdentityError = 90008 // 身份错误 非管理员token,且token中userID与请求userID不一致 + + ConfigError = 90009 ) // 账号错误码 diff --git a/pkg/common/constant/limit.go b/pkg/common/constant/limit.go index cdd19e240..57e764eef 100644 --- a/pkg/common/constant/limit.go +++ b/pkg/common/constant/limit.go @@ -1,5 +1,7 @@ package constant const ( - ShowNumber = 1000 + ShowNumber = 1000 + StatisticsTimeInterval = 60 + MaxNotificationNum = 500 ) diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index 90e6d897c..e1c45dbfb 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -49,6 +49,8 @@ type MsgInterface interface { SetGroupUserMinSeq(ctx context.Context, groupID, userID string, minSeq int64) (err error) // 设置用户最小seq 直接调用cache SetUserMinSeq(ctx context.Context, userID string, minSeq int64) (err error) + + MsgToMQ(ctx context.Context, key string, data *pbMsg.MsgDataToMQ) (err error) } func NewMsgController(mgo *mongo.Client, rdb redis.UniversalClient) MsgInterface { diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index e0509709b..d36100898 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -3,6 +3,7 @@ package controller import ( "Open_IM/pkg/common/constant" "Open_IM/pkg/common/db/table/relation" + "Open_IM/pkg/utils" "context" ) @@ -23,6 +24,8 @@ type UserInterface interface { IsExist(ctx context.Context, userIDs []string) (exist bool, err error) //获取所有用户ID GetAllUserID(ctx context.Context) ([]string, error) + //函数内部先查询db中是否存在,存在则什么都不做;不存在则插入 + InitOnce(ctx context.Context, users []*relation.UserModel) (err error) } type UserController struct { @@ -60,6 +63,10 @@ func (u *UserController) GetAllUserID(ctx context.Context) ([]string, error) { return u.database.GetAllUserID(ctx) } +func (u *UserController) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) { + return u.database.InitOnce(ctx, users) +} + func NewUserController(database UserDatabaseInterface) UserInterface { return &UserController{database} } @@ -81,6 +88,8 @@ type UserDatabaseInterface interface { IsExist(ctx context.Context, userIDs []string) (exist bool, err error) //获取所有用户ID GetAllUserID(ctx context.Context) ([]string, error) + //函数内部先查询db中是否存在,存在则什么都不做;不存在则插入 + InitOnce(ctx context.Context, users []*relation.UserModel) (err error) } type UserDatabase struct { @@ -91,6 +100,17 @@ func NewUserDatabase(userDB relation.UserModelInterface) *UserDatabase { return &UserDatabase{userDB: userDB} } +func (u *UserDatabase) InitOnce(ctx context.Context, users []*relation.UserModel) (err error) { + userIDs := utils.Slice(users, func(e *relation.UserModel) string { + return e.UserID + }) + result, err := u.userDB.Find(ctx, userIDs) + if err != nil { + return err + } + +} + // 获取指定用户的信息 如有userID未找到 也返回错误 func (u *UserDatabase) FindWithError(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) { diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index d3b72bce7..1047f51cc 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -68,12 +68,3 @@ type Writer struct{} func (w Writer) Printf(format string, args ...interface{}) { fmt.Printf(format, args...) } - -//func getDBConn(db *gorm.DB, tx []any) *gorm.DB { -// if len(tx) > 0 { -// if txDB, ok := tx[0].(*gorm.DB); ok { -// return txDB -// } -// } -// return db -//} diff --git a/pkg/common/db/table/relation/user.go b/pkg/common/db/table/relation/user.go index b0b3ce6f8..2b0d2d441 100644 --- a/pkg/common/db/table/relation/user.go +++ b/pkg/common/db/table/relation/user.go @@ -20,7 +20,7 @@ type UserModel struct { Email string `gorm:"column:email;size:64"` Ex string `gorm:"column:ex;size:1024"` CreateTime time.Time `gorm:"column:create_time;index:create_time; autoCreateTime"` - AppMangerLevel int32 `gorm:"column:app_manger_level"` + AppMangerLevel int32 `gorm:"column:app_manger_level;default:18"` GlobalRecvMsgOpt int32 `gorm:"column:global_recv_msg_opt"` } diff --git a/pkg/utils/utils_v2.go b/pkg/utils/utils_v2.go index f32a7b73a..ed5f3df53 100644 --- a/pkg/utils/utils_v2.go +++ b/pkg/utils/utils_v2.go @@ -5,6 +5,28 @@ import ( "sort" ) +// SliceSub a中存在,b中不存在 (a-b) +func SliceSub[E comparable](a, b []E) []E { + k := make(map[E]struct{}) + for i := 0; i < len(b); i++ { + k[b[i]] = struct{}{} + } + t := make(map[E]struct{}) + rs := make([]E, 0, len(a)) + for i := 0; i < len(a); i++ { + e := a[i] + if _, ok := t[e]; ok { + continue + } + if _, ok := k[e]; ok { + continue + } + rs = append(rs, e) + t[e] = struct{}{} + } + return rs +} + // DistinctAny 去重 func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E { v := make([]E, 0, len(es))