From 2c2b4c7c4a7d35e0cf21ff092689aac8f91b2409 Mon Sep 17 00:00:00 2001 From: "Xinwei Xiong (cubxxw)" <3293172751nss@gmail.com> Date: Thu, 29 Feb 2024 13:57:45 +0800 Subject: [PATCH] style: format openim server code style --- internal/msggateway/init.go | 2 +- internal/push/callback.go | 2 +- pkg/common/cmd/msg_gateway.go | 5 ++--- pkg/common/cmd/root.go | 22 +++++++++++----------- pkg/common/cmd/rpc.go | 9 +-------- pkg/common/config/parse.go | 8 ++++---- pkg/common/convert/black.go | 6 +----- pkg/common/convert/friend.go | 6 +----- pkg/common/db/cache/meta_cache.go | 14 +++----------- pkg/common/db/cache/msg.go | 5 +++-- pkg/common/db/cache/user.go | 6 +----- pkg/common/db/controller/black.go | 24 ++++++++++++------------ pkg/common/http/http_client.go | 1 + pkg/common/tls/tls.go | 1 + pkg/msgprocessor/conversation.go | 3 ++- pkg/rpcclient/conversation.go | 1 + test/e2e/page/chat_page.go | 2 +- 17 files changed, 47 insertions(+), 70 deletions(-) mode change 100755 => 100644 pkg/common/tls/tls.go diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index dda2d5e5d..5d19ad16d 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -23,7 +23,7 @@ import ( // RunWsAndServer run ws server. func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { - fmt.Println("start rpc/msg_gateway server, port: ", rpcPort, wsPort, prometheusPort, ", OpenIM version: ",config.Version) + fmt.Println("start rpc/msg_gateway server, port: ", rpcPort, wsPort, prometheusPort, ", OpenIM version: ", config.Version) longServer, err := NewWsServer( WithPort(wsPort), WithMaxConnNum(int64(config.Config.LongConnSvr.WebsocketMaxConnNum)), diff --git a/internal/push/callback.go b/internal/push/callback.go index 5ce2b7049..46b5545fb 100644 --- a/internal/push/callback.go +++ b/internal/push/callback.go @@ -59,7 +59,7 @@ func callbackOfflinePush(ctx context.Context, userIDs []string, msg *sdkws.MsgDa if err := http.CallBackPostReturn(ctx, url(), req, resp, config.Config.Callback.CallbackOfflinePush); err != nil { return err } - + if len(resp.UserIDs) != 0 { *offlinePushUserIDs = resp.UserIDs } diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go index 9d95781ed..3c179f62c 100644 --- a/pkg/common/cmd/msg_gateway.go +++ b/pkg/common/cmd/msg_gateway.go @@ -16,7 +16,6 @@ package cmd import ( "github.com/openimsdk/open-im-server/v3/internal/msggateway" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/OpenIMSDK/protocol/constant" @@ -52,7 +51,7 @@ func (m *MsgGatewayCmd) addRunE() { m.Command.RunE = func(cmd *cobra.Command, args []string) error { wsPort, err := m.getWsPortFlag(cmd) if err != nil { - return errors.Wrap(err, "failed to get WS port flag") + return errs.Wrap(err, "failed to get WS port flag") } port, err := m.getPortFlag(cmd) if err != nil { @@ -62,6 +61,6 @@ func (m *MsgGatewayCmd) addRunE() { if err != nil { return err } - return msggateway.RunWsAndServer(port, wsPort, prometheusPort) + return msggateway.RunWsAndServer(port, wsPort, prometheusPort) } } diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 17e23a074..726aed05e 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -18,12 +18,12 @@ import ( "fmt" config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/pkg/errors" + + "github.com/OpenIMSDK/tools/errs" "github.com/spf13/cobra" "github.com/OpenIMSDK/protocol/constant" - "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/openimsdk/open-im-server/v3/pkg/common/config" @@ -137,27 +137,27 @@ func (r *RootCmd) getPortFlag(cmd *cobra.Command) (int, error) { port, err := cmd.Flags().GetInt(constant.FlagPort) if err != nil { // Wrapping the error with additional context - return 0, errors.Wrap(err, "error getting port flag") + return 0, errs.Wrap(err, "error getting port flag") } if port == 0 { port, _ = r.PortFromConfig(constant.FlagPort) // port, err := r.PortFromConfig(constant.FlagPort) // if err != nil { // // Optionally wrap the error if it's an internal error needing context - // return 0, errors.Wrap(err, "error getting port from config") + // return 0, errs.Wrap(err, "error getting port from config") // } } return port, nil } -// GetPortFlag returns the port flag -func (r *RootCmd) GetPortFlag() (int, error) { +// // GetPortFlag returns the port flag +// func (r *RootCmd) GetPortFlag() (int, error) { - if portFlag == 0 { - return errs.Wrap(errors.New("port is 0"), "error getting port flag") - } - return r.port, nil -} +// if portFlag == 0 { +// return errs.Wrap(errs.New("port is 0"), "error getting port flag") +// } +// return r.port, nil +// } func (r *RootCmd) AddPrometheusPortFlag() { r.Command.Flags().IntP(constant.FlagPrometheusPort, "", 0, "server prometheus listen port") diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index 491c3ce6f..2e9f7c5a0 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -15,7 +15,6 @@ package cmd import ( - "errors" "fmt" "github.com/OpenIMSDK/protocol/constant" @@ -66,13 +65,7 @@ func (a *RpcCmd) StartSvr(name string, rpcFn func(discov discoveryregistry.SvcDi a.port = portFlag } - portFlag, err = a.GetPortFlag() - - if err != nil { - return err - } - - return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn) + return startrpc.Start(portFlag, name, a.GetPrometheusPortFlag(), rpcFn) } func (a *RpcCmd) GetPortFromConfig(portType string) (int, error) { diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index 4037429e3..9623d8319 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -21,6 +21,7 @@ import ( "path/filepath" "github.com/OpenIMSDK/protocol/constant" + "github.com/OpenIMSDK/tools/errs" "gopkg.in/yaml.v3" "github.com/openimsdk/open-im-server/v3/pkg/msgprocessor" @@ -88,18 +89,17 @@ func initConfig(config any, configName, configFolderPath string) error { _, err := os.Stat(configFolderPath) if err != nil { if !os.IsNotExist(err) { - fmt.Println("stat config path error:", err.Error()) - return fmt.Errorf("stat config path error: %w", err) + return errs.Wrap(err, "stat config path error") } configFolderPath = filepath.Join(GetProjectRoot(), "config", configName) fmt.Println("flag's path,enviment's path,default path all is not exist,using project path:", configFolderPath) } data, err := os.ReadFile(configFolderPath) if err != nil { - return fmt.Errorf("read file error: %w", err) + return errs.Wrap(err, "read file error") } if err = yaml.Unmarshal(data, config); err != nil { - return fmt.Errorf("unmarshal yaml error: %w", err) + return errs.Wrap(err, "unmarshal yaml error") } fmt.Println("The path of the configuration file to start the process:", configFolderPath) diff --git a/pkg/common/convert/black.go b/pkg/common/convert/black.go index 50c270dcb..57140f436 100644 --- a/pkg/common/convert/black.go +++ b/pkg/common/convert/black.go @@ -23,11 +23,7 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" ) -func BlackDB2Pb( - ctx context.Context, - blackDBs []*relation.BlackModel, - f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), -) (blackPbs []*sdk.BlackInfo, err error) { +func BlackDB2Pb(ctx context.Context, blackDBs []*relation.BlackModel, f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (blackPbs []*sdk.BlackInfo, err error) { if len(blackDBs) == 0 { return nil, nil } diff --git a/pkg/common/convert/friend.go b/pkg/common/convert/friend.go index f3a19e459..ed4456e35 100644 --- a/pkg/common/convert/friend.go +++ b/pkg/common/convert/friend.go @@ -53,11 +53,7 @@ func FriendDB2Pb(ctx context.Context, friendDB *relation.FriendModel, }, nil } -func FriendsDB2Pb( - ctx context.Context, - friendsDB []*relation.FriendModel, - getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error), -) (friendsPb []*sdkws.FriendInfo, err error) { +func FriendsDB2Pb(ctx context.Context, friendsDB []*relation.FriendModel, getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error)) (friendsPb []*sdkws.FriendInfo, err error) { if len(friendsDB) == 0 { return nil, nil } diff --git a/pkg/common/db/cache/meta_cache.go b/pkg/common/db/cache/meta_cache.go index 7eb486c9a..fee982dc5 100644 --- a/pkg/common/db/cache/meta_cache.go +++ b/pkg/common/db/cache/meta_cache.go @@ -134,7 +134,7 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin } bs, err := json.Marshal(t) if err != nil { - return "", errs.Wrap(err) + return "", errs.Wrap(err, "marshal failed") } write = true @@ -152,8 +152,7 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin err = json.Unmarshal([]byte(v), &t) if err != nil { log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire) - - return t, errs.Wrap(err) + return t, errs.Wrap(err, "unmarshal failed") } return t, nil @@ -197,14 +196,7 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin // return tArrays, nil //} -func batchGetCache2[T any, K comparable]( - ctx context.Context, - rcClient *rockscache.Client, - expire time.Duration, - keys []K, - keyFn func(key K) string, - fns func(ctx context.Context, key K) (T, error), -) ([]T, error) { +func batchGetCache2[T any, K comparable](ctx context.Context, rcClient *rockscache.Client, expire time.Duration, keys []K, keyFn func(key K) string, fns func(ctx context.Context, key K) (T, error)) ([]T, error) { if len(keys) == 0 { return nil, nil } diff --git a/pkg/common/db/cache/msg.go b/pkg/common/db/cache/msg.go index 8a54e1a8b..9a4844e26 100644 --- a/pkg/common/db/cache/msg.go +++ b/pkg/common/db/cache/msg.go @@ -30,6 +30,7 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" + "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" @@ -433,7 +434,7 @@ func (c *msgCache) PipeSetMessageToCache(ctx context.Context, conversationID str for _, msg := range msgs { s, err := msgprocessor.Pb2String(msg) if err != nil { - return 0, errs.Wrap(err, "pb.marshal") + return 0, err } key := c.getMessageCacheKey(conversationID, msg.Seq) @@ -442,7 +443,7 @@ func (c *msgCache) PipeSetMessageToCache(ctx context.Context, conversationID str results, err := pipe.Exec(ctx) if err != nil { - return 0, errs.Wrap(err, "pipe.set") + return 0, errs.Warp(err) } for _, res := range results { diff --git a/pkg/common/db/cache/user.go b/pkg/common/db/cache/user.go index 979bd06e4..417729e76 100644 --- a/pkg/common/db/cache/user.go +++ b/pkg/common/db/cache/user.go @@ -66,11 +66,7 @@ type UserCacheRedis struct { rcClient *rockscache.Client } -func NewUserCacheRedis( - rdb redis.UniversalClient, - userDB relationtb.UserModelInterface, - options rockscache.Options, -) UserCache { +func NewUserCacheRedis(rdb redis.UniversalClient, userDB relationtb.UserModelInterface, options rockscache.Options) UserCache { rcClient := rockscache.NewClient(rdb, options) return &UserCacheRedis{ diff --git a/pkg/common/db/controller/black.go b/pkg/common/db/controller/black.go index e68d06b01..3c8a47a01 100644 --- a/pkg/common/db/controller/black.go +++ b/pkg/common/db/controller/black.go @@ -27,14 +27,14 @@ import ( ) type BlackDatabase interface { - // Create 增加黑名单 + // Create add BlackList Create(ctx context.Context, blacks []*relation.BlackModel) (err error) - // Delete 删除黑名单 + // Delete delete BlackList Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) - // FindOwnerBlacks 获取黑名单列表 + // FindOwnerBlacks get BlackList list FindOwnerBlacks(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, blacks []*relation.BlackModel, err error) FindBlackInfos(ctx context.Context, ownerUserID string, userIDs []string) (blacks []*relation.BlackModel, err error) - // CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true) + // CheckIn Check whether user2 is in the black list of user1 (inUser1Blacks==true) Check whether user1 is in the black list of user2 (inUser2Blacks==true) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) } @@ -47,7 +47,7 @@ func NewBlackDatabase(black relation.BlackModelInterface, cache cache.BlackCache return &blackDatabase{black, cache} } -// Create 增加黑名单. +// Create Add Blacklist. func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackModel) (err error) { if err := b.black.Create(ctx, blacks); err != nil { return err @@ -55,7 +55,7 @@ func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackMode return b.deleteBlackIDsCache(ctx, blacks) } -// Delete 删除黑名单. +// Delete Delete Blacklist. func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackModel) (err error) { if err := b.black.Delete(ctx, blacks); err != nil { return err @@ -63,6 +63,7 @@ func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackMode return b.deleteBlackIDsCache(ctx, blacks) } +// FindOwnerBlacks Get Blacklist List. func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relation.BlackModel) (err error) { cache := b.cache.NewCache() for _, black := range blacks { @@ -71,16 +72,13 @@ func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relat return cache.ExecDel(ctx) } -// FindOwnerBlacks 获取黑名单列表. +// FindOwnerBlacks Get Blacklist List. func (b *blackDatabase) FindOwnerBlacks(ctx context.Context, ownerUserID string, pagination pagination.Pagination) (total int64, blacks []*relation.BlackModel, err error) { return b.black.FindOwnerBlacks(ctx, ownerUserID, pagination) } -// CheckIn 检查user2是否在user1的黑名单列表中(inUser1Blacks==true) 检查user1是否在user2的黑名单列表中(inUser2Blacks==true). -func (b *blackDatabase) CheckIn( - ctx context.Context, - userID1, userID2 string, -) (inUser1Blacks bool, inUser2Blacks bool, err error) { +// FindOwnerBlacks Get Blacklist List. +func (b *blackDatabase) CheckIn(ctx context.Context, userID1, userID2 string) (inUser1Blacks bool, inUser2Blacks bool, err error) { userID1BlackIDs, err := b.cache.GetBlackIDs(ctx, userID1) if err != nil { return @@ -93,10 +91,12 @@ func (b *blackDatabase) CheckIn( return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil } +// FindBlackIDs Get Blacklist List. func (b *blackDatabase) FindBlackIDs(ctx context.Context, ownerUserID string) (blackIDs []string, err error) { return b.cache.GetBlackIDs(ctx, ownerUserID) } +// FindBlackInfos Get Blacklist List. func (b *blackDatabase) FindBlackInfos(ctx context.Context, ownerUserID string, userIDs []string) (blacks []*relation.BlackModel, err error) { return b.black.FindOwnerBlackInfos(ctx, ownerUserID, userIDs) } diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index cd1135633..258ab7126 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -25,6 +25,7 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" + "github.com/openimsdk/open-im-server/v3/pkg/callbackstruct" "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) diff --git a/pkg/common/tls/tls.go b/pkg/common/tls/tls.go old mode 100755 new mode 100644 index a52f46df7..e897c13b6 --- a/pkg/common/tls/tls.go +++ b/pkg/common/tls/tls.go @@ -22,6 +22,7 @@ import ( "os" "github.com/OpenIMSDK/tools/errs" + "github.com/openimsdk/open-im-server/v3/pkg/common/config" ) diff --git a/pkg/msgprocessor/conversation.go b/pkg/msgprocessor/conversation.go index 7477bea7a..e3ea89fad 100644 --- a/pkg/msgprocessor/conversation.go +++ b/pkg/msgprocessor/conversation.go @@ -20,6 +20,7 @@ import ( "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" + "github.com/OpenIMSDK/tools/errs" "google.golang.org/protobuf/proto" ) @@ -188,7 +189,7 @@ func (s MsgBySeq) Swap(i, j int) { func Pb2String(pb proto.Message) (string, error) { s, err := proto.Marshal(pb) if err != nil { - return "", err + return "", errs.Wrap(err) } return string(s), nil } diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index ecbe86d95..6b5b3ee87 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -23,6 +23,7 @@ import ( pbconversation "github.com/OpenIMSDK/protocol/conversation" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" + util "github.com/openimsdk/open-im-server/v3/pkg/util/genutil" "github.com/openimsdk/open-im-server/v3/pkg/common/config" diff --git a/test/e2e/page/chat_page.go b/test/e2e/page/chat_page.go index 3ad6d8b8a..52aa8377d 100644 --- a/test/e2e/page/chat_page.go +++ b/test/e2e/page/chat_page.go @@ -1 +1 @@ -package page \ No newline at end of file +package page