You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/db/unrelation/super_group.go

122 lines
4.5 KiB

2 years ago
package unrelation
import (
2 years ago
"Open_IM/pkg/common/config"
2 years ago
"Open_IM/pkg/common/db/table/unrelation"
2 years ago
"Open_IM/pkg/utils"
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern"
)
2 years ago
type SuperGroupMongoDriver struct {
2 years ago
MgoClient *mongo.Client
MgoDB *mongo.Database
2 years ago
superGroupCollection *mongo.Collection
userToSuperGroupCollection *mongo.Collection
}
2 years ago
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
2 years ago
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
2 years ago
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
2 years ago
}
2 years ago
func (db *SuperGroupMongoDriver) CreateSuperGroup(sCtx mongo.SessionContext, groupID string, initMemberIDs []string) error {
superGroup := unrelation.SuperGroupModel{
GroupID: groupID,
MemberIDs: initMemberIDs,
2 years ago
}
_, err := db.superGroupCollection.InsertOne(sCtx, superGroup)
if err != nil {
return err
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
2 years ago
for _, userID := range initMemberIDs {
2 years ago
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
2 years ago
if err != nil {
return err
}
2 years ago
}
return nil
2 years ago
}
2 years ago
func (db *SuperGroupMongoDriver) GetSuperGroup(ctx context.Context, groupID string) (*unrelation.SuperGroupModel, error) {
superGroup := unrelation.SuperGroupModel{}
2 years ago
err := db.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup)
return &superGroup, err
}
2 years ago
func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
2 years ago
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
2 years ago
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
2 years ago
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
2 years ago
for _, userID := range userIDs {
2 years ago
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return utils.Wrap(err, "transaction failed")
}
}
2 years ago
return sCtx.CommitTransaction(ctx)
2 years ago
})
}
2 years ago
func (db *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
2 years ago
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
2 years ago
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
2 years ago
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
2 years ago
err = db.RemoveGroupFromUser(sCtx, groupID, userIDs)
2 years ago
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
return sCtx.CommitTransaction(ctx)
})
2 years ago
}
2 years ago
func (db *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
var user unrelation.UserToSuperGroupModel
2 years ago
err := db.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
return &user, utils.Wrap(err, "")
2 years ago
}
2 years ago
func (db *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
2 years ago
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
2 years ago
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
2 years ago
superGroup := &unrelation.SuperGroupModel{}
2 years ago
_, err := db.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
2 years ago
if err = db.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
2 years ago
_ = sCtx.AbortTransaction(ctx)
return err
}
return sCtx.CommitTransaction(ctx)
})
2 years ago
}
2 years ago
func (db *SuperGroupMongoDriver) RemoveGroupFromUser(sCtx context.Context, groupID string, userIDs []string) error {
_, err := db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
2 years ago
return err
}