mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-30 06:22:44 +08:00
group complete
This commit is contained in:
parent
b3ae69ce94
commit
f20e9e71b7
4
pkg/common/db/cache/conversation.go
vendored
4
pkg/common/db/cache/conversation.go
vendored
@ -22,6 +22,8 @@ type ConversationCache interface {
|
|||||||
GetConversationsFromCache(ownerUserID string, conversationIDList []string, fn DBFun) ([]*table.ConversationModel, error)
|
GetConversationsFromCache(ownerUserID string, conversationIDList []string, fn DBFun) ([]*table.ConversationModel, error)
|
||||||
GetUserAllConversationList(ownerUserID string, fn DBFun) ([]*table.ConversationModel, error)
|
GetUserAllConversationList(ownerUserID string, fn DBFun) ([]*table.ConversationModel, error)
|
||||||
DelConversationFromCache(ownerUserID, conversationID string) error
|
DelConversationFromCache(ownerUserID, conversationID string) error
|
||||||
|
|
||||||
|
GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(ctx context.Context, userID string) ([]string, error)) ([]string, error)
|
||||||
}
|
}
|
||||||
type ConversationRedis struct {
|
type ConversationRedis struct {
|
||||||
rcClient *rockscache.Client
|
rcClient *rockscache.Client
|
||||||
@ -51,7 +53,7 @@ func (c *ConversationCache) getSuperGroupRecvNotNotifyUserIDsKey(groupID string)
|
|||||||
return superGroupRecvMsgNotNotifyUserIDsKey + groupID
|
return superGroupRecvMsgNotNotifyUserIDsKey + groupID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(userID string) ([]string, error)) (conversationIDs []string, err error) {
|
func (c *ConversationCache) GetUserConversationIDs(ctx context.Context, ownerUserID string, f func(ctx context.Context, userID string) ([]string, error)) (conversationIDs []string, err error) {
|
||||||
//getConversationIDs := func() (string, error) {
|
//getConversationIDs := func() (string, error) {
|
||||||
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
|
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
|
||||||
// if err != nil {
|
// if err != nil {
|
||||||
|
11
pkg/common/db/cache/group.go
vendored
11
pkg/common/db/cache/group.go
vendored
@ -2,8 +2,8 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/constant"
|
||||||
"Open_IM/pkg/common/db/localcache"
|
|
||||||
"Open_IM/pkg/common/db/relation"
|
"Open_IM/pkg/common/db/relation"
|
||||||
|
relation2 "Open_IM/pkg/common/db/table/relation"
|
||||||
"Open_IM/pkg/common/db/unrelation"
|
"Open_IM/pkg/common/db/unrelation"
|
||||||
"Open_IM/pkg/common/tracelog"
|
"Open_IM/pkg/common/tracelog"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
@ -14,7 +14,6 @@ import (
|
|||||||
"math/big"
|
"math/big"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,16 +29,16 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GroupCache struct {
|
type GroupCache struct {
|
||||||
group *relation.GroupGorm
|
group relation2.GroupModelInterface
|
||||||
groupMember *relation.GroupMemberGorm
|
groupMember relation2.GroupMemberModelInterface
|
||||||
groupRequest *relation.GroupRequestGorm
|
groupRequest relation2.GroupRequestModelInterface
|
||||||
mongoDB *unrelation.SuperGroupMongoDriver
|
mongoDB *unrelation.SuperGroupMongoDriver
|
||||||
expireTime time.Duration
|
expireTime time.Duration
|
||||||
redisClient *RedisClient
|
redisClient *RedisClient
|
||||||
rcClient *rockscache.Client
|
rcClient *rockscache.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGroupCache(rdb redis.UniversalClient, groupDB *relation.GroupGorm, groupMemberDB *relation.GroupMemberGorm, groupRequestDB *relation.GroupRequestGorm, mongoClient *unrelation.SuperGroupMongoDriver, opts rockscache.Options) *GroupCache {
|
func NewGroupCache(rdb redis.UniversalClient, groupDB relation2.GroupModelInterface, groupMemberDB relation2.GroupMemberModelInterface, groupRequestDB relation2.GroupRequestModelInterface, mongoClient *unrelation.SuperGroupMongoDriver, opts rockscache.Options) *GroupCache {
|
||||||
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
|
return &GroupCache{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
|
||||||
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
|
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
|
||||||
mongoDB: mongoClient,
|
mongoDB: mongoClient,
|
||||||
|
@ -188,7 +188,6 @@ type GroupDataBaseInterface interface {
|
|||||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error)
|
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error)
|
||||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupRequestModel, error)
|
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupRequestModel, error)
|
||||||
// SuperGroup
|
// SuperGroup
|
||||||
//TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation2.SuperGroupModel, err error)
|
|
||||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error)
|
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error)
|
||||||
FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, error)
|
FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, error)
|
||||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||||
@ -222,9 +221,9 @@ func newGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.C
|
|||||||
var _ GroupDataBaseInterface = (*GroupDataBase)(nil)
|
var _ GroupDataBaseInterface = (*GroupDataBase)(nil)
|
||||||
|
|
||||||
type GroupDataBase struct {
|
type GroupDataBase struct {
|
||||||
groupDB *relation.GroupGorm
|
groupDB relation2.GroupModelInterface
|
||||||
groupMemberDB *relation.GroupMemberGorm
|
groupMemberDB relation2.GroupMemberModelInterface
|
||||||
groupRequestDB *relation.GroupRequestGorm
|
groupRequestDB relation2.GroupRequestModelInterface
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
|
||||||
cache *cache.GroupCache
|
cache *cache.GroupCache
|
||||||
@ -354,10 +353,6 @@ func (g *GroupDataBase) PageGroupRequestUser(ctx context.Context, userID string,
|
|||||||
return g.groupRequestDB.Page(ctx, userID, pageNumber, showNumber)
|
return g.groupRequestDB.Page(ctx, userID, pageNumber, showNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (g *GroupDataBase) TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation2.SuperGroupModel, err error) {
|
|
||||||
// return g.mongoDB.GetSuperGroup(ctx, groupID)
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (g *GroupDataBase) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error) {
|
func (g *GroupDataBase) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error) {
|
||||||
return g.mongoDB.FindSuperGroup(ctx, groupIDs)
|
return g.mongoDB.FindSuperGroup(ctx, groupIDs)
|
||||||
}
|
}
|
||||||
@ -367,36 +362,25 @@ func (g *GroupDataBase) FindJoinSuperGroup(ctx context.Context, userID string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
||||||
return MongoTransaction(ctx, g.mongoDB.MgoClient, func(ctx mongo.SessionContext) error {
|
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||||
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDList); err != nil {
|
return s.CreateSuperGroup(ctx, groupID, initMemberIDList, tx)
|
||||||
return err
|
|
||||||
}
|
|
||||||
return g.cache.BatchDelJoinedSuperGroupIDs(ctx, initMemberIDList)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
func (g *GroupDataBase) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||||
return g.mongoDB.DeleteSuperGroup(ctx, groupID)
|
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||||
|
return s.DeleteSuperGroup(ctx, groupID, tx)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
func (g *GroupDataBase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||||
return g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs)
|
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||||
|
return s.RemoverUserFromSuperGroup(ctx, groupID, userIDs, tx)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupDataBase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
func (g *GroupDataBase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||||
return g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs)
|
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
|
||||||
}
|
return s.AddUserToSuperGroup(ctx, groupID, userIDs, tx)
|
||||||
|
})
|
||||||
func MongoTransaction(ctx context.Context, mgo *mongo.Client, fn func(ctx mongo.SessionContext) error) error {
|
|
||||||
sess, err := mgo.StartSession()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
sCtx := mongo.NewSessionContext(ctx, sess)
|
|
||||||
defer sess.EndSession(sCtx)
|
|
||||||
if err := fn(sCtx); err != nil {
|
|
||||||
_ = sess.AbortTransaction(sCtx)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return utils.Wrap(sess.CommitTransaction(sCtx), "")
|
|
||||||
}
|
}
|
||||||
|
@ -1,234 +1,151 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
//type GroupMember struct {
|
import (
|
||||||
// GroupID string `gorm:"column:group_id;primaryKey;"`
|
"Open_IM/pkg/common/constant"
|
||||||
// UserID string `gorm:"column:user_id;primaryKey;"`
|
"Open_IM/pkg/common/db/table/relation"
|
||||||
// NickName string `gorm:"column:nickname"`
|
"Open_IM/pkg/common/tracelog"
|
||||||
// FaceUrl string `gorm:"user_group_face_url"`
|
"Open_IM/pkg/utils"
|
||||||
// RoleLevel int32 `gorm:"column:role_level"`
|
"context"
|
||||||
// JoinTime time.Time `gorm:"column:join_time"`
|
"gorm.io/gorm"
|
||||||
// JoinSource int32 `gorm:"column:join_source"`
|
)
|
||||||
// OperatorUserID string `gorm:"column:operator_user_id"`
|
|
||||||
// Ex string `gorm:"column:ex"`
|
var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
|
||||||
|
|
||||||
|
type GroupMemberGorm struct {
|
||||||
|
DB *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGroupMemberDB(db *gorm.DB) relation.GroupMemberModelInterface {
|
||||||
|
return &GroupMemberGorm{DB: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel, tx ...any) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string, tx ...any) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error) {
|
||||||
|
// defer func() {
|
||||||
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||||
|
// }()
|
||||||
|
// return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(args).Error, "")
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//func InsertIntoGroupMember(toInsertInfo GroupMember) error {
|
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (err error) {
|
||||||
// toInsertInfo.JoinTime = time.Now()
|
defer func() {
|
||||||
// if toInsertInfo.RoleLevel == 0 {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "data", data)
|
||||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...any) (rowsAffected int64, err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "roleLevel", roleLevel)
|
||||||
|
}()
|
||||||
|
db := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
||||||
|
"role_level": roleLevel,
|
||||||
|
})
|
||||||
|
return db.RowsAffected, utils.Wrap(db.Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*relation.GroupMemberModel, err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||||
|
}()
|
||||||
|
db := getDBConn(g.DB, tx)
|
||||||
|
if len(groupIDs) > 0 {
|
||||||
|
db = db.Where("group_id in (?)", groupIDs)
|
||||||
|
}
|
||||||
|
if len(userIDs) > 0 {
|
||||||
|
db = db.Where("user_id in (?)", userIDs)
|
||||||
|
}
|
||||||
|
if len(roleLevels) > 0 {
|
||||||
|
db = db.Where("role_level in (?)", roleLevels)
|
||||||
|
}
|
||||||
|
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (g *GroupMemberGorm) FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*relation.GroupMemberModel, err error) {
|
||||||
|
// defer func() {
|
||||||
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
||||||
|
// }()
|
||||||
|
// db := getDBConn(g.DB, tx)
|
||||||
|
// if len(groupList) > 0 {
|
||||||
|
// db = db.Where("group_id in (?)", groupIDs)
|
||||||
// }
|
// }
|
||||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
// if len(userIDs) > 0 {
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Create(toInsertInfo).Error
|
// db = db.Where("user_id in (?)", userIDs)
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
// }
|
||||||
// return nil
|
// if len(roleLevels) > 0 {
|
||||||
|
// db = db.Where("role_level in (?)", roleLevels)
|
||||||
|
// }
|
||||||
|
// return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
||||||
//}
|
//}
|
||||||
|
|
||||||
//func BatchInsertIntoGroupMember(toInsertInfoList []*GroupMember) error {
|
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...any) (groupMember *relation.GroupMemberModel, err error) {
|
||||||
// for _, toInsertInfo := range toInsertInfoList {
|
defer func() {
|
||||||
// toInsertInfo.JoinTime = time.Now()
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
||||||
// if toInsertInfo.RoleLevel == 0 {
|
}()
|
||||||
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
|
groupMember = &relation.GroupMemberModel{}
|
||||||
// }
|
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
|
||||||
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
|
}
|
||||||
// }
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Create(toInsertInfoList).Error
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func FindGroupMember(userID string) ([]GroupMember, error) {
|
|
||||||
// var groupMemberList []GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("user_id=?", userID).Find(&groupMemberList).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMemberList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberListByGroupID(groupID string) ([]GroupMember, error) {
|
|
||||||
// var groupMemberList []GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Find(&groupMemberList).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMemberList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberIDListByGroupID(groupID string) ([]string, error) {
|
|
||||||
// var groupMemberIDList []string
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Pluck("user_id", &groupMemberIDList).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMemberIDList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberListByGroupIDAndRoleLevel(groupID string, roleLevel int32) ([]GroupMember, error) {
|
|
||||||
// var groupMemberList []GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and role_level=?", groupID, roleLevel).Find(&groupMemberList).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMemberList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberInfoByGroupIDAndUserID(groupID, userID string) (*GroupMember, error) {
|
|
||||||
// var groupMember GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Limit(1).Take(&groupMember).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return &groupMember, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func DeleteGroupMemberByGroupIDAndUserID(groupID, userID string) error {
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=? ", groupID, userID).Delete(GroupMember{}).Error
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func DeleteGroupMemberByGroupID(groupID string) error {
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? ", groupID).Delete(GroupMember{}).Error
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func UpdateGroupMemberInfo(groupMemberInfo GroupMember) error {
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(&groupMemberInfo).Error
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func UpdateGroupMemberInfoByMap(groupMemberInfo GroupMember, m map[string]interface{}) error {
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and user_id=?", groupMemberInfo.GroupID, groupMemberInfo.UserID).Updates(m).Error
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetOwnerManagerByGroupID(groupID string) ([]GroupMember, error) {
|
|
||||||
// var groupMemberList []GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=? and role_level>?", groupID, constant.GroupOrdinaryUsers).Find(&groupMemberList).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMemberList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberNumByGroupID(groupID string) (int64, error) {
|
|
||||||
// var number int64
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Count(&number).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return 0, utils.Wrap(err, "")
|
|
||||||
// }
|
|
||||||
// return number, nil
|
|
||||||
//}
|
|
||||||
|
|
||||||
//func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) {
|
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string, tx ...any) (groupMember *relation.GroupMemberModel, err error) {
|
||||||
// omList, err := GetOwnerManagerByGroupID(groupID)
|
defer func() {
|
||||||
// if err != nil {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
|
||||||
// return nil, err
|
}()
|
||||||
// }
|
groupMember = &relation.GroupMemberModel{}
|
||||||
// for _, v := range omList {
|
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
|
||||||
// if v.RoleLevel == constant.GroupOwner {
|
}
|
||||||
// return &v, nil
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return nil, utils.Wrap(constant.ErrNoGroupOwner, "")
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func IsExistGroupMember(groupID, userID string) bool {
|
|
||||||
// var number int64
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id = ? and user_id = ?", groupID, userID).Count(&number).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// if number != 1 {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// return true
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMemberByGroupID(groupID string, filter int32, begin int32, maxNumber int32) ([]GroupMember, error) {
|
|
||||||
// var memberList []GroupMember
|
|
||||||
// var err error
|
|
||||||
// if filter >= 0 {
|
|
||||||
// memberList, err = GetGroupMemberListByGroupIDAndRoleLevel(groupID, filter) //sorted by join time
|
|
||||||
// } else {
|
|
||||||
// memberList, err = GetGroupMemberListByGroupID(groupID)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// if begin >= int32(len(memberList)) {
|
|
||||||
// return nil, nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// var end int32
|
|
||||||
// if begin+int32(maxNumber) < int32(len(memberList)) {
|
|
||||||
// end = begin + maxNumber
|
|
||||||
// } else {
|
|
||||||
// end = int32(len(memberList))
|
|
||||||
// }
|
|
||||||
// return memberList[begin:end], nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetJoinedGroupIDListByUserID(userID string) ([]string, error) {
|
|
||||||
// memberList, err := FindGroupMember(userID)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// var groupIDList []string
|
|
||||||
// for _, v := range memberList {
|
|
||||||
// groupIDList = append(groupIDList, v.GroupID)
|
|
||||||
// }
|
|
||||||
// return groupIDList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func IsGroupOwnerAdmin(groupID, UserID string) bool {
|
|
||||||
// groupMemberList, err := GetOwnerManagerByGroupID(groupID)
|
|
||||||
// if err != nil {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// for _, v := range groupMemberList {
|
|
||||||
// if v.UserID == UserID && v.RoleLevel > constant.GroupOrdinaryUsers {
|
|
||||||
// return true
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return false
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMembersByGroupIdCMS(groupId string, userName string, showNumber, pageNumber int32) ([]GroupMember, error) {
|
|
||||||
// var groupMembers []GroupMember
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupId).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groupMembers).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return groupMembers, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetGroupMembersCount(groupID, userName string) (int64, error) {
|
|
||||||
// var count int64
|
|
||||||
// if err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Where("group_id=?", groupID).Where(fmt.Sprintf(" nickname like '%%%s%%' ", userName)).Count(&count).Error; err != nil {
|
|
||||||
// return count, err
|
|
||||||
// }
|
|
||||||
// return count, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func UpdateGroupMemberInfoDefaultZero(groupMemberInfo GroupMember, args map[string]interface{}) error {
|
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Model(groupMemberInfo).Updates(args).Error
|
|
||||||
//}
|
|
||||||
|
|
||||||
//
|
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...any) (total int32, groupList []*relation.GroupMemberModel, err error) {
|
||||||
//func SelectGroupList(groupID string) ([]string, error) {
|
defer func() {
|
||||||
// var groupUserID string
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "groupIDs", groupIDs, "userIDs", userIDs, "roleLevels", roleLevels, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groupList", groupList)
|
||||||
// var groupList []string
|
}()
|
||||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
db := getDBConn(g.DB, tx)
|
||||||
// if err != nil {
|
gormIn(&db, "group_id", groupIDs)
|
||||||
// return groupList, err
|
gormIn(&db, "user_id", userIDs)
|
||||||
// }
|
gormIn(&db, "role_level", roleLevels)
|
||||||
//
|
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||||
// rows, err := dbConn.Model(&GroupMember{}).Where("group_id = ?", groupID).Select("user_id").Rows()
|
}
|
||||||
// if err != nil {
|
|
||||||
// return groupList, err
|
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...any) (count map[string]uint32, err error) {
|
||||||
// }
|
defer func() {
|
||||||
// defer rows.Close()
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "count", count)
|
||||||
// for rows.Next() {
|
}()
|
||||||
// rows.Scan(&groupUserID)
|
return mapCount(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs), "group_id")
|
||||||
// groupList = append(groupList, groupUserID)
|
}
|
||||||
// }
|
|
||||||
// return groupList, nil
|
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string, tx ...any) (groupUsers map[string][]string, err error) {
|
||||||
//}
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groupUsers", groupUsers)
|
||||||
|
}()
|
||||||
|
var items []struct {
|
||||||
|
GroupID string `gorm:"group_id"`
|
||||||
|
UserID string `gorm:"user_id"`
|
||||||
|
}
|
||||||
|
if err := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
|
||||||
|
return nil, utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
groupUsers = make(map[string][]string)
|
||||||
|
for _, item := range items {
|
||||||
|
groupUsers[item.GroupID] = append(groupUsers[item.GroupID], item.UserID)
|
||||||
|
}
|
||||||
|
return groupUsers, nil
|
||||||
|
}
|
||||||
|
@ -1,156 +0,0 @@
|
|||||||
package relation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Open_IM/pkg/common/constant"
|
|
||||||
"Open_IM/pkg/common/db/table/relation"
|
|
||||||
"Open_IM/pkg/common/tracelog"
|
|
||||||
"Open_IM/pkg/utils"
|
|
||||||
"context"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GroupMemberGorm struct {
|
|
||||||
DB *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupMemberDB(db *gorm.DB) *GroupMemberGorm {
|
|
||||||
return &GroupMemberGorm{DB: db}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id in (?)", groupID, userIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
//func (g *GroupMemberGorm) Delete(ctx context.Context, groupMembers []*relation.GroupMemberModel, tx ...*gorm.DB) (err error) {
|
|
||||||
// defer func() {
|
|
||||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMembers", groupMembers)
|
|
||||||
// }()
|
|
||||||
// return utils.Wrap(getDBConn(g.DB, tx).Delete(groupMembers).Error, "")
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(args).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "data", data)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(data).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...*gorm.DB) (rowsAffected int64, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "roleLevel", roleLevel)
|
|
||||||
}()
|
|
||||||
db := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
|
||||||
"role_level": roleLevel,
|
|
||||||
})
|
|
||||||
return db.RowsAffected, utils.Wrap(db.Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...*gorm.DB) (groupList []*relation.GroupMemberModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
|
||||||
}()
|
|
||||||
db := getDBConn(g.DB, tx)
|
|
||||||
if len(groupIDs) > 0 {
|
|
||||||
db = db.Where("group_id in (?)", groupIDs)
|
|
||||||
}
|
|
||||||
if len(userIDs) > 0 {
|
|
||||||
db = db.Where("user_id in (?)", userIDs)
|
|
||||||
}
|
|
||||||
if len(roleLevels) > 0 {
|
|
||||||
db = db.Where("role_level in (?)", roleLevels)
|
|
||||||
}
|
|
||||||
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...*gorm.DB) (groupList []*relation.GroupMemberModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "userIDs", userIDs, "groupList", groupList)
|
|
||||||
}()
|
|
||||||
db := getDBConn(g.DB, tx)
|
|
||||||
if len(groupList) > 0 {
|
|
||||||
db = db.Where("group_id in (?)", groupIDs)
|
|
||||||
}
|
|
||||||
if len(userIDs) > 0 {
|
|
||||||
db = db.Where("user_id in (?)", userIDs)
|
|
||||||
}
|
|
||||||
if len(roleLevels) > 0 {
|
|
||||||
db = db.Where("role_level in (?)", roleLevels)
|
|
||||||
}
|
|
||||||
return groupList, utils.Wrap(db.Find(&groupList).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupMember", *groupMember)
|
|
||||||
}()
|
|
||||||
groupMember = &relation.GroupMemberModel{}
|
|
||||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string, tx ...*gorm.DB) (groupMember *relation.GroupMemberModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "groupMember", *groupMember)
|
|
||||||
}()
|
|
||||||
groupMember = &relation.GroupMemberModel{}
|
|
||||||
return groupMember, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groupList []*relation.GroupMemberModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "groupIDs", groupIDs, "userIDs", userIDs, "roleLevels", roleLevels, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groupList", groupList)
|
|
||||||
}()
|
|
||||||
db := getDBConn(g.DB, tx)
|
|
||||||
gormIn(&db, "group_id", groupIDs)
|
|
||||||
gormIn(&db, "user_id", userIDs)
|
|
||||||
gormIn(&db, "role_level", roleLevels)
|
|
||||||
return gormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (count map[string]uint32, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "count", count)
|
|
||||||
}()
|
|
||||||
return mapCount(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs), "group_id")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groupUsers map[string][]string, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groupUsers", groupUsers)
|
|
||||||
}()
|
|
||||||
var items []struct {
|
|
||||||
GroupID string `gorm:"group_id"`
|
|
||||||
UserID string `gorm:"user_id"`
|
|
||||||
}
|
|
||||||
if err := getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
|
|
||||||
return nil, utils.Wrap(err, "")
|
|
||||||
}
|
|
||||||
groupUsers = make(map[string][]string)
|
|
||||||
for _, item := range items {
|
|
||||||
groupUsers[item.GroupID] = append(groupUsers[item.GroupID], item.UserID)
|
|
||||||
}
|
|
||||||
return groupUsers, nil
|
|
||||||
}
|
|
@ -1,98 +1,69 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/db/table/relation"
|
||||||
|
"Open_IM/pkg/common/tracelog"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"fmt"
|
"context"
|
||||||
|
"gorm.io/gorm"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func InsertIntoGroup(groupInfo Group) error {
|
var _ relation.GroupModelInterface = (*GroupGorm)(nil)
|
||||||
if groupInfo.GroupName == "" {
|
|
||||||
groupInfo.GroupName = "Group Chat"
|
|
||||||
}
|
|
||||||
groupInfo.CreateTime = time.Now()
|
|
||||||
|
|
||||||
if groupInfo.NotificationUpdateTime.Unix() < 0 {
|
type GroupGorm struct {
|
||||||
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
|
DB *gorm.DB
|
||||||
}
|
|
||||||
err := GroupDB.Create(groupInfo).Error
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TakeGroupInfoByGroupID(groupID string) (*Group, error) {
|
func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
|
||||||
var groupInfo Group
|
return &GroupGorm{DB: db}
|
||||||
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
|
|
||||||
return &groupInfo, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroupInfoByGroupID(groupID string) (*Group, error) {
|
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...any) (err error) {
|
||||||
var groupInfo Group
|
defer func() {
|
||||||
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||||
return &groupInfo, err
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetGroupInfo(groupInfo Group) error {
|
//func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...any) (err error) {
|
||||||
return GroupDB.Where("group_id=?", groupInfo.GroupID).Updates(&groupInfo).Error
|
// defer func() {
|
||||||
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
||||||
|
// }()
|
||||||
|
// return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupModel{}).Error, "")
|
||||||
|
//}
|
||||||
|
|
||||||
|
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...any) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
type GroupWithNum struct {
|
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (err error) {
|
||||||
Group
|
defer func() {
|
||||||
MemberCount int `gorm:"column:num"`
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, int64, error) {
|
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...any) (groups []*relation.GroupModel, err error) {
|
||||||
var groups []GroupWithNum
|
defer func() {
|
||||||
var count int64
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
||||||
sql := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
|
}()
|
||||||
Where(" name like ? and status != ?", fmt.Sprintf("%%%s%%", groupName), constant.GroupStatusDismissed)
|
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
||||||
if err := sql.Count(&count).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
err := sql.Limit(int(showNumber)).Offset(int(showNumber * (pageNumber - 1))).Find(&groups).Error
|
|
||||||
return groups, count, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetGroups(pageNumber, showNumber int) ([]GroupWithNum, error) {
|
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...any) (group *relation.GroupModel, err error) {
|
||||||
var groups []GroupWithNum
|
group = &relation.GroupModel{}
|
||||||
if err := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
|
defer func() {
|
||||||
Limit(showNumber).Offset(showNumber * (pageNumber - 1)).Find(&groups).Error; err != nil {
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
|
||||||
return groups, err
|
}()
|
||||||
}
|
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
||||||
return groups, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func OperateGroupStatus(groupId string, groupStatus int32) error {
|
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (total int32, groups []*relation.GroupModel, err error) {
|
||||||
group := Group{
|
defer func() {
|
||||||
GroupID: groupId,
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||||
Status: groupStatus,
|
}()
|
||||||
}
|
return gormSearch[relation.GroupModel](getDBConn(g.DB, tx), []string{"name"}, keyword, pageNumber, showNumber)
|
||||||
if err := SetGroupInfo(group); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupsCountNum(group Group) (int32, error) {
|
|
||||||
var count int64
|
|
||||||
if err := GroupDB.Where(" name like ? ", fmt.Sprintf("%%%s%%", group.GroupName)).Count(&count).Error; err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return int32(count), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateGroupInfoDefaultZero(groupID string, args map[string]interface{}) error {
|
|
||||||
return GroupDB.Where("group_id = ? ", groupID).Updates(args).Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGroupIDListByGroupType(groupType int) ([]string, error) {
|
|
||||||
var groupIDList []string
|
|
||||||
if err := GroupDB.Where("group_type = ? ", groupType).Pluck("group_id", &groupIDList).Error; err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return groupIDList, nil
|
|
||||||
}
|
}
|
||||||
|
@ -1,74 +0,0 @@
|
|||||||
package relation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Open_IM/pkg/common/db/table/relation"
|
|
||||||
"Open_IM/pkg/common/tracelog"
|
|
||||||
"Open_IM/pkg/utils"
|
|
||||||
"context"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GroupGorm struct {
|
|
||||||
DB *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupDB(db *gorm.DB) *GroupGorm {
|
|
||||||
return &GroupGorm{DB: db}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) Delete(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Delete(&relation.GroupModel{}).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "args", args)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "status", status)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
//func (g *GroupGorm) Update(ctx context.Context, groups []*relation.GroupModel, tx ...*gorm.DB) (err error) {
|
|
||||||
// defer func() {
|
|
||||||
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
|
||||||
// }()
|
|
||||||
// return utils.Wrap(getDBConn(g.DB, tx).Updates(&groups).Error, "")
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...*gorm.DB) (groups []*relation.GroupModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groups", groups)
|
|
||||||
}()
|
|
||||||
return groups, utils.Wrap(getDBConn(g.DB, tx).Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...*gorm.DB) (group *relation.GroupModel, err error) {
|
|
||||||
group = &relation.GroupModel{}
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "group", group)
|
|
||||||
}()
|
|
||||||
return group, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ?", groupID).Take(group).Error, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groups []*relation.GroupModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "keyword", keyword, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
|
||||||
}()
|
|
||||||
return gormSearch[relation.GroupModel](getDBConn(g.DB, tx), []string{"name"}, keyword, pageNumber, showNumber)
|
|
||||||
}
|
|
@ -1,158 +1,85 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
//
|
import (
|
||||||
//func UpdateGroupRequest(groupRequest GroupRequest) error {
|
"Open_IM/pkg/common/db/table/relation"
|
||||||
// if groupRequest.HandledTime.Unix() < 0 {
|
"Open_IM/pkg/common/tracelog"
|
||||||
// groupRequest.HandledTime = utils.UnixSecondToTime(0)
|
"Open_IM/pkg/utils"
|
||||||
// }
|
"context"
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupRequest.GroupID, groupRequest.UserID).Updates(&groupRequest).Error
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ relation.GroupRequestModelInterface = (*GroupRequestGorm)(nil)
|
||||||
|
|
||||||
|
type GroupRequestGorm struct {
|
||||||
|
DB *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewGroupRequest(db *gorm.DB) relation.GroupRequestModelInterface {
|
||||||
|
return &GroupRequestGorm{
|
||||||
|
DB: db,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||||
|
}()
|
||||||
|
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupRequests).Error, utils.GetSelfFuncName())
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (g *GroupRequestGorm) Delete(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||||
|
// defer func() {
|
||||||
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||||
|
// }()
|
||||||
|
// return utils.Wrap(getDBConn(g.DB, tx).Delete(&groupRequests).Error, utils.GetSelfFuncName())
|
||||||
//}
|
//}
|
||||||
//
|
|
||||||
//func InsertIntoGroupRequest(toInsertInfo GroupRequest) error {
|
//func (g *GroupRequestGorm) UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error) {
|
||||||
// DelGroupRequestByGroupIDAndUserID(toInsertInfo.GroupID, toInsertInfo.UserID)
|
// defer func() {
|
||||||
// if toInsertInfo.HandledTime.Unix() < 0 {
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
||||||
// toInsertInfo.HandledTime = utils.UnixSecondToTime(0)
|
// }()
|
||||||
// }
|
// return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(args).Error, utils.GetSelfFuncName())
|
||||||
// u := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", toInsertInfo.GroupID, toInsertInfo.UserID).Updates(&toInsertInfo)
|
|
||||||
// if u.RowsAffected != 0 {
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// toInsertInfo.ReqTime = time.Now()
|
|
||||||
// if toInsertInfo.HandledTime.Unix() < 0 {
|
|
||||||
// toInsertInfo.HandledTime = utils.UnixSecondToTime(0)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Create(&toInsertInfo).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// return nil
|
|
||||||
//}
|
//}
|
||||||
//
|
|
||||||
//func GetGroupRequestByGroupIDAndUserID(groupID, userID string) (*GroupRequest, error) {
|
func (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (err error) {
|
||||||
// var groupRequest GroupRequest
|
defer func() {
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("user_id=? and group_id=?", userID, groupID).Take(&groupRequest).Error
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "handledMsg", handledMsg, "handleResult", handleResult)
|
||||||
// if err != nil {
|
}()
|
||||||
// return nil, err
|
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(map[string]any{
|
||||||
// }
|
"handle_msg": handledMsg,
|
||||||
// return &groupRequest, nil
|
"handle_result": handleResult,
|
||||||
|
}).Error, utils.GetSelfFuncName())
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (g *GroupRequestGorm) Update(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (err error) {
|
||||||
|
// defer func() {
|
||||||
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
||||||
|
// }()
|
||||||
|
// return utils.Wrap(getDBConn(g.DB, tx).Updates(&groupRequests).Error, utils.GetSelfFuncName())
|
||||||
//}
|
//}
|
||||||
//
|
|
||||||
//func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error {
|
//func (g *GroupRequestGorm) Find(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (resultGroupRequests []*relation.GroupRequestModel, err error) {
|
||||||
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupID, userID).Delete(GroupRequest{}).Error
|
// defer func() {
|
||||||
//}
|
// tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests, "resultGroupRequests", resultGroupRequests)
|
||||||
//
|
// }()
|
||||||
//func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) {
|
// var where [][]interface{}
|
||||||
// var groupRequestList []GroupRequest
|
// for _, groupMember := range groupRequests {
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=?", groupID).Find(&groupRequestList).Error
|
// where = append(where, []interface{}{groupMember.GroupID, groupMember.UserID})
|
||||||
// if err != nil {
|
// }
|
||||||
// return nil, err
|
// return resultGroupRequests, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&resultGroupRequests).Error, utils.GetSelfFuncName())
|
||||||
// }
|
|
||||||
// return groupRequestList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//received
|
|
||||||
//func GetRecvGroupApplicationList(userID string) ([]GroupRequest, error) {
|
|
||||||
// var groupRequestList []GroupRequest
|
|
||||||
// memberList, err := FindGroupMember(userID)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, utils.Wrap(err, utils.GetSelfFuncName())
|
|
||||||
// }
|
|
||||||
// for _, v := range memberList {
|
|
||||||
// if v.RoleLevel > constant.GroupOrdinaryUsers {
|
|
||||||
// list, err := GetGroupRequestByGroupID(v.GroupID)
|
|
||||||
// if err != nil {
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// groupRequestList = append(groupRequestList, list...)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return groupRequestList, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func GetUserReqGroupByUserID(userID string) ([]GroupRequest, error) {
|
|
||||||
// var groupRequestList []GroupRequest
|
|
||||||
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("user_id=?", userID).Find(&groupRequestList).Error
|
|
||||||
// return groupRequestList, err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//func GroupApplicationResponse(pb *group.GroupApplicationResponseReq) (*group.CommonResp, error) {
|
|
||||||
//
|
|
||||||
// ownerUser, err := FindGroupMemberInfoByGroupIdAndUserId(pb.GroupID, pb.OwnerID)
|
|
||||||
// if err != nil {
|
|
||||||
// log.ErrorByKv("FindGroupMemberInfoByGroupIdAndUserId failed", pb.OperationID, "groupId", pb.GroupID, "ownerID", pb.OwnerID)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// if ownerUser.AdministratorLevel <= 0 {
|
|
||||||
// return nil, errors.New("insufficient permissions")
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// var groupRequest GroupRequest
|
|
||||||
// err = dbConn.Raw("select * from `group_request` where handled_user = ? and group_id = ? and from_user_id = ? and to_user_id = ?",
|
|
||||||
// "", pb.GroupID, pb.FromUserID, pb.ToUserID).Scan(&groupRequest).Error
|
|
||||||
// if err != nil {
|
|
||||||
// log.ErrorByKv("find group_request info failed", pb.OperationID, "groupId", pb.GroupID, "fromUserId", pb.FromUserID, "toUserId", pb.OwnerID)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if groupRequest.Flag != 0 {
|
|
||||||
// return nil, errors.New("application has already handle")
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// var saveFlag int
|
|
||||||
// if pb.HandleResult == 0 {
|
|
||||||
// saveFlag = -1
|
|
||||||
// } else if pb.HandleResult == 1 {
|
|
||||||
// saveFlag = 1
|
|
||||||
// } else {
|
|
||||||
// return nil, errors.New("parma HandleResult error")
|
|
||||||
// }
|
|
||||||
// err = dbConn.Exec("update `group_request` set flag = ?, handled_msg = ?, handled_user = ? where group_id = ? and from_user_id = ? and to_user_id = ?",
|
|
||||||
// saveFlag, pb.HandledMsg, pb.OwnerID, groupRequest.GroupID, groupRequest.FromUserID, groupRequest.ToUserID).Error
|
|
||||||
// if err != nil {
|
|
||||||
// log.ErrorByKv("update group request failed", pb.OperationID, "groupID", pb.GroupID, "flag", saveFlag, "ownerId", pb.OwnerID, "fromUserId", pb.FromUserID, "toUserID", pb.ToUserID)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// if saveFlag == 1 {
|
|
||||||
// if groupRequest.ToUserID == "0" {
|
|
||||||
// err = InsertIntoGroupMember(pb.GroupID, pb.FromUserID, groupRequest.FromUserNickname, groupRequest.FromUserFaceUrl, 0)
|
|
||||||
// if err != nil {
|
|
||||||
// log.ErrorByKv("InsertIntoGroupMember failed", pb.OperationID, "groupID", pb.GroupID, "fromUserId", pb.FromUserID)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// err = InsertIntoGroupMember(pb.GroupID, pb.ToUserID, groupRequest.ToUserNickname, groupRequest.ToUserFaceUrl, 0)
|
|
||||||
// if err != nil {
|
|
||||||
// log.ErrorByKv("InsertIntoGroupMember failed", pb.OperationID, "groupID", pb.GroupID, "fromUserId", pb.FromUserID)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// return &group.GroupApplicationResponseResp{}, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func FindGroupBeInvitedRequestInfoByUidAndGroupID(groupId, uid string) (*GroupRequest, error) {
|
|
||||||
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// var beInvitedRequestUserInfo GroupRequest
|
|
||||||
// err = dbConn.Table("group_request").Where("to_user_id=? and group_id=?", uid, groupId).Find(&beInvitedRequestUserInfo).Error
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// return &beInvitedRequestUserInfo, nil
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func InsertGroupRequest(groupId, fromUser, fromUserNickName, fromUserFaceUrl, toUser, requestMsg, handledMsg string, handleStatus int) error {
|
|
||||||
// return nil
|
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID string, tx ...any) (groupRequest *relation.GroupRequestModel, err error) {
|
||||||
|
groupRequest = &relation.GroupRequestModel{}
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupRequest", *groupRequest)
|
||||||
|
}()
|
||||||
|
return groupRequest, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...any) (total int32, groups []*relation.GroupRequestModel, err error) {
|
||||||
|
defer func() {
|
||||||
|
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
||||||
|
}()
|
||||||
|
return gormSearch[relation.GroupRequestModel](getDBConn(g.DB, tx).Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||||
|
}
|
||||||
|
@ -1,83 +0,0 @@
|
|||||||
package relation
|
|
||||||
|
|
||||||
import (
|
|
||||||
"Open_IM/pkg/common/db/table/relation"
|
|
||||||
"Open_IM/pkg/common/tracelog"
|
|
||||||
"Open_IM/pkg/utils"
|
|
||||||
"context"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
type GroupRequestGorm struct {
|
|
||||||
DB *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewGroupRequest(db *gorm.DB) *GroupRequestGorm {
|
|
||||||
return &GroupRequestGorm{
|
|
||||||
DB: db,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupRequests).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Delete(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Delete(&groupRequests).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "args", args)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(args).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "handledMsg", handledMsg, "handleResult", handleResult)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(map[string]any{
|
|
||||||
"handle_msg": handledMsg,
|
|
||||||
"handle_result": handleResult,
|
|
||||||
}).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Update(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests)
|
|
||||||
}()
|
|
||||||
return utils.Wrap(getDBConn(g.DB, tx).Updates(&groupRequests).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Find(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...*gorm.DB) (resultGroupRequests []*relation.GroupRequestModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupRequests", groupRequests, "resultGroupRequests", resultGroupRequests)
|
|
||||||
}()
|
|
||||||
var where [][]interface{}
|
|
||||||
for _, groupMember := range groupRequests {
|
|
||||||
where = append(where, []interface{}{groupMember.GroupID, groupMember.UserID})
|
|
||||||
}
|
|
||||||
return resultGroupRequests, utils.Wrap(getDBConn(g.DB, tx).Where("(group_id, user_id) in ?", where).Find(&resultGroupRequests).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID string, tx ...*gorm.DB) (groupRequest *relation.GroupRequestModel, err error) {
|
|
||||||
groupRequest = &relation.GroupRequestModel{}
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userID", userID, "groupRequest", *groupRequest)
|
|
||||||
}()
|
|
||||||
return groupRequest, utils.Wrap(getDBConn(g.DB, tx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error, utils.GetSelfFuncName())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...*gorm.DB) (total int32, groups []*relation.GroupRequestModel, err error) {
|
|
||||||
defer func() {
|
|
||||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "pageNumber", pageNumber, "showNumber", showNumber, "total", total, "groups", groups)
|
|
||||||
}()
|
|
||||||
return gormSearch[relation.GroupRequestModel](getDBConn(g.DB, tx).Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
|
||||||
}
|
|
@ -87,9 +87,11 @@ func (w Writer) Printf(format string, args ...interface{}) {
|
|||||||
fmt.Printf(format, args...)
|
fmt.Printf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDBConn(db *gorm.DB, tx []*gorm.DB) *gorm.DB {
|
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
|
||||||
if len(tx) > 0 {
|
if len(tx) > 0 {
|
||||||
return tx[0]
|
if txDb, ok := tx[0].(*gorm.DB); ok {
|
||||||
|
return txDb
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GroupModelTableName = "groups"
|
GroupModelTableName = "groups"
|
||||||
@ -29,4 +32,11 @@ func (GroupModel) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GroupModelInterface interface {
|
type GroupModelInterface interface {
|
||||||
|
Create(ctx context.Context, groups []*GroupModel, tx ...any) (err error)
|
||||||
|
//Delete(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||||
|
UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...any) (err error)
|
||||||
|
UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (err error)
|
||||||
|
Find(ctx context.Context, groupIDs []string, tx ...any) (groups []*GroupModel, err error)
|
||||||
|
Take(ctx context.Context, groupID string, tx ...any) (group *GroupModel, err error)
|
||||||
|
Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (total int32, groups []*GroupModel, err error)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GroupMemberModelTableName = "group_members"
|
GroupMemberModelTableName = "group_members"
|
||||||
@ -25,4 +28,17 @@ func (GroupMemberModel) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GroupMemberModelInterface interface {
|
type GroupMemberModelInterface interface {
|
||||||
|
Create(ctx context.Context, groupMemberList []*GroupMemberModel, tx ...any) (err error)
|
||||||
|
Delete(ctx context.Context, groupID string, userIDs []string, tx ...any) (err error)
|
||||||
|
DeleteGroup(ctx context.Context, groupIDs []string, tx ...any) (err error)
|
||||||
|
//UpdateByMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error)
|
||||||
|
Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (err error)
|
||||||
|
UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32, tx ...any) (rowsAffected int64, err error)
|
||||||
|
Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*GroupMemberModel, err error)
|
||||||
|
//FindGroupUser(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, tx ...any) (groupList []*GroupMemberModel, err error)
|
||||||
|
Take(ctx context.Context, groupID string, userID string, tx ...any) (groupMember *GroupMemberModel, err error)
|
||||||
|
TakeOwner(ctx context.Context, groupID string, tx ...any) (groupMember *GroupMemberModel, err error)
|
||||||
|
SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32, tx ...any) (total int32, groupList []*GroupMemberModel, err error)
|
||||||
|
MapGroupMemberNum(ctx context.Context, groupIDs []string, tx ...any) (count map[string]uint32, err error)
|
||||||
|
FindJoinUserID(ctx context.Context, groupIDs []string, tx ...any) (groupUsers map[string][]string, err error)
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GroupRequestModelTableName = "group_requests"
|
GroupRequestModelTableName = "group_requests"
|
||||||
@ -25,4 +28,12 @@ func (GroupRequestModel) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GroupRequestModelInterface interface {
|
type GroupRequestModelInterface interface {
|
||||||
|
Create(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||||
|
//Delete(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||||
|
//UpdateMap(ctx context.Context, groupID string, userID string, args map[string]interface{}, tx ...any) (err error)
|
||||||
|
UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (err error)
|
||||||
|
//Update(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||||
|
//Find(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (resultGroupRequests []*GroupRequestModel, err error)
|
||||||
|
Take(ctx context.Context, groupID string, userID string, tx ...any) (groupRequest *GroupRequestModel, err error)
|
||||||
|
Page(ctx context.Context, userID string, pageNumber, showNumber int32, tx ...any) (total int32, groups []*GroupRequestModel, err error)
|
||||||
}
|
}
|
||||||
|
@ -28,11 +28,22 @@ func (UserToSuperGroupModel) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type SuperGroupModelInterface interface {
|
type SuperGroupModelInterface interface {
|
||||||
// tx is your transaction object
|
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error
|
||||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error
|
FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*SuperGroupModel, err error)
|
||||||
GetSuperGroup(ctx context.Context, groupID string) (SuperGroupModel, error)
|
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||||
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||||
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*UserToSuperGroupModel, error)
|
||||||
GetSuperGroupByUserID(ctx context.Context, userID string) (*UserToSuperGroupModel, error)
|
DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error
|
||||||
DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error
|
RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string, tx ...any) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//type SuperGroupModelInterface interface {
|
||||||
|
// // tx is your transaction object
|
||||||
|
// CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error
|
||||||
|
// FindSuperGroup(ctx context.Context, groupIDs []string, tx ...interface{}) ([]*SuperGroupModel, error)
|
||||||
|
// //GetSuperGroup(ctx context.Context, groupID string) (SuperGroupModel, error)
|
||||||
|
// AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||||
|
// RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error
|
||||||
|
// GetSuperGroupByUserID(ctx context.Context, userID string, tx ...interface{}) (*UserToSuperGroupModel, error)
|
||||||
|
// DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error
|
||||||
|
//}
|
||||||
|
@ -11,6 +11,13 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ unrelation.SuperGroupModelInterface = (*SuperGroupMongoDriver)(nil)
|
||||||
|
|
||||||
|
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
|
||||||
|
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
|
||||||
|
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
|
||||||
|
}
|
||||||
|
|
||||||
type SuperGroupMongoDriver struct {
|
type SuperGroupMongoDriver struct {
|
||||||
MgoClient *mongo.Client
|
MgoClient *mongo.Client
|
||||||
MgoDB *mongo.Database
|
MgoDB *mongo.Database
|
||||||
@ -18,42 +25,161 @@ type SuperGroupMongoDriver struct {
|
|||||||
userToSuperGroupCollection *mongo.Collection
|
userToSuperGroupCollection *mongo.Collection
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
|
// func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error {
|
||||||
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
|
// superGroup := unrelation.SuperGroupModel{
|
||||||
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
|
// GroupID: groupID,
|
||||||
}
|
// MemberIDs: initMemberIDs,
|
||||||
|
// }
|
||||||
|
// coll := getTxCtx(s.superGroupCollection, tx)
|
||||||
|
// _, err := coll.InsertOne(ctx, superGroup)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// opts := &options.UpdateOptions{
|
||||||
|
// Upsert: utils.ToPtr(true),
|
||||||
|
// }
|
||||||
|
// for _, userID := range initMemberIDs {
|
||||||
|
// _, err = coll.UpdateOne(ctx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...interface{}) (groups []*unrelation.SuperGroupModel, err error) {
|
||||||
|
// cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||||
|
// "$in": groupIDs,
|
||||||
|
// }})
|
||||||
|
// if err != nil {
|
||||||
|
// return nil, utils.Wrap(err, "")
|
||||||
|
// }
|
||||||
|
// defer cursor.Close(ctx)
|
||||||
|
// if err := cursor.All(ctx, &groups); err != nil {
|
||||||
|
// return nil, utils.Wrap(err, "")
|
||||||
|
// }
|
||||||
|
// return groups, nil
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error {
|
||||||
|
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||||
|
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||||
|
// _, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||||
|
// if err != nil {
|
||||||
|
// _ = sCtx.AbortTransaction(ctx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// upsert := true
|
||||||
|
// opts := &options.UpdateOptions{
|
||||||
|
// Upsert: &upsert,
|
||||||
|
// }
|
||||||
|
// for _, userID := range userIDs {
|
||||||
|
// _, err = s.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")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return sCtx.CommitTransaction(ctx)
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...interface{}) error {
|
||||||
|
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||||
|
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||||
|
// _, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||||
|
// if err != nil {
|
||||||
|
// _ = sCtx.AbortTransaction(ctx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// err = s.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
||||||
|
// if err != nil {
|
||||||
|
// _ = sCtx.AbortTransaction(ctx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// return sCtx.CommitTransaction(ctx)
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...interface{}) (*unrelation.UserToSuperGroupModel, error) {
|
||||||
|
// //TODO implement me
|
||||||
|
// panic("implement me")
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error {
|
||||||
|
// //TODO implement me
|
||||||
|
// panic("implement me")
|
||||||
|
// }
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) CreateSuperGroup(sCtx mongo.SessionContext, groupID string, initMemberIDs []string) error {
|
func (s *SuperGroupMongoDriver) Transaction(ctx context.Context, fn func(s unrelation.SuperGroupModelInterface, tx any) error) error {
|
||||||
superGroup := unrelation.SuperGroupModel{
|
sess, err := s.MgoClient.StartSession()
|
||||||
GroupID: groupID,
|
|
||||||
MemberIDs: initMemberIDs,
|
|
||||||
}
|
|
||||||
_, err := db.superGroupCollection.InsertOne(sCtx, superGroup)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
upsert := true
|
txCtx := mongo.NewSessionContext(ctx, sess)
|
||||||
opts := &options.UpdateOptions{
|
defer sess.EndSession(txCtx)
|
||||||
Upsert: &upsert,
|
if err := fn(s, txCtx); err != nil {
|
||||||
|
_ = sess.AbortTransaction(txCtx)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return utils.Wrap(sess.CommitTransaction(txCtx), "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SuperGroupMongoDriver) getTxCtx(ctx context.Context, tx []any) context.Context {
|
||||||
|
if len(tx) > 0 {
|
||||||
|
if ctx, ok := tx[0].(mongo.SessionContext); ok {
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
|
||||||
|
//func (s *SuperGroupMongoDriver) Transaction(ctx context.Context, fn func(ctx mongo.SessionContext) error) error {
|
||||||
|
// sess, err := s.MgoClient.StartSession()
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// sCtx := mongo.NewSessionContext(ctx, sess)
|
||||||
|
//
|
||||||
|
// defer sess.EndSession(sCtx)
|
||||||
|
// if err := fn(sCtx); err != nil {
|
||||||
|
// _ = sess.AbortTransaction(sCtx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// return utils.Wrap(sess.CommitTransaction(sCtx), "")
|
||||||
|
//}
|
||||||
|
|
||||||
|
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error {
|
||||||
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
|
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
|
||||||
|
GroupID: groupID,
|
||||||
|
MemberIDs: initMemberIDs,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
for _, userID := range initMemberIDs {
|
for _, userID := range initMemberIDs {
|
||||||
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
_, 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),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (db *SuperGroupMongoDriver) GetSuperGroup(ctx context.Context, groupID string) (*unrelation.SuperGroupModel, error) {
|
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string, tx ...any) (group *unrelation.SuperGroupModel, err error) {
|
||||||
// superGroup := unrelation.SuperGroupModel{}
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
// err := db.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup)
|
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
||||||
// return &superGroup, err
|
return nil, utils.Wrap(err, "")
|
||||||
//}
|
}
|
||||||
|
return group, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*unrelation.SuperGroupModel, err error) {
|
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*unrelation.SuperGroupModel, err error) {
|
||||||
cursor, err := db.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
|
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||||
"$in": groupIDs,
|
"$in": groupIDs,
|
||||||
}})
|
}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,10 +192,11 @@ func (db *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []
|
|||||||
return groups, nil
|
return groups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
|
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||||
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||||
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$addToSet": bson.M{"member_id_list": bson.M{"$each": userIDs}}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
_ = sCtx.AbortTransaction(ctx)
|
||||||
return err
|
return err
|
||||||
@ -79,7 +206,7 @@ func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupI
|
|||||||
Upsert: &upsert,
|
Upsert: &upsert,
|
||||||
}
|
}
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
_, err = db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
_, err = s.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": userID}, bson.M{"$addToSet": bson.M{"group_id_list": groupID}}, opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
_ = sCtx.AbortTransaction(ctx)
|
||||||
return utils.Wrap(err, "transaction failed")
|
return utils.Wrap(err, "transaction failed")
|
||||||
@ -89,15 +216,16 @@ func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupI
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error {
|
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||||
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||||
_, err := db.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
_, err := s.superGroupCollection.UpdateOne(sCtx, bson.M{"group_id": groupID}, bson.M{"$pull": bson.M{"member_id_list": bson.M{"$in": userIDs}}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
_ = sCtx.AbortTransaction(ctx)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = db.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
err = s.RemoveGroupFromUser(sCtx, groupID, userIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
_ = sCtx.AbortTransaction(ctx)
|
||||||
return err
|
return err
|
||||||
@ -106,30 +234,45 @@ func (db *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context,
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
|
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*unrelation.UserToSuperGroupModel, error) {
|
||||||
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
var user unrelation.UserToSuperGroupModel
|
var user unrelation.UserToSuperGroupModel
|
||||||
err := db.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||||
return &user, utils.Wrap(err, "")
|
return &user, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||||
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
group, err := s.TakeSuperGroup(ctx, groupID, tx...)
|
||||||
superGroup := &unrelation.SuperGroupModel{}
|
if err != nil {
|
||||||
_, err := db.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
|
return err
|
||||||
if err != nil {
|
}
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
|
||||||
return err
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
if err = db.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
|
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
|
||||||
_ = sCtx.AbortTransaction(ctx)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return sCtx.CommitTransaction(ctx)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *SuperGroupMongoDriver) RemoveGroupFromUser(sCtx context.Context, groupID string, userIDs []string) error {
|
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||||
_, err := db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
|
// ctx = s.getTxCtx(ctx, tx)
|
||||||
return err
|
// opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
|
||||||
|
// return s.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
|
||||||
|
// superGroup := &unrelation.SuperGroupModel{}
|
||||||
|
// _, err := s.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
|
||||||
|
// if err != nil {
|
||||||
|
// _ = sCtx.AbortTransaction(ctx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// if err = s.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
|
||||||
|
// _ = sCtx.AbortTransaction(ctx)
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// return sCtx.CommitTransaction(ctx)
|
||||||
|
// })
|
||||||
|
//}
|
||||||
|
|
||||||
|
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||||
|
ctx = s.getTxCtx(ctx, tx)
|
||||||
|
_, 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, "")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user