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

412 lines
17 KiB

2 years ago
package controller
import (
"context"
2 years ago
"fmt"
2 years ago
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/tx"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2 years ago
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
2 years ago
)
2 years ago
type GroupDatabase interface {
2 years ago
// Group
2 years ago
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error)
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
2 years ago
GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error)
2 years ago
// GroupMember
2 years ago
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error)
TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error)
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error)
2 years ago
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
2 years ago
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
2 years ago
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
2 years ago
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error)
2 years ago
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
2 years ago
UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error
2 years ago
// GroupRequest
2 years ago
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error)
2 years ago
// SuperGroupModelInterface
2 years ago
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error)
2 years ago
FindJoinSuperGroup(ctx context.Context, userID string) ([]string, error)
2 years ago
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
2 years ago
DeleteSuperGroup(ctx context.Context, groupID string) error
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
2 years ago
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
2 years ago
}
2 years ago
func NewGroupDatabase(
2 years ago
group relationTb.GroupModelInterface,
member relationTb.GroupMemberModelInterface,
request relationTb.GroupRequestModelInterface,
tx tx.Tx,
ctxTx tx.CtxTx,
2 years ago
superGroup unRelationTb.SuperGroupModelInterface,
cache cache.GroupCache,
2 years ago
) GroupDatabase {
database := &groupDatabase{
2 years ago
groupDB: group,
groupMemberDB: member,
groupRequestDB: request,
tx: tx,
ctxTx: ctxTx,
2 years ago
cache: cache,
mongoDB: superGroup,
2 years ago
}
return database
}
2 years ago
func InitGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, database *mongo.Database) GroupDatabase {
2 years ago
rcOptions := rockscache.NewDefaultOptions()
rcOptions.StrongConsistency = true
rcOptions.RandomExpireAdjustment = 0.2
2 years ago
return NewGroupDatabase(
relation.NewGroupDB(db),
relation.NewGroupMemberDB(db),
relation.NewGroupRequest(db),
tx.NewGorm(db),
tx.NewMongo(database.Client()),
unrelation.NewSuperGroupMongoDriver(database),
2 years ago
cache.NewGroupCacheRedis(rdb, relation.NewGroupDB(db), relation.NewGroupMemberDB(db), relation.NewGroupRequest(db), unrelation.NewSuperGroupMongoDriver(database), rcOptions),
2 years ago
)
}
2 years ago
type groupDatabase struct {
2 years ago
groupDB relationTb.GroupModelInterface
groupMemberDB relationTb.GroupMemberModelInterface
groupRequestDB relationTb.GroupRequestModelInterface
2 years ago
tx tx.Tx
ctxTx tx.CtxTx
2 years ago
cache cache.GroupCache
2 years ago
mongoDB unRelationTb.SuperGroupModelInterface
2 years ago
}
2 years ago
func (g *groupDatabase) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
2 years ago
return g.groupDB.GetGroupIDsByGroupType(ctx, groupType)
}
2 years ago
func (g *groupDatabase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
2 years ago
return g.cache.GetGroupMemberIDs(ctx, groupID)
2 years ago
}
2 years ago
func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
2 years ago
if len(groups) > 0 {
2 years ago
if err := g.groupDB.NewTx(tx).Create(ctx, groups); err != nil {
2 years ago
return err
}
2 years ago
}
if len(groupMembers) > 0 {
2 years ago
if err := g.groupMemberDB.NewTx(tx).Create(ctx, groupMembers); err != nil {
2 years ago
return err
}
}
2 years ago
m := make(map[string]struct{})
var cache = g.cache.NewCache()
for _, groupMember := range groupMembers {
if _, ok := m[groupMember.GroupID]; !ok {
m[groupMember.GroupID] = struct{}{}
cache = cache.DelGroupMemberIDs(groupMember.GroupID).DelGroupMembersHash(groupMember.GroupID).DelJoinedGroupID(groupMember.UserID).DelGroupsMemberNum(groupMember.GroupID)
}
}
return g.cache.ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
2 years ago
return g.cache.GetGroupInfo(ctx, groupID)
2 years ago
}
2 years ago
func (g *groupDatabase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
2 years ago
return g.cache.GetGroupsInfo(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *groupDatabase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
2 years ago
return g.groupDB.Search(ctx, keyword, pageNumber, showNumber)
2 years ago
}
2 years ago
func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
if err := g.groupDB.NewTx(tx).UpdateMap(ctx, groupID, data); err != nil {
2 years ago
return err
}
2 years ago
return g.cache.DelGroupsInfo(groupID).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
if err := g.groupDB.NewTx(tx).UpdateStatus(ctx, groupID, constant.GroupStatusDismissed); err != nil {
2 years ago
return err
}
2 years ago
if err := g.groupMemberDB.NewTx(tx).DeleteGroup(ctx, []string{groupID}); err != nil {
2 years ago
return err
}
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
if err != nil {
return err
}
2 years ago
return g.cache.DelJoinedGroupID(userIDs...).DelGroupsInfo(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
2 years ago
return g.cache.GetGroupMemberInfo(ctx, groupID, userID)
2 years ago
}
2 years ago
func (g *groupDatabase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
2 years ago
return g.groupMemberDB.TakeOwner(ctx, groupID) // todo cache group owner
2 years ago
}
2 years ago
func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) (totalGroupMembers []*relationTb.GroupMemberModel, err error) {
2 years ago
if roleLevels == nil {
2 years ago
for _, groupID := range groupIDs {
groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, userIDs)
if err != nil {
return nil, err
}
totalGroupMembers = append(totalGroupMembers, groupMembers...)
}
return totalGroupMembers, nil
2 years ago
}
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
2 years ago
}
2 years ago
func (g *groupDatabase) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, totalGroupMembers []*relationTb.GroupMemberModel, err error) {
2 years ago
//if len(roleLevels) == 0 {
// if pageNumber == 0 || showNumber == 0 {
// if len(groupIDs) == 0 {
// for _, userID := range userIDs {
// groupIDs, err := g.cache.GetJoinedGroupIDs(ctx, userID)
// if err != nil {
// return 0, nil, err
// }
// for _, groupID := range groupIDs {
// groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, []string{userID})
// if err != nil {
// return 0, nil, err
// }
// totalGroupMembers = append(totalGroupMembers, groupMembers...)
// }
// }
//
// return uint32(len(totalGroupMembers)), totalGroupMembers, nil
// }
// for _, groupID := range groupIDs {
// groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, userIDs)
// if err != nil {
// return 0, nil, err
// }
// totalGroupMembers = append(totalGroupMembers, groupMembers...)
// }
// return uint32(len(totalGroupMembers)), totalGroupMembers, nil
// } else {
// if len(groupIDs) == 0 {
// for _, userID := range userIDs {
// groupIDs, err := g.cache.GetJoinedGroupIDs(ctx, userID)
// if err != nil {
// return 0, nil, err
// }
// groupIDs = utils.Paginate(groupIDs, int(pageNumber), int(showNumber))
// for _, groupID := range groupIDs {
// groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, []string{userID})
// if err != nil {
// return 0, nil, err
// }
// totalGroupMembers = append(totalGroupMembers, groupMembers...)
// }
// }
// return uint32(len(groupIDs)), totalGroupMembers, nil
// }
// var totalAll uint32
// for _, groupID := range groupIDs {
// total, groupMembers, err := g.cache.GetGroupMembersPage(ctx, groupID, userIDs, pageNumber, showNumber)
// if err != nil {
// return 0, nil, err
// }
// totalAll += total
// totalGroupMembers = append(totalGroupMembers, groupMembers...)
// }
// return totalAll, totalGroupMembers, nil
// }
//}
2 years ago
return g.groupMemberDB.SearchMember(ctx, "", groupIDs, userIDs, roleLevels, pageNumber, showNumber)
2 years ago
}
2 years ago
func (g *groupDatabase) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error) {
2 years ago
return g.groupMemberDB.SearchMember(ctx, keyword, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
2 years ago
}
2 years ago
func (g *groupDatabase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
if err := g.groupRequestDB.NewTx(tx).UpdateHandler(ctx, groupID, userID, handledMsg, handleResult); err != nil {
2 years ago
return err
}
2 years ago
if member != nil {
2 years ago
if err := g.groupMemberDB.NewTx(tx).Create(ctx, []*relationTb.GroupMemberModel{member}); err != nil {
2 years ago
return err
}
2 years ago
return g.cache.DelGroupMembersHash(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelJoinedGroupID(member.UserID).ExecDel(ctx)
2 years ago
}
return nil
2 years ago
})
2 years ago
}
2 years ago
2 years ago
func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
if err := g.groupMemberDB.NewTx(tx).Delete(ctx, groupID, userIDs); err != nil {
2 years ago
return err
}
2 years ago
return g.cache.DelGroupMembersHash(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelJoinedGroupID(userIDs...).DelGroupMembersInfo(groupID, userIDs...).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error) {
2 years ago
return g.cache.GetGroupMemberHashMap(ctx, groupIDs)
2 years ago
}
2 years ago
func (g *groupDatabase) MapGroupMemberNum(ctx context.Context, groupIDs []string) (m map[string]uint32, err error) {
m = make(map[string]uint32)
for _, groupID := range groupIDs {
num, err := g.cache.GetGroupMemberNum(ctx, groupID)
if err != nil {
return nil, err
}
m[groupID] = uint32(num)
}
return m, nil
2 years ago
}
2 years ago
func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
rowsAffected, err := g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, oldOwnerUserID, roleLevel)
2 years ago
if err != nil {
return err
}
if rowsAffected != 1 {
return utils.Wrap(fmt.Errorf("oldOwnerUserID %s rowsAffected = %d", oldOwnerUserID, rowsAffected), "")
}
2 years ago
rowsAffected, err = g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, newOwnerUserID, constant.GroupOwner)
2 years ago
if err != nil {
return err
}
if rowsAffected != 1 {
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
}
2 years ago
return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
if err := g.groupMemberDB.NewTx(tx).Update(ctx, groupID, userID, data); err != nil {
2 years ago
return err
}
2 years ago
return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
2 years ago
var cache = g.cache.NewCache()
for _, item := range data {
2 years ago
if err := g.groupMemberDB.NewTx(tx).Update(ctx, item.GroupID, item.UserID, item.Map); err != nil {
return err
}
2 years ago
cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID)
}
2 years ago
return cache.ExecDel(ctx)
})
}
2 years ago
func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
2 years ago
return g.tx.Transaction(func(tx any) error {
db := g.groupRequestDB.NewTx(tx)
for _, request := range requests {
if err := db.Delete(ctx, request.GroupID, request.UserID); err != nil {
return err
}
}
return db.Create(ctx, requests)
})
2 years ago
}
2 years ago
func (g *groupDatabase) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) {
2 years ago
return g.groupRequestDB.Take(ctx, groupID, userID)
2 years ago
}
2 years ago
func (g *groupDatabase) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error) {
2 years ago
return g.groupRequestDB.Page(ctx, userID, pageNumber, showNumber)
2 years ago
}
2 years ago
func (g *groupDatabase) FindSuperGroup(ctx context.Context, groupIDs []string) (models []*unRelationTb.SuperGroupModel, err error) {
return g.cache.GetSuperGroupMemberIDs(ctx, groupIDs...)
2 years ago
}
2 years ago
func (g *groupDatabase) FindJoinSuperGroup(ctx context.Context, userID string) ([]string, error) {
return g.cache.GetJoinedSuperGroupIDs(ctx, userID)
2 years ago
}
2 years ago
func (g *groupDatabase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
2 years ago
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2 years ago
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
2 years ago
func (g *groupDatabase) DeleteSuperGroup(ctx context.Context, groupID string) error {
2 years ago
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2 years ago
if err := g.mongoDB.DeleteSuperGroup(ctx, groupID); err != nil {
return err
}
models, err := g.cache.GetSuperGroupMemberIDs(ctx, groupID)
if err != nil {
return err
}
cache := g.cache.DelSuperGroupMemberIDs(groupID)
if len(models) > 0 {
cache = cache.DelJoinedSuperGroupIDs(models[0].MemberIDs...)
}
return cache.ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2 years ago
if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
2 years ago
})
2 years ago
}
2 years ago
func (g *groupDatabase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2 years ago
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2 years ago
if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
2 years ago
})
2 years ago
}