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/controller/group.go

287 lines
11 KiB

2 years ago
package controller
import (
"Open_IM/pkg/common/db/cache"
"Open_IM/pkg/common/db/relation"
2 years ago
relation2 "Open_IM/pkg/common/db/table/relation"
2 years ago
"Open_IM/pkg/common/db/unrelation"
"context"
"github.com/dtm-labs/rockscache"
_ "github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
)
type GroupInterface interface {
2 years ago
FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMember []*relation2.GroupMemberModel) error
2 years ago
DeleteGroupByIDs(ctx context.Context, groupIDs []string) error
2 years ago
TakeGroupByID(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
TakeGroupMemberByID(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupModel, err error)
GetJoinedGroupList(ctx context.Context, userID string) ([]*relation2.GroupModel, error)
GetGroupMemberList(ctx context.Context, groupID string) ([]*relation2.GroupMemberModel, error)
GetGroupMemberListByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupMemberModel, error)
GetGroupMemberFilterList(ctx context.Context, groupID string, filter int32, begin int32, maxNumber int32) ([]*relation2.GroupModel, error) // relation.GetGroupMemberByGroupID(req.GroupID, req.Filter, req.NextSeq, 30)
FindGroupMembersByID(ctx context.Context, groupID string, userIDs []string) (groups []*relation2.GroupMemberModel, err error)
2 years ago
DelGroupMember(ctx context.Context, groupID string, userIDs []string) error
2 years ago
GetGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]int, error)
GetGroupOwnerUserID(ctx context.Context, groupIDs []string) (map[string]string, error)
2 years ago
GetGroupRecvApplicationList(ctx context.Context, userID string) ([]*relation2.GroupRequestModel, error)
2 years ago
2 years ago
CreateGroupMember(ctx context.Context, groupMember []*relation2.GroupMemberModel) error
CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error
2 years ago
2 years ago
//mongo
2 years ago
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
2 years ago
DelSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
2 years ago
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error
2 years ago
GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
}
2 years ago
var _ GroupInterface = (*GroupController)(nil)
2 years ago
type GroupController struct {
2 years ago
database GroupDataBaseInterface
2 years ago
}
2 years ago
func (g *GroupController) TakeGroupMemberByID(ctx context.Context, groupID string, userID string) (groupMember *relation2.GroupModel, err error) {
2 years ago
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) FindGroupMembersByID(ctx context.Context, groupID string, userIDs []string) (groups []*relation2.GroupModel, err error) {
2 years ago
//TODO implement me
panic("implement me")
}
func (g *GroupController) DelGroupMember(ctx context.Context, groupID string, userIDs []string) error {
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) GetGroupRecvApplicationList(ctx context.Context, userID string) ([]*relation2.GroupRequestModel, error) {
2 years ago
/*
var groupRequestList []db.GroupRequest
memberList, err := GetGroupMemberListByUserID(userID)
if err != nil {
return nil, err
}
for _, v := range memberList {
if v.RoleLevel > constant.GroupOrdinaryUsers {
list, err := GetGroupRequestByGroupID(v.GroupID)
if err != nil {
// fmt.Println("111 GetGroupRequestByGroupID failed ", err.Error())
continue
}
// fmt.Println("222 GetGroupRequestByGroupID ok ", list)
groupRequestList = append(groupRequestList, list...)
// fmt.Println("333 GetGroupRequestByGroupID ok ", groupRequestList)
}
}
return groupRequestList, nil
*/
//TODO implement me
panic("implement me")
}
func (g *GroupController) DelSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) GetJoinedGroupList(ctx context.Context, userID string) ([]*relation2.GroupModel, error) {
2 years ago
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) GetGroupMemberList(ctx context.Context, groupID string) ([]*relation2.GroupModel, error) {
2 years ago
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) GetGroupMemberListByUserID(ctx context.Context, groupID string, userIDs []string) ([]*relation2.GroupModel, error) {
2 years ago
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) GetGroupMemberFilterList(ctx context.Context, groupID string, filter int32, begin int32, maxNumber int32) ([]*relation2.GroupModel, error) {
2 years ago
//TODO implement me
panic("implement me")
}
func (g *GroupController) GetGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]int, error) {
//TODO implement me
panic("implement me")
}
func (g *GroupController) GetGroupOwnerUserID(ctx context.Context, groupIDs []string) (map[string]string, error) {
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) CreateGroupMember(ctx context.Context, groupMember []*relation2.GroupModel) error {
2 years ago
//TODO implement me
panic("implement me")
}
2 years ago
func (g *GroupController) CreateGroupRequest(ctx context.Context, requests []*relation2.GroupRequestModel) error {
2 years ago
//TODO implement me
panic("implement me")
}
func (g *GroupController) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
//TODO implement me
panic("implement me")
}
2 years ago
func NewGroupController(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupInterface {
groupController := &GroupController{database: newGroupDatabase(db, rdb, mgoClient)}
2 years ago
return groupController
}
2 years ago
func (g *GroupController) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error) {
2 years ago
return g.database.FindGroupsByID(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMember []*relation2.GroupModel) error {
2 years ago
return g.database.CreateGroup(ctx, groups, groupMember)
2 years ago
}
func (g *GroupController) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error {
2 years ago
return g.database.DeleteGroupByIDs(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *GroupController) TakeGroupByID(ctx context.Context, groupID string) (group *relation2.GroupModel, err error) {
2 years ago
return g.database.TakeGroupByID(ctx, groupID)
2 years ago
}
func (g *GroupController) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
2 years ago
return g.database.GetSuperGroupByID(ctx, groupID)
2 years ago
}
2 years ago
func (g *GroupController) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
return g.database.CreateSuperGroup(ctx, groupID, initMemberIDList)
2 years ago
}
2 years ago
type GroupDataBaseInterface interface {
2 years ago
FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error)
CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMember []*relation2.GroupModel) error
2 years ago
DeleteGroupByIDs(ctx context.Context, groupIDs []string) error
2 years ago
TakeGroupByID(ctx context.Context, groupID string) (group *relation2.GroupModel, err error)
2 years ago
GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error)
2 years ago
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
2 years ago
}
type GroupDataBase struct {
2 years ago
groupDB *relation.GroupGorm
groupMemberDB *relation.GroupMemberGorm
groupRequestDB *relation.GroupRequestGorm
2 years ago
db *gorm.DB
2 years ago
2 years ago
cache *cache.GroupCache
2 years ago
mongoDB *unrelation.SuperGroupMongoDriver
2 years ago
}
2 years ago
func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupDataBaseInterface {
2 years ago
groupDB := relation.NewGroupDB(db)
groupMemberDB := relation.NewGroupMemberDB(db)
groupRequestDB := relation.NewGroupRequest(db)
2 years ago
newDB := *db
2 years ago
SuperGroupMongoDriver := unrelation.NewSuperGroupMongoDriver(mgoClient)
2 years ago
database := &GroupDataBase{
2 years ago
groupDB: groupDB,
groupMemberDB: groupMemberDB,
groupRequestDB: groupRequestDB,
2 years ago
db: &newDB,
2 years ago
cache: cache.NewGroupCache(rdb, groupDB, groupMemberDB, groupRequestDB, SuperGroupMongoDriver, rockscache.Options{
2 years ago
RandomExpireAdjustment: 0.2,
DisableCacheRead: false,
DisableCacheDelete: false,
StrongConsistency: true,
}),
2 years ago
mongoDB: SuperGroupMongoDriver,
2 years ago
}
return database
}
2 years ago
func (g *GroupDataBase) FindGroupsByID(ctx context.Context, groupIDs []string) (groups []*relation2.GroupModel, err error) {
2 years ago
return g.cache.GetGroupsInfo(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation2.GroupModel, groupMembers []*relation2.GroupMemberModel) error {
2 years ago
return g.db.Transaction(func(tx *gorm.DB) error {
2 years ago
if len(groups) > 0 {
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
return err
}
2 years ago
}
2 years ago
if len(groupMembers) > 0 {
if err := g.groupMemberDB.Create(ctx, groupMembers, tx); err != nil {
2 years ago
return err
}
}
return nil
})
2 years ago
}
2 years ago
func (g *GroupDataBase) DeleteGroupByIDs(ctx context.Context, groupIDs []string) error {
2 years ago
return g.groupDB.DB.Transaction(func(tx *gorm.DB) error {
if err := g.groupDB.Delete(ctx, groupIDs, tx); err != nil {
2 years ago
return err
}
2 years ago
if err := g.cache.DelGroupsInfo(ctx, groupIDs); err != nil {
2 years ago
return err
}
return nil
})
}
2 years ago
func (g *GroupDataBase) TakeGroupByID(ctx context.Context, groupID string) (group *relation2.GroupModel, err error) {
2 years ago
return g.cache.GetGroupInfo(ctx, groupID)
2 years ago
}
2 years ago
func (g *GroupDataBase) Update(ctx context.Context, groups []*relation2.GroupModel) error {
2 years ago
return g.db.Transaction(func(tx *gorm.DB) error {
2 years ago
if err := g.groupDB.Update(ctx, groups, tx); err != nil {
2 years ago
return err
}
var groupIDs []string
for _, group := range groups {
groupIDs = append(groupIDs, group.GroupID)
}
2 years ago
if err := g.cache.DelGroupsInfo(ctx, groupIDs); err != nil {
2 years ago
return err
}
return nil
})
}
2 years ago
func (g *GroupDataBase) GetJoinedGroupList(ctx context.Context, userID string) ([]*relation2.GroupModel, error) {
2 years ago
return nil, nil
}
2 years ago
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
2 years ago
sess, err := g.mongoDB.MgoClient.StartSession()
if err != nil {
return err
}
defer sess.EndSession(ctx)
sCtx := mongo.NewSessionContext(ctx, sess)
2 years ago
if err = g.mongoDB.CreateSuperGroup(sCtx, groupID, initMemberIDList); err != nil {
2 years ago
_ = sess.AbortTransaction(ctx)
return err
}
2 years ago
if err = g.cache.BatchDelJoinedSuperGroupIDs(ctx, initMemberIDList); err != nil {
2 years ago
_ = sess.AbortTransaction(ctx)
return err
}
return sess.CommitTransaction(ctx)
2 years ago
}
2 years ago
func (g *GroupDataBase) GetSuperGroupByID(ctx context.Context, groupID string) (superGroup *unrelation.SuperGroup, err error) {
2 years ago
return g.mongoDB.GetSuperGroup(ctx, groupID)
}