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

112 lines
3.9 KiB

2 years ago
package unrelation
import (
"context"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
2 years ago
func NewSuperGroupMongoDriver(database *mongo.Database) unrelation.SuperGroupModelInterface {
return &SuperGroupMongoDriver{superGroupCollection: database.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: database.Collection(unrelation.CUserToSuperGroup)}
2 years ago
}
2 years ago
type SuperGroupMongoDriver struct {
2 years ago
superGroupCollection *mongo.Collection
userToSuperGroupCollection *mongo.Collection
}
2 years ago
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
2 years ago
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
2 years ago
GroupID: groupID,
MemberIDs: initMemberIDs,
2 years ago
})
2 years ago
if err != nil {
return err
}
2 years ago
for _, userID := range initMemberIDs {
2 years ago
_, 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),
})
2 years ago
if err != nil {
return err
}
2 years ago
}
return nil
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string) (group *unrelation.SuperGroupModel, err error) {
2 years ago
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
return nil, utils.Wrap(err, "")
}
return group, nil
}
2 years ago
2 years ago
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*unrelation.SuperGroupModel, err error) {
2 years ago
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
2 years ago
"$in": groupIDs,
}})
if err != nil {
2 years ago
return nil, err
2 years ago
}
defer cursor.Close(ctx)
if err := cursor.All(ctx, &groups); err != nil {
return nil, utils.Wrap(err, "")
}
return groups, nil
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
_, 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)
2 years ago
if err != nil {
2 years ago
return utils.Wrap(err, "transaction failed")
2 years ago
}
2 years ago
}
2 years ago
return nil
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
_, 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
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
2 years ago
var user unrelation.UserToSuperGroupModel
2 years ago
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
2 years ago
return &user, utils.Wrap(err, "")
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
group, err := s.TakeSuperGroup(ctx, groupID)
2 years ago
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)
2 years ago
}
2 years ago
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
_, 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, "")
2 years ago
}