From c1626947fc3135846dcfcb87997ac94a9d50c755 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Fri, 1 Mar 2024 03:09:22 +0800 Subject: [PATCH] feat: add openim auto format code --- pkg/common/cmd/msg_transfer.go | 3 +-- pkg/common/cmd/msg_utils.go | 3 ++- pkg/common/cmd/root.go | 4 +-- pkg/common/cmd/rpc.go | 4 +-- pkg/common/db/controller/msg.go | 27 +++++++++---------- pkg/common/db/controller/third.go | 8 +----- test/e2e/framework/helpers/chat/chat.go | 2 +- tools/data-conversion/openim/mysql/v3/user.go | 25 +++++++++++++---- tools/formitychecker/checker/checker.go | 3 ++- 9 files changed, 44 insertions(+), 35 deletions(-) diff --git a/pkg/common/cmd/msg_transfer.go b/pkg/common/cmd/msg_transfer.go index 891c8745d..545c7543b 100644 --- a/pkg/common/cmd/msg_transfer.go +++ b/pkg/common/cmd/msg_transfer.go @@ -22,7 +22,6 @@ import ( config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/OpenIMSDK/tools/errs" "github.com/openimsdk/open-im-server/v3/internal/msgtransfer" ) @@ -40,7 +39,7 @@ func (m *MsgTransferCmd) addRunE() { m.Command.RunE = func(cmd *cobra.Command, args []string) error { prometheusPort, err := m.getPrometheusPortFlag(cmd) if err != nil { - return errs.Wrap(err, "failed to get Prometheus port") + return err } return msgtransfer.StartTransfer(prometheusPort) } diff --git a/pkg/common/cmd/msg_utils.go b/pkg/common/cmd/msg_utils.go index dd6d645d7..3c670ad85 100644 --- a/pkg/common/cmd/msg_utils.go +++ b/pkg/common/cmd/msg_utils.go @@ -18,6 +18,7 @@ import ( "github.com/spf13/cobra" "github.com/openimsdk/open-im-server/v3/internal/tools" + util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil" ) type MsgUtilsCmd struct { @@ -137,7 +138,7 @@ func (s *SeqCmd) GetSeqCmd() *cobra.Command { s.Command.Run = func(cmdLines *cobra.Command, args []string) { _, err := tools.InitMsgTool() if err != nil { - panic(err) + util.ExitWithError(err) } userID := s.getUserIDFlag(cmdLines) superGroupID := s.getSuperGroupIDFlag(cmdLines) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index f6c56b595..ccd567461 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -80,7 +80,7 @@ func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) cmdOpts := rc.applyOptions(opts...) if err := rc.initializeLogger(cmdOpts); err != nil { - return fmt.Errorf("failed to initialize from config: %w", err) + return errs.Wrap(err, "failed to initialize logger") } return nil @@ -164,7 +164,7 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) (int, error) { if err != nil || port == 0 { port, err = r.PortFromConfig(constant.FlagPrometheusPort) if err != nil { - return 0, errs.Wrap(err, "error getting prometheus port from config") + return 0, err } } return port, nil diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index 303cefe3c..39bf9c036 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -45,13 +45,13 @@ func (a *RpcCmd) Exec() error { a.Command.RunE = func(cmd *cobra.Command, args []string) error { portFlag, err := a.getPortFlag(cmd) if err != nil { - return errs.Wrap(err, "error getting port flag") + return err } a.port = portFlag prometheusPort, err := a.getPrometheusPortFlag(cmd) if err != nil { - return errs.Wrap(err, "error getting prometheus port flag") + return err } a.prometheusPort = prometheusPort diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index 7eac624a7..cb3355e34 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -48,33 +48,32 @@ const ( updateKeyRevoke ) +// CommonMsgDatabase defines the interface for message database operations. type CommonMsgDatabase interface { - // 批量插入消息 + // BatchInsertChat2DB inserts a batch of messages into the database for a specific conversation. BatchInsertChat2DB(ctx context.Context, conversationID string, msgs []*sdkws.MsgData, currentMaxSeq int64) error - // 撤回消息 + // RevokeMsg revokes a message in a conversation. RevokeMsg(ctx context.Context, conversationID string, seq int64, revoke *unrelationtb.RevokeModel) error - // mark as read + // MarkSingleChatMsgsAsRead marks messages as read for a single chat by sequence numbers. MarkSingleChatMsgsAsRead(ctx context.Context, userID string, conversationID string, seqs []int64) error - // 刪除redis中消息缓存 + // DeleteMessagesFromCache deletes message caches from Redis by sequence numbers. DeleteMessagesFromCache(ctx context.Context, conversationID string, seqs []int64) error + // DelUserDeleteMsgsList deletes user's message deletion list. DelUserDeleteMsgsList(ctx context.Context, conversationID string, seqs []int64) - // incrSeq然后批量插入缓存 + // BatchInsertChat2Cache increments the sequence number and then batch inserts messages into the cache. BatchInsertChat2Cache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (seq int64, isNewConversation bool, err error) - - // 通过seqList获取mongo中写扩散消息 + // GetMsgBySeqsRange retrieves messages from MongoDB by a range of sequence numbers. GetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin, end, num, userMaxSeq int64) (minSeq int64, maxSeq int64, seqMsg []*sdkws.MsgData, err error) - // 通过seqList获取大群在 mongo里面的消息 + // GetMsgBySeqs retrieves messages for large groups from MongoDB by sequence numbers. GetMsgBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) (minSeq int64, maxSeq int64, seqMsg []*sdkws.MsgData, err error) - // 删除会话消息重置最小seq, remainTime为消息保留的时间单位秒,超时消息删除, 传0删除所有消息(此方法不删除redis cache) + // DeleteConversationMsgsAndSetMinSeq deletes conversation messages and resets the minimum sequence number. If `remainTime` is 0, all messages are deleted (this method does not delete Redis cache). DeleteConversationMsgsAndSetMinSeq(ctx context.Context, conversationID string, remainTime int64) error - // 用户标记删除过期消息返回标记删除的seq列表 + // UserMsgsDestruct marks messages for deletion based on destruct time and returns a list of sequence numbers for marked messages. UserMsgsDestruct(ctx context.Context, userID string, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) - - // 用户根据seq删除消息 + // DeleteUserMsgsBySeqs allows a user to delete messages based on sequence numbers. DeleteUserMsgsBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) error - // 物理删除消息置空 + // DeleteMsgsPhysicalBySeqs physically deletes messages by emptying them based on sequence numbers. DeleteMsgsPhysicalBySeqs(ctx context.Context, conversationID string, seqs []int64) error - SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error GetMaxSeqs(ctx context.Context, conversationIDs []string) (map[string]int64, error) GetMaxSeq(ctx context.Context, conversationID string) (int64, error) diff --git a/pkg/common/db/controller/third.go b/pkg/common/db/controller/third.go index fb5b0ccbe..55c047bd6 100644 --- a/pkg/common/db/controller/third.go +++ b/pkg/common/db/controller/third.go @@ -63,13 +63,7 @@ func NewThirdDatabase(cache cache.MsgModel, logdb relation.LogInterface) ThirdDa return &thirdDatabase{cache: cache, logdb: logdb} } -func (t *thirdDatabase) FcmUpdateToken( - ctx context.Context, - account string, - platformID int, - fcmToken string, - expireTime int64, -) error { +func (t *thirdDatabase) FcmUpdateToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) error { return t.cache.SetFcmToken(ctx, account, platformID, fcmToken, expireTime) } diff --git a/test/e2e/framework/helpers/chat/chat.go b/test/e2e/framework/helpers/chat/chat.go index 412e1fa4c..82dcc1aab 100644 --- a/test/e2e/framework/helpers/chat/chat.go +++ b/test/e2e/framework/helpers/chat/chat.go @@ -39,7 +39,7 @@ func main() { latestVersion := defaultTemplateVersion // getLatestVersion - getLatestVersion + // getLatestVersion // Construct the download URL downloadURL := fmt.Sprintf("https://github.com/openimsdk/chat/releases/download/%s/chat_Linux_x86_64.tar.gz", latestVersion) diff --git a/tools/data-conversion/openim/mysql/v3/user.go b/tools/data-conversion/openim/mysql/v3/user.go index 10a715bda..409689933 100644 --- a/tools/data-conversion/openim/mysql/v3/user.go +++ b/tools/data-conversion/openim/mysql/v3/user.go @@ -53,20 +53,35 @@ func (UserModel) TableName() string { return UserModelTableName } +// UserModelInterface defines the operations available for managing user models. type UserModelInterface interface { + // Create inserts a new user or multiple users into the database. Create(ctx context.Context, users []*UserModel) (err error) + + // UpdateByMap updates a user's information based on a map of changes. UpdateByMap(ctx context.Context, userID string, args map[string]interface{}) (err error) + + // Update modifies a user's information in the database. Update(ctx context.Context, user *UserModel) (err error) - // 获取指定用户信息 不存在,也不返回错误 + + // Find retrieves information for a list of users by their IDs. If a user does not exist, it is simply skipped without returning an error. Find(ctx context.Context, userIDs []string) (users []*UserModel, err error) - // 获取某个用户信息 不存在,则返回错误 + + // Take retrieves a specific user's information by their ID. Returns an error if the user does not exist. Take(ctx context.Context, userID string) (user *UserModel, err error) - // 获取用户信息 不存在,不返回错误 + + // Page retrieves a paginated list of users and the total count of users. If no users exist, returns an empty list without an error. Page(ctx context.Context, pageNumber, showNumber int32) (users []*UserModel, count int64, err error) + + // GetAllUserID retrieves all user IDs in a paginated manner. GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) + + // GetUserGlobalRecvMsgOpt retrieves a user's global message receiving option. GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) - // 获取用户总数 + + // CountTotal returns the total number of users before a specified time. CountTotal(ctx context.Context, before *time.Time) (count int64, err error) - // 获取范围内用户增量 + + // CountRangeEverydayTotal calculates the daily increment of users within a specified time range. CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) } diff --git a/tools/formitychecker/checker/checker.go b/tools/formitychecker/checker/checker.go index c9ad8239f..9a64b7090 100644 --- a/tools/formitychecker/checker/checker.go +++ b/tools/formitychecker/checker/checker.go @@ -21,6 +21,7 @@ import ( "regexp" "strings" + "github.com/OpenIMSDK/tools/errs" "github.com/openimsdk/open-im-server/tools/formitychecker/config" ) @@ -39,7 +40,7 @@ func CheckDirectory(cfg *config.Config) error { for _, targetDir := range cfg.TargetDirs { err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error { if err != nil { - return err + return errs.Wrap(err, fmt.Sprintf("error walking directory '%s'", targetDir)) } // Skip if the directory is in the ignore list