diff --git a/internal/rpc/friend/friend_test.go b/internal/rpc/friend/friend_test.go deleted file mode 100644 index d1e1e420d..000000000 --- a/internal/rpc/friend/friend_test.go +++ /dev/null @@ -1,114 +0,0 @@ -package friend - -import ( - "context" - "github.com/OpenIMSDK/protocol/constant" - "github.com/OpenIMSDK/tools/log" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" - tx2 "github.com/openimsdk/open-im-server/v3/pkg/common/db/tx" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" - "github.com/redis/go-redis/v9" - "net" - "testing" -) - -var ( - rdb redis.UniversalClient - mgo *unrelation.Mongo - ctx context.Context - - friendDB *mgo.FriendMgo - friendRequestDB *mgo.FriendRequestMgo - blackDB *mgo.BlackMgo - - friendDatabase controller.FriendDatabase -) - -func InitDB() error { - addr := "172.16.8.142" - pwd := "openIM123" - - config.Config.Redis.Address = []string{net.JoinHostPort(addr, "16379")} - config.Config.Redis.Password = pwd - config.Config.Mongo.Address = []string{net.JoinHostPort(addr, "37017")} - config.Config.Mongo.Database = "openIM_v3" - config.Config.Mongo.Username = "root" - config.Config.Mongo.Password = pwd - config.Config.Mongo.MaxPoolSize = 100 - var err error - rdb, err = cache.NewRedis() - if err != nil { - return err - } - mgo, err = unrelation.NewMongo() - if err != nil { - return err - } - tx, err := tx2.NewAuto(context.Background(), mgo.GetClient()) - if err != nil { - return err - } - - config.Config.Object.Enable = "minio" - config.Config.Object.ApiURL = "http://" + net.JoinHostPort(addr, "10002") - config.Config.Object.Minio.Bucket = "openim" - config.Config.Object.Minio.Endpoint = "http://" + net.JoinHostPort(addr, "10005") - config.Config.Object.Minio.AccessKeyID = "root" - config.Config.Object.Minio.SecretAccessKey = pwd - config.Config.Object.Minio.SignEndpoint = config.Config.Object.Minio.Endpoint - - config.Config.Manager.UserID = []string{"openIM123456"} - config.Config.Manager.Nickname = []string{"openIM123456"} - - ctx = context.WithValue(context.Background(), constant.OperationID, "debugOperationID") - ctx = context.WithValue(context.Background(), constant.OpUserID, config.Config.Manager.UserID[0]) - - if err := log.InitFromConfig("", "", 6, true, false, "", 2, 1); err != nil { - panic(err) - } - - a, err := mgo.NewFriendMongo(mgo.GetDatabase()) - if err != nil { - return err - } - b, err := mgo.NewFriendRequestMongo(mgo.GetDatabase()) - if err != nil { - return err - } - c, err := mgo.NewBlackMongo(mgo.GetDatabase()) - if err != nil { - return err - } - - friendDB = a.(*mgo.FriendMgo) - friendRequestDB = b.(*mgo.FriendRequestMgo) - blackDB = c.(*mgo.BlackMgo) - - friendDatabase = controller.NewFriendDatabase( - friendDB, - friendRequestDB, - cache.NewFriendCacheRedis(rdb, friendDB, cache.GetDefaultOpt()), - tx, - ) - - return nil -} - -func init() { - if err := InitDB(); err != nil { - panic(err) - } -} - -func TestName(t *testing.T) { - err := friendDatabase.AgreeFriendRequest(ctx, &relation.FriendRequestModel{FromUserID: "8449222880", ToUserID: "3136372091", HandleResult: 1, HandleMsg: "ok"}) - t.Log(err) -} - -func Test_GroupCreateCount(t *testing.T) { - -} diff --git a/internal/rpc/user/user_test.go b/internal/rpc/user/user_test.go deleted file mode 100644 index 1724f4b53..000000000 --- a/internal/rpc/user/user_test.go +++ /dev/null @@ -1,132 +0,0 @@ -package user - -import ( - "context" - "errors" - "github.com/OpenIMSDK/protocol/constant" - "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/protocol/user" - "github.com/OpenIMSDK/tools/log" - "github.com/openimsdk/open-im-server/v3/pkg/common/config" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/cache" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/controller" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo" - tablerelation "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" - tx2 "github.com/openimsdk/open-im-server/v3/pkg/common/db/tx" - "github.com/openimsdk/open-im-server/v3/pkg/common/db/unrelation" - "github.com/redis/go-redis/v9" - "math/rand" - "net" - "strconv" - "testing" -) - -var ( - rdb redis.UniversalClient - mgo *unrelation.Mongo - ctx context.Context -) - -func InitDB() error { - addr := "172.16.8.142" - pwd := "openIM123" - - config.Config.Redis.Address = []string{net.JoinHostPort(addr, "16379")} - config.Config.Redis.Password = pwd - config.Config.Mongo.Address = []string{net.JoinHostPort(addr, "37017")} - config.Config.Mongo.Database = "openIM_v3" - config.Config.Mongo.Username = "root" - config.Config.Mongo.Password = pwd - config.Config.Mongo.MaxPoolSize = 100 - var err error - rdb, err = cache.NewRedis() - if err != nil { - return err - } - mgo, err = unrelation.NewMongo() - if err != nil { - return err - } - tx, err := tx2.NewAuto(context.Background(), mgo.GetClient()) - if err != nil { - return err - } - - config.Config.Object.Enable = "minio" - config.Config.Object.ApiURL = "http://" + net.JoinHostPort(addr, "10002") - config.Config.Object.Minio.Bucket = "openim" - config.Config.Object.Minio.Endpoint = "http://" + net.JoinHostPort(addr, "10005") - config.Config.Object.Minio.AccessKeyID = "root" - config.Config.Object.Minio.SecretAccessKey = pwd - config.Config.Object.Minio.SignEndpoint = config.Config.Object.Minio.Endpoint - - config.Config.Manager.UserID = []string{"openIM123456"} - config.Config.Manager.Nickname = []string{"openIM123456"} - - ctx = context.WithValue(context.Background(), constant.OperationID, "debugOperationID") - ctx = context.WithValue(context.Background(), constant.OpUserID, config.Config.Manager.UserID[0]) - - if err := log.InitFromConfig("", "", 6, true, false, "", 2, 1); err != nil { - panic(err) - } - - users := make([]*tablerelation.UserModel, 0) - if len(config.Config.Manager.UserID) != len(config.Config.Manager.Nickname) { - return errors.New("len(config.Config.Manager.AppManagerUid) != len(config.Config.Manager.Nickname)") - } - for k, v := range config.Config.Manager.UserID { - users = append(users, &tablerelation.UserModel{UserID: v, Nickname: config.Config.Manager.Nickname[k], AppMangerLevel: constant.AppAdmin}) - } - userDB, err := mgo.NewUserMongo(mgo.GetDatabase()) - if err != nil { - return err - } - - //var client registry.SvcDiscoveryRegistry - //_= client - cache := cache.NewUserCacheRedis(rdb, userDB, cache.GetDefaultOpt()) - userMongoDB := unrelation.NewUserMongoDriver(mgo.GetDatabase()) - database := controller.NewUserDatabase(userDB, cache, tx, userMongoDB) - //friendRpcClient := rpcclient.NewFriendRpcClient(client) - //groupRpcClient := rpcclient.NewGroupRpcClient(client) - //msgRpcClient := rpcclient.NewMessageRpcClient(client) - - userSrv = &userServer{ - UserDatabase: database, - //RegisterCenter: client, - //friendRpcClient: &friendRpcClient, - //groupRpcClient: &groupRpcClient, - //friendNotificationSender: notification.NewFriendNotificationSender(&msgRpcClient, notification.WithDBFunc(database.FindWithError)), - //userNotificationSender: notification.NewUserNotificationSender(&msgRpcClient, notification.WithUserFunc(database.FindWithError)), - } - - return nil -} - -func init() { - if err := InitDB(); err != nil { - panic(err) - } -} - -var userSrv *userServer - -func TestName(t *testing.T) { - userID := strconv.Itoa(int(rand.Uint32())) - res, err := userSrv.UserRegister(ctx, &user.UserRegisterReq{ - Secret: config.Config.Secret, - Users: []*sdkws.UserInfo{ - { - UserID: userID, - Nickname: userID, - FaceURL: "", - Ex: "", - }, - }, - }) - if err != nil { - panic(err) - } - t.Log(res) - -} diff --git a/pkg/common/db/mgo/black.go b/pkg/common/db/mgo/black.go index 4cf77f5df..52abcda81 100644 --- a/pkg/common/db/mgo/black.go +++ b/pkg/common/db/mgo/black.go @@ -11,9 +11,15 @@ import ( ) func NewBlackMongo(db *mongo.Database) (relation.BlackModelInterface, error) { - return &BlackMgo{ - coll: db.Collection("black"), - }, nil + coll := db.Collection("black") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"owner_user_id": 1, "block_user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &BlackMgo{coll: coll}, nil } type BlackMgo struct { diff --git a/pkg/common/db/mgo/conversation.go b/pkg/common/db/mgo/conversation.go index 87018df96..7f2107e59 100644 --- a/pkg/common/db/mgo/conversation.go +++ b/pkg/common/db/mgo/conversation.go @@ -13,9 +13,15 @@ import ( ) func NewConversationMongo(db *mongo.Database) (*ConversationMgo, error) { - return &ConversationMgo{ - coll: db.Collection("conversation"), - }, nil + coll := db.Collection("conversation") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"owner_user_id": 1, "conversation_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &ConversationMgo{coll: coll}, nil } type ConversationMgo struct { diff --git a/pkg/common/db/mgo/friend.go b/pkg/common/db/mgo/friend.go index 82fa929f9..40c2cbc45 100644 --- a/pkg/common/db/mgo/friend.go +++ b/pkg/common/db/mgo/friend.go @@ -18,9 +18,15 @@ type FriendMgo struct { // NewFriendMongo creates a new instance of FriendMgo with the provided MongoDB database. func NewFriendMongo(db *mongo.Database) (relation.FriendModelInterface, error) { - return &FriendMgo{ - coll: db.Collection("friend"), - }, nil + coll := db.Collection("friend") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"owner_user_id": 1, "friend_user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &FriendMgo{coll: coll}, nil } // Create inserts multiple friend records. diff --git a/pkg/common/db/mgo/friend_request.go b/pkg/common/db/mgo/friend_request.go index 9cfc37797..5886258d4 100644 --- a/pkg/common/db/mgo/friend_request.go +++ b/pkg/common/db/mgo/friend_request.go @@ -3,6 +3,7 @@ package mgo import ( "context" "github.com/openimsdk/open-im-server/v3/pkg/common/pagination" + "go.mongodb.org/mongo-driver/mongo/options" "github.com/openimsdk/open-im-server/v3/pkg/common/db/mgo/mtool" "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation" @@ -11,9 +12,15 @@ import ( ) func NewFriendRequestMongo(db *mongo.Database) (relation.FriendRequestModelInterface, error) { - return &FriendRequestMgo{ - coll: db.Collection("friend_request"), - }, nil + coll := db.Collection("friend_request") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"from_user_id": 1, "to_user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &FriendRequestMgo{coll: coll}, nil } type FriendRequestMgo struct { diff --git a/pkg/common/db/mgo/group.go b/pkg/common/db/mgo/group.go index ca5e96dd8..88b9a0f21 100644 --- a/pkg/common/db/mgo/group.go +++ b/pkg/common/db/mgo/group.go @@ -7,13 +7,20 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/pagination" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) func NewGroupMongo(db *mongo.Database) (relation.GroupModelInterface, error) { - return &GroupMgo{ - coll: db.Collection("group"), - }, nil + coll := db.Collection("group") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"group_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &GroupMgo{coll: coll}, nil } type GroupMgo struct { diff --git a/pkg/common/db/mgo/group_member.go b/pkg/common/db/mgo/group_member.go index 798f27ade..34069b7f3 100644 --- a/pkg/common/db/mgo/group_member.go +++ b/pkg/common/db/mgo/group_member.go @@ -12,7 +12,15 @@ import ( ) func NewGroupMember(db *mongo.Database) (relation.GroupMemberModelInterface, error) { - return &GroupMemberMgo{coll: db.Collection("group_member")}, nil + coll := db.Collection("group_member") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"group_id": 1, "user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &GroupMemberMgo{coll: coll}, nil } type GroupMemberMgo struct { diff --git a/pkg/common/db/mgo/group_request.go b/pkg/common/db/mgo/group_request.go index a980d4e90..d48ec8906 100644 --- a/pkg/common/db/mgo/group_request.go +++ b/pkg/common/db/mgo/group_request.go @@ -7,10 +7,19 @@ import ( "github.com/openimsdk/open-im-server/v3/pkg/common/pagination" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" ) func NewGroupRequestMgo(db *mongo.Database) (relation.GroupRequestModelInterface, error) { - return &GroupRequestMgo{coll: db.Collection("group_request")}, nil + coll := db.Collection("group_request") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"group_id": 1, "user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &GroupRequestMgo{coll: coll}, nil } type GroupRequestMgo struct { diff --git a/pkg/common/db/mgo/log.go b/pkg/common/db/mgo/log.go index c3db198d5..9dcf703a9 100644 --- a/pkg/common/db/mgo/log.go +++ b/pkg/common/db/mgo/log.go @@ -12,10 +12,23 @@ import ( ) func NewLogMongo(db *mongo.Database) (relation.LogInterface, error) { - lm := &LogMgo{ - coll: db.Collection("log"), + coll := db.Collection("log") + _, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{ + { + Keys: bson.M{"log_id": 1}, + Options: options.Index().SetUnique(true), + }, + { + Keys: bson.M{"user_id": 1}, + }, + { + Keys: bson.M{"create_time": -1}, + }, + }) + if err != nil { + return nil, err } - return lm, nil + return &LogMgo{coll: coll}, nil } type LogMgo struct { diff --git a/pkg/common/db/mgo/mtool/tool.go b/pkg/common/db/mgo/mtool/tool.go index eb4a854bb..6c2ca6eb5 100644 --- a/pkg/common/db/mgo/mtool/tool.go +++ b/pkg/common/db/mgo/mtool/tool.go @@ -2,6 +2,7 @@ package mtool import ( "context" + "go.mongodb.org/mongo-driver/bson" "github.com/OpenIMSDK/tools/errs" "github.com/openimsdk/open-im-server/v3/pkg/common/pagination" @@ -176,3 +177,18 @@ func Aggregate[T any](ctx context.Context, coll *mongo.Collection, pipeline any, } return ts, nil } + +func CreateUniqueIndex(ctx context.Context, coll *mongo.Collection, fields ...string) error { + if len(fields) == 0 { + return nil + } + keys := bson.M{} + for _, field := range fields { + keys[field] = 1 + } + _, err := coll.Indexes().CreateOne(ctx, mongo.IndexModel{ + Keys: keys, + Options: options.Index().SetUnique(true), + }) + return err +} diff --git a/pkg/common/db/mgo/object.go b/pkg/common/db/mgo/object.go index 500ef1371..0a6e1031c 100644 --- a/pkg/common/db/mgo/object.go +++ b/pkg/common/db/mgo/object.go @@ -10,9 +10,15 @@ import ( ) func NewS3Mongo(db *mongo.Database) (relation.ObjectInfoModelInterface, error) { - return &S3Mongo{ - coll: db.Collection("s3"), - }, nil + coll := db.Collection("s3") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"name": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &S3Mongo{coll: coll}, nil } type S3Mongo struct { diff --git a/pkg/common/db/mgo/user.go b/pkg/common/db/mgo/user.go index 4ef8e6ba4..a9e661c31 100644 --- a/pkg/common/db/mgo/user.go +++ b/pkg/common/db/mgo/user.go @@ -12,9 +12,15 @@ import ( ) func NewUserMongo(db *mongo.Database) (relation.UserModelInterface, error) { - return &UserMgo{ - coll: db.Collection("user"), - }, nil + coll := db.Collection("user") + _, err := coll.Indexes().CreateOne(context.Background(), mongo.IndexModel{ + Keys: bson.M{"user_id": 1}, + Options: options.Index().SetUnique(true), + }) + if err != nil { + return nil, err + } + return &UserMgo{coll: coll}, nil } type UserMgo struct {