feat: Remove MySQL and implement it all with Mongo (#1508)

* fix: GetUserReqApplicationList error when there is a disbanded group chat

* fix: error when querying some information about disbanded group

* fix: GetUserReqApplicationList dismissed group error

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* fix: the original message referenced by the pull message processing is withdrawn

* merge

* cicd: robot automated Change

* sdkws.MsgData

* user

* interface{} -> any

* user

* third

* group

* group

* group

* group

* group

* group

* conversation

* standalone mysql db model

* tx

* s3

* group

* mongo

* group

* group

* group

* group

* group

* group

* refactor: add openim mysql to mongo refactor

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>

* refactor: add openim mysql to mongo refactor

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>

* remove mysql

* remove mysql

* friend

* friend

* friend

* friend

* friend

* friend

* group

* convert

* index

* index

* all

* all

* mysql2mongo

* data conversion

* up35

* up35

* feat: add format set

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>

* fix: fix scripts

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>

* merge main

* merge main

* Update init-config.sh

* fix: user args check

---------

Signed-off-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
Co-authored-by: withchao <withchao@users.noreply.github.com>
Co-authored-by: Xinwei Xiong(cubxxw) <3293172751nss@gmail.com>
Co-authored-by: Xinwei Xiong <3293172751@qq.com>
This commit is contained in:
chao
2023-12-05 20:53:02 +08:00
committed by GitHub
parent 1c1322e3d2
commit c0194f6ef4
141 changed files with 4273 additions and 4858 deletions
-10
View File
@@ -99,16 +99,6 @@ func (m *Mongo) CreateMsgIndex() error {
return m.createMongoIndex(unrelation.Msg, true, "doc_id")
}
func (m *Mongo) CreateSuperGroupIndex() error {
if err := m.createMongoIndex(unrelation.CSuperGroup, true, "group_id"); err != nil {
return err
}
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
return err
}
return nil
}
func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...string) error {
db := m.db.Database(config.Config.Mongo.Database).Collection(collection)
opts := options.CreateIndexes().SetMaxTime(10 * time.Second)
Executable → Regular
View File
+1 -1
View File
@@ -48,7 +48,7 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs
log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID)
continue
}
var newMsgDocs []interface{}
var newMsgDocs []any
for _, msgDoc := range msgDocs {
if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() {
continue
+147 -146
View File
@@ -14,149 +14,150 @@
package unrelation
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/OpenIMSDK/tools/utils"
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation"
)
func NewSuperGroupMongoDriver(database *mongo.Database) unrelation.SuperGroupModelInterface {
return &SuperGroupMongoDriver{
superGroupCollection: database.Collection(unrelation.CSuperGroup),
userToSuperGroupCollection: database.Collection(unrelation.CUserToSuperGroup),
}
}
type SuperGroupMongoDriver struct {
superGroupCollection *mongo.Collection
userToSuperGroupCollection *mongo.Collection
}
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
GroupID: groupID,
MemberIDs: initMemberIDs,
})
if err != nil {
return err
}
for _, userID := range initMemberIDs {
_, err = s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": userID},
bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
&options.UpdateOptions{
Upsert: utils.ToPtr(true),
},
)
if err != nil {
return err
}
}
return nil
}
func (s *SuperGroupMongoDriver) TakeSuperGroup(
ctx context.Context,
groupID string,
) (group *unrelation.SuperGroupModel, err error) {
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
return nil, utils.Wrap(err, "")
}
return group, nil
}
func (s *SuperGroupMongoDriver) FindSuperGroup(
ctx context.Context,
groupIDs []string,
) (groups []*unrelation.SuperGroupModel, err error) {
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
"$in": groupIDs,
}})
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
if err := cursor.All(ctx, &groups); err != nil {
return nil, utils.Wrap(err, "")
}
return groups, nil
}
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.superGroupCollection.UpdateOne(
ctx,
bson.M{"group_id": groupID},
bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}},
)
if err != nil {
return err
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
for _, userID := range userIDs {
_, err = s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": userID},
bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
opts,
)
if err != nil {
return utils.Wrap(err, "transaction failed")
}
}
return nil
}
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.superGroupCollection.UpdateOne(
ctx,
bson.M{"group_id": groupID},
bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}},
)
if err != nil {
return err
}
err = s.RemoveGroupFromUser(ctx, groupID, userIDs)
if err != nil {
return err
}
return nil
}
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(
ctx context.Context,
userID string,
) (*unrelation.UserToSuperGroupModel, error) {
var user unrelation.UserToSuperGroupModel
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return &user, utils.Wrap(err, "")
}
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
group, err := s.TakeSuperGroup(ctx, groupID)
if err != nil {
return err
}
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
return utils.Wrap(err, "")
}
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
}
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error {
_, err := s.userToSuperGroupCollection.UpdateOne(
ctx,
bson.M{"user_id": bson.M{"$in": userIDs}},
bson.M{"$pull": bson.M{"group_id_list": groupID}},
)
return utils.Wrap(err, "")
}
//
//import (
// "context"
//
// "go.mongodb.org/mongo-driver/bson"
// "go.mongodb.org/mongo-driver/mongo"
// "go.mongodb.org/mongo-driver/mongo/options"
//
// "github.com/OpenIMSDK/tools/utils"
//
// "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation"
//)
//
//func NewSuperGroupMongoDriver(database *mongo.Database) unrelation.SuperGroupModelInterface {
// return &SuperGroupMongoDriver{
// superGroupCollection: database.Collection(unrelation.CSuperGroup),
// userToSuperGroupCollection: database.Collection(unrelation.CUserToSuperGroup),
// }
//}
//
//type SuperGroupMongoDriver struct {
// superGroupCollection *mongo.Collection
// userToSuperGroupCollection *mongo.Collection
//}
//
//func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
// _, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
// GroupID: groupID,
// MemberIDs: initMemberIDs,
// })
// if err != nil {
// return err
// }
// for _, userID := range initMemberIDs {
// _, err = s.userToSuperGroupCollection.UpdateOne(
// ctx,
// bson.M{"user_id": userID},
// bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
// &options.UpdateOptions{
// Upsert: utils.ToPtr(true),
// },
// )
// if err != nil {
// return err
// }
// }
// return nil
//}
//
//func (s *SuperGroupMongoDriver) TakeSuperGroup(
// ctx context.Context,
// groupID string,
//) (group *unrelation.SuperGroupModel, err error) {
// if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
// return nil, utils.Wrap(err, "")
// }
// return group, nil
//}
//
//func (s *SuperGroupMongoDriver) FindSuperGroup(
// ctx context.Context,
// groupIDs []string,
//) (groups []*unrelation.SuperGroupModel, err error) {
// cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
// "$in": groupIDs,
// }})
// if err != nil {
// return nil, err
// }
// defer cursor.Close(ctx)
// if err := cursor.All(ctx, &groups); err != nil {
// return nil, utils.Wrap(err, "")
// }
// return groups, nil
//}
//
//func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
// _, err := s.superGroupCollection.UpdateOne(
// ctx,
// bson.M{"group_id": groupID},
// bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}},
// )
// if err != nil {
// return err
// }
// upsert := true
// opts := &options.UpdateOptions{
// Upsert: &upsert,
// }
// for _, userID := range userIDs {
// _, err = s.userToSuperGroupCollection.UpdateOne(
// ctx,
// bson.M{"user_id": userID},
// bson.M{"$addToSet": bson.M{"group_id_list": groupID}},
// opts,
// )
// if err != nil {
// return utils.Wrap(err, "transaction failed")
// }
// }
// return nil
//}
//
//func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
// _, err := s.superGroupCollection.UpdateOne(
// ctx,
// bson.M{"group_id": groupID},
// bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}},
// )
// if err != nil {
// return err
// }
// err = s.RemoveGroupFromUser(ctx, groupID, userIDs)
// if err != nil {
// return err
// }
// return nil
//}
//
//func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(
// ctx context.Context,
// userID string,
//) (*unrelation.UserToSuperGroupModel, error) {
// var user unrelation.UserToSuperGroupModel
// err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
// return &user, utils.Wrap(err, "")
//}
//
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
// group, err := s.TakeSuperGroup(ctx, groupID)
// if err != nil {
// return err
// }
// if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
// return utils.Wrap(err, "")
// }
// return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
//}
//
//func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error {
// _, err := s.userToSuperGroupCollection.UpdateOne(
// ctx,
// bson.M{"user_id": bson.M{"$in": userIDs}},
// bson.M{"$pull": bson.M{"group_id_list": groupID}},
// )
// return utils.Wrap(err, "")
//}
Executable → Regular
View File