Files
open-im-server/pkg/common/db/controller/group.go
T

404 lines
17 KiB
Go
Raw Normal View History

2023-01-28 13:19:36 +08:00
package controller
import (
"context"
2023-02-08 17:23:14 +08:00
"fmt"
2023-03-23 19:02:20 +08:00
2023-03-16 10:46:06 +08:00
"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"
2023-03-03 17:42:26 +08:00
"github.com/dtm-labs/rockscache"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm"
2023-01-28 13:19:36 +08:00
)
2023-02-22 11:35:24 +08:00
type GroupDatabase interface {
2023-02-20 17:43:09 +08:00
// Group
2023-02-14 19:28:57 +08:00
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 // 解散群,并删除群成员
2023-02-16 15:20:59 +08:00
GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error)
2023-02-08 14:41:09 +08:00
// GroupMember
2023-02-09 14:40:49 +08:00
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)
2023-02-09 16:33:40 +08:00
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
2023-02-09 14:40:49 +08:00
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
2023-02-07 17:16:04 +08:00
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
2023-02-10 15:36:25 +08:00
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error)
2023-02-08 17:23:14 +08:00
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
2023-02-09 19:40:45 +08:00
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
2023-02-10 15:36:25 +08:00
UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error
2023-02-08 14:41:09 +08:00
// GroupRequest
2023-02-09 14:40:49 +08:00
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)
2023-02-20 17:43:09 +08:00
// SuperGroupModelInterface
2023-02-15 15:52:32 +08:00
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error)
2023-03-27 15:05:40 +08:00
FindJoinSuperGroup(ctx context.Context, userID string) ([]string, error)
2023-01-29 14:51:59 +08:00
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
2023-02-07 17:16:04 +08:00
DeleteSuperGroup(ctx context.Context, groupID string) error
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
2023-02-08 14:41:09 +08:00
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
2023-01-28 13:19:36 +08:00
}
2023-02-22 11:35:24 +08:00
func NewGroupDatabase(
2023-02-20 17:43:09 +08:00
group relationTb.GroupModelInterface,
member relationTb.GroupMemberModelInterface,
request relationTb.GroupRequestModelInterface,
tx tx.Tx,
ctxTx tx.CtxTx,
2023-03-01 15:32:26 +08:00
superGroup unRelationTb.SuperGroupModelInterface,
cache cache.GroupCache,
2023-02-22 11:35:24 +08:00
) GroupDatabase {
database := &groupDatabase{
2023-02-20 17:43:09 +08:00
groupDB: group,
groupMemberDB: member,
groupRequestDB: request,
tx: tx,
ctxTx: ctxTx,
2023-03-01 15:32:26 +08:00
cache: cache,
mongoDB: superGroup,
2023-01-28 13:19:36 +08:00
}
return database
}
2023-03-03 17:42:26 +08:00
func InitGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, database *mongo.Database) GroupDatabase {
2023-03-08 19:34:36 +08:00
rcOptions := rockscache.NewDefaultOptions()
rcOptions.StrongConsistency = true
rcOptions.RandomExpireAdjustment = 0.2
2023-03-03 17:42:26 +08:00
return NewGroupDatabase(
relation.NewGroupDB(db),
relation.NewGroupMemberDB(db),
relation.NewGroupRequest(db),
tx.NewGorm(db),
tx.NewMongo(database.Client()),
unrelation.NewSuperGroupMongoDriver(database),
2023-03-08 19:34:36 +08:00
cache.NewGroupCacheRedis(rdb, relation.NewGroupDB(db), relation.NewGroupMemberDB(db), relation.NewGroupRequest(db), unrelation.NewSuperGroupMongoDriver(database), rcOptions),
2023-03-03 17:42:26 +08:00
)
}
2023-02-22 11:35:24 +08:00
type groupDatabase struct {
2023-02-09 14:40:49 +08:00
groupDB relationTb.GroupModelInterface
groupMemberDB relationTb.GroupMemberModelInterface
groupRequestDB relationTb.GroupRequestModelInterface
2023-02-20 17:43:09 +08:00
tx tx.Tx
ctxTx tx.CtxTx
2023-03-01 15:32:26 +08:00
cache cache.GroupCache
2023-02-20 17:43:09 +08:00
mongoDB unRelationTb.SuperGroupModelInterface
2023-02-07 18:36:47 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
2023-02-16 17:08:24 +08:00
return g.groupDB.GetGroupIDsByGroupType(ctx, groupType)
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
2023-02-09 17:54:31 +08:00
return g.cache.GetGroupMemberIDs(ctx, groupID)
2023-02-09 16:33:40 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
2023-02-10 19:17:33 +08:00
if len(groups) > 0 {
2023-02-20 17:43:09 +08:00
if err := g.groupDB.NewTx(tx).Create(ctx, groups); err != nil {
2023-02-07 18:36:47 +08:00
return err
}
2023-02-10 19:17:33 +08:00
}
if len(groupMembers) > 0 {
2023-02-20 17:43:09 +08:00
if err := g.groupMemberDB.NewTx(tx).Create(ctx, groupMembers); err != nil {
2023-02-10 19:17:33 +08:00
return err
}
}
2023-03-27 10:07:41 +08:00
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)
2023-02-10 19:17:33 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
2023-02-16 17:08:24 +08:00
return g.cache.GetGroupInfo(ctx, groupID)
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
2023-02-09 17:54:31 +08:00
return g.cache.GetGroupsInfo(ctx, groupIDs)
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
2023-02-08 15:25:25 +08:00
return g.groupDB.Search(ctx, keyword, pageNumber, showNumber)
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
if err := g.groupDB.NewTx(tx).UpdateMap(ctx, groupID, data); err != nil {
2023-02-09 17:54:31 +08:00
return err
}
2023-03-23 19:02:20 +08:00
return g.cache.DelGroupsInfo(groupID).ExecDel(ctx)
2023-02-09 17:54:31 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
if err := g.groupDB.NewTx(tx).UpdateStatus(ctx, groupID, constant.GroupStatusDismissed); err != nil {
2023-02-08 15:25:25 +08:00
return err
}
2023-02-20 17:43:09 +08:00
if err := g.groupMemberDB.NewTx(tx).DeleteGroup(ctx, []string{groupID}); err != nil {
2023-02-09 17:54:31 +08:00
return err
}
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
if err != nil {
return err
}
2023-03-23 19:02:20 +08:00
return g.cache.DelJoinedGroupID(userIDs...).DelGroupsInfo(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID).ExecDel(ctx)
2023-02-08 15:25:25 +08:00
})
2023-02-08 14:41:09 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
2023-02-09 17:54:31 +08:00
return g.cache.GetGroupMemberInfo(ctx, groupID, userID)
2023-01-28 13:19:36 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
2023-02-10 19:17:33 +08:00
return g.groupMemberDB.TakeOwner(ctx, groupID) // todo cache group owner
2023-01-28 13:19:36 +08:00
}
2023-03-29 14:30:38 +08:00
func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) (totalGroupMembers []*relationTb.GroupMemberModel, err error) {
2023-03-27 19:24:16 +08:00
if roleLevels == nil {
2023-03-29 14:30:38 +08:00
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
2023-03-27 19:24:16 +08:00
}
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
2023-01-28 13:19:36 +08:00
}
2023-03-28 19:24:59 +08:00
func (g *groupDatabase) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, totalGroupMembers []*relationTb.GroupMemberModel, err error) {
2023-03-29 10:19:20 +08:00
if roleLevels == nil {
if pageNumber == 0 || showNumber == 0 {
2023-03-29 14:30:38 +08:00
if groupIDs == nil {
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...)
}
}
2023-03-29 15:57:00 +08:00
return uint32(len(totalGroupMembers)), totalGroupMembers, nil
2023-03-29 14:30:38 +08:00
}
2023-03-29 10:19:20 +08:00
for _, groupID := range groupIDs {
2023-03-29 14:30:38 +08:00
groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, userIDs)
2023-03-29 10:19:20 +08:00
if err != nil {
return 0, nil, err
}
totalGroupMembers = append(totalGroupMembers, groupMembers...)
}
2023-03-29 11:01:03 +08:00
return uint32(len(totalGroupMembers)), totalGroupMembers, nil
2023-03-29 10:19:20 +08:00
} else {
2023-03-29 14:42:00 +08:00
if groupIDs == nil {
for _, userID := range userIDs {
groupIDs, err := g.cache.GetJoinedGroupIDs(ctx, userID)
if err != nil {
return 0, nil, err
}
2023-03-29 15:00:30 +08:00
groupIDs = utils.Paginate(groupIDs, int(pageNumber), int(showNumber))
2023-03-29 14:42:00 +08:00
for _, groupID := range groupIDs {
2023-03-29 15:00:30 +08:00
groupMembers, err := g.cache.GetGroupMembersInfo(ctx, groupID, []string{userID})
2023-03-29 14:42:00 +08:00
if err != nil {
return 0, nil, err
}
totalGroupMembers = append(totalGroupMembers, groupMembers...)
}
}
2023-03-29 17:59:52 +08:00
return uint32(len(groupIDs)), totalGroupMembers, nil
2023-03-29 14:42:00 +08:00
}
2023-03-29 17:59:52 +08:00
var totalAll uint32
2023-03-29 10:19:20 +08:00
for _, groupID := range groupIDs {
2023-03-29 17:59:52 +08:00
total, groupMembers, err := g.cache.GetGroupMembersPage(ctx, groupID, userIDs, pageNumber, showNumber)
2023-03-29 10:19:20 +08:00
if err != nil {
return 0, nil, err
}
2023-03-29 17:59:52 +08:00
totalAll += total
2023-03-29 10:19:20 +08:00
totalGroupMembers = append(totalGroupMembers, groupMembers...)
2023-03-28 19:24:59 +08:00
}
2023-03-29 17:59:52 +08:00
return totalAll, totalGroupMembers, nil
2023-03-28 19:24:59 +08:00
}
}
2023-02-08 15:25:25 +08:00
return g.groupMemberDB.SearchMember(ctx, "", groupIDs, userIDs, roleLevels, pageNumber, showNumber)
2023-01-28 13:19:36 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error) {
2023-02-08 15:25:25 +08:00
return g.groupMemberDB.SearchMember(ctx, keyword, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
2023-01-28 13:19:36 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
if err := g.groupRequestDB.NewTx(tx).UpdateHandler(ctx, groupID, userID, handledMsg, handleResult); err != nil {
2023-02-08 17:23:14 +08:00
return err
}
2023-02-09 17:54:31 +08:00
if member != nil {
2023-02-20 17:43:09 +08:00
if err := g.groupMemberDB.NewTx(tx).Create(ctx, []*relationTb.GroupMemberModel{member}); err != nil {
2023-02-09 17:54:31 +08:00
return err
}
2023-03-23 19:02:20 +08:00
return g.cache.DelGroupMembersHash(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelJoinedGroupID(member.UserID).ExecDel(ctx)
2023-02-09 17:54:31 +08:00
}
return nil
2023-02-08 17:23:14 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-01-29 15:23:14 +08:00
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
if err := g.groupMemberDB.NewTx(tx).Delete(ctx, groupID, userIDs); err != nil {
2023-02-09 17:54:31 +08:00
return err
}
2023-03-23 19:02:20 +08:00
return g.cache.DelGroupMembersHash(groupID).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelJoinedGroupID(userIDs...).ExecDel(ctx)
2023-02-09 17:54:31 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error) {
2023-03-23 19:02:20 +08:00
return g.cache.GetGroupMemberHashMap(ctx, groupIDs)
2023-02-07 17:16:04 +08:00
}
2023-03-29 10:19:20 +08:00
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
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
rowsAffected, err := g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, oldOwnerUserID, roleLevel)
2023-02-08 17:23:14 +08:00
if err != nil {
return err
}
if rowsAffected != 1 {
return utils.Wrap(fmt.Errorf("oldOwnerUserID %s rowsAffected = %d", oldOwnerUserID, rowsAffected), "")
}
2023-02-20 17:43:09 +08:00
rowsAffected, err = g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, newOwnerUserID, constant.GroupOwner)
2023-02-08 17:23:14 +08:00
if err != nil {
return err
}
if rowsAffected != 1 {
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
}
2023-03-23 19:02:20 +08:00
return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).ExecDel(ctx)
2023-02-08 17:23:14 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
if err := g.groupMemberDB.NewTx(tx).Update(ctx, groupID, userID, data); err != nil {
2023-02-09 17:54:31 +08:00
return err
}
2023-03-23 19:02:20 +08:00
return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx)
2023-02-09 17:54:31 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error {
2023-02-20 17:43:09 +08:00
return g.tx.Transaction(func(tx any) error {
2023-03-23 19:02:20 +08:00
var cache = g.cache.NewCache()
2023-02-09 19:40:45 +08:00
for _, item := range data {
2023-02-20 17:43:09 +08:00
if err := g.groupMemberDB.NewTx(tx).Update(ctx, item.GroupID, item.UserID, item.Map); err != nil {
2023-02-09 19:40:45 +08:00
return err
}
2023-03-23 19:02:20 +08:00
cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID)
2023-02-09 19:40:45 +08:00
}
2023-03-23 19:02:20 +08:00
return cache.ExecDel(ctx)
2023-02-09 19:40:45 +08:00
})
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
2023-02-08 17:23:14 +08:00
return g.groupRequestDB.Create(ctx, requests)
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) {
2023-02-08 17:23:14 +08:00
return g.groupRequestDB.Take(ctx, groupID, userID)
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error) {
2023-02-08 17:23:14 +08:00
return g.groupRequestDB.Page(ctx, userID, pageNumber, showNumber)
2023-02-07 17:16:04 +08:00
}
2023-03-27 15:05:40 +08:00
func (g *groupDatabase) FindSuperGroup(ctx context.Context, groupIDs []string) (models []*unRelationTb.SuperGroupModel, err error) {
return g.cache.GetSuperGroupMemberIDs(ctx, groupIDs...)
2023-02-07 17:16:04 +08:00
}
2023-03-27 15:05:40 +08:00
func (g *groupDatabase) FindJoinSuperGroup(ctx context.Context, userID string) ([]string, error) {
return g.cache.GetJoinedSuperGroupIDs(ctx, userID)
2023-01-29 15:23:14 +08:00
}
2023-03-27 15:05:40 +08:00
func (g *groupDatabase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
2023-02-20 17:43:09 +08:00
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2023-03-27 15:05:40 +08:00
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx)
2023-02-08 17:23:14 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-01-29 11:15:16 +08:00
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) DeleteSuperGroup(ctx context.Context, groupID string) error {
2023-02-20 17:43:09 +08:00
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2023-03-27 15:05:40 +08:00
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)
2023-02-08 20:08:22 +08:00
})
2023-01-28 17:09:20 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2023-02-20 17:43:09 +08:00
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2023-03-27 15:05:40 +08:00
if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
2023-02-08 20:08:22 +08:00
})
2023-02-07 17:16:04 +08:00
}
2023-02-22 11:35:24 +08:00
func (g *groupDatabase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
2023-02-20 17:43:09 +08:00
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
2023-03-27 15:05:40 +08:00
if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil {
return err
}
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
2023-02-08 20:08:22 +08:00
})
2023-01-28 13:19:36 +08:00
}