group complete

test-errcode
withchao 2 years ago
parent b3ae69ce94
commit f20e9e71b7

@ -22,6 +22,8 @@ type ConversationCache interface {
GetConversationsFromCache(ownerUserID string, conversationIDList []string, fn DBFun) ([]*table.ConversationModel, error)
GetUserAllConversationList(ownerUserID string, fn DBFun) ([]*table.ConversationModel, 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 {
rcClient *rockscache.Client
@ -51,7 +53,7 @@ func (c *ConversationCache) getSuperGroupRecvNotNotifyUserIDsKey(groupID string)
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) {
// conversationIDs, err := relation.GetConversationIDsByUserID(ownerUserID)
// if err != nil {

@ -2,8 +2,8 @@ package cache
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/localcache"
"Open_IM/pkg/common/db/relation"
relation2 "Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/db/unrelation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
@ -14,7 +14,6 @@ import (
"math/big"
"sort"
"strconv"
"sync"
"time"
)
@ -30,16 +29,16 @@ const (
)
type GroupCache struct {
group *relation.GroupGorm
groupMember *relation.GroupMemberGorm
groupRequest *relation.GroupRequestGorm
group relation2.GroupModelInterface
groupMember relation2.GroupMemberModelInterface
groupRequest relation2.GroupRequestModelInterface
mongoDB *unrelation.SuperGroupMongoDriver
expireTime time.Duration
redisClient *RedisClient
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,
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
mongoDB: mongoClient,

@ -188,7 +188,6 @@ type GroupDataBaseInterface interface {
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relation2.GroupRequestModel, error)
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (int32, []*relation2.GroupRequestModel, error)
// SuperGroup
//TakeSuperGroup(ctx context.Context, groupID string) (superGroup *unrelation2.SuperGroupModel, err error)
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unrelation2.SuperGroupModel, error)
FindJoinSuperGroup(ctx context.Context, userID string) (*unrelation2.UserToSuperGroupModel, 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)
type GroupDataBase struct {
groupDB *relation.GroupGorm
groupMemberDB *relation.GroupMemberGorm
groupRequestDB *relation.GroupRequestGorm
groupDB relation2.GroupModelInterface
groupMemberDB relation2.GroupMemberModelInterface
groupRequestDB relation2.GroupRequestModelInterface
db *gorm.DB
cache *cache.GroupCache
@ -354,10 +353,6 @@ func (g *GroupDataBase) PageGroupRequestUser(ctx context.Context, userID string,
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) {
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 {
return MongoTransaction(ctx, g.mongoDB.MgoClient, func(ctx mongo.SessionContext) error {
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDList); err != nil {
return err
}
return g.cache.BatchDelJoinedSuperGroupIDs(ctx, initMemberIDList)
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
return s.CreateSuperGroup(ctx, groupID, initMemberIDList, tx)
})
}
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 {
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 {
return g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs)
}
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), "")
return g.mongoDB.Transaction(ctx, func(s unrelation2.SuperGroupModelInterface, tx any) error {
return s.AddUserToSuperGroup(ctx, groupID, userIDs, tx)
})
}

@ -1,234 +1,151 @@
package relation
//type GroupMember struct {
// GroupID string `gorm:"column:group_id;primaryKey;"`
// UserID string `gorm:"column:user_id;primaryKey;"`
// NickName string `gorm:"column:nickname"`
// FaceUrl string `gorm:"user_group_face_url"`
// RoleLevel int32 `gorm:"column:role_level"`
// JoinTime time.Time `gorm:"column:join_time"`
// JoinSource int32 `gorm:"column:join_source"`
// OperatorUserID string `gorm:"column:operator_user_id"`
// Ex string `gorm:"column:ex"`
//}
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"
)
//func InsertIntoGroupMember(toInsertInfo GroupMember) error {
// toInsertInfo.JoinTime = time.Now()
// if toInsertInfo.RoleLevel == 0 {
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
// }
// toInsertInfo.MuteEndTime = time.Unix(int64(time.Now().Second()), 0)
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_members").Create(toInsertInfo).Error
// if err != nil {
// return err
// }
// return nil
//}
var _ relation.GroupMemberModelInterface = (*GroupMemberGorm)(nil)
//func BatchInsertIntoGroupMember(toInsertInfoList []*GroupMember) error {
// for _, toInsertInfo := range toInsertInfoList {
// toInsertInfo.JoinTime = time.Now()
// if toInsertInfo.RoleLevel == 0 {
// toInsertInfo.RoleLevel = constant.GroupOrdinaryUsers
// }
// 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
//}
type GroupMemberGorm struct {
DB *gorm.DB
}
//func GetGroupOwnerInfoByGroupID(groupID string) (*GroupMember, error) {
// omList, err := GetOwnerManagerByGroupID(groupID)
// if err != nil {
// return nil, err
// }
// for _, v := range omList {
// 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 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 SelectGroupList(groupID string) ([]string, error) {
// var groupUserID string
// var groupList []string
// dbConn, err := DB.DB.MysqlDB.DefaultGormDB()
// if err != nil {
// return groupList, err
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (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 ...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)
// }
//
// rows, err := dbConn.Model(&GroupMember{}).Where("group_id = ?", groupID).Select("user_id").Rows()
// if err != nil {
// return groupList, err
// if len(userIDs) > 0 {
// db = db.Where("user_id in (?)", userIDs)
// }
// defer rows.Close()
// for rows.Next() {
// rows.Scan(&groupUserID)
// groupList = append(groupList, groupUserID)
// if len(roleLevels) > 0 {
// db = db.Where("role_level in (?)", roleLevels)
// }
// return groupList, nil
// return groupList, utils.Wrap(db.Find(&groupList).Error, "")
//}
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string, tx ...any) (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 ...any) (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 ...any) (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 ...any) (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 ...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
import (
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"fmt"
"time"
"context"
"gorm.io/gorm"
)
func InsertIntoGroup(groupInfo Group) error {
if groupInfo.GroupName == "" {
groupInfo.GroupName = "Group Chat"
}
groupInfo.CreateTime = time.Now()
if groupInfo.NotificationUpdateTime.Unix() < 0 {
groupInfo.NotificationUpdateTime = utils.UnixSecondToTime(0)
}
err := GroupDB.Create(groupInfo).Error
if err != nil {
return err
}
return nil
}
func TakeGroupInfoByGroupID(groupID string) (*Group, error) {
var groupInfo Group
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
return &groupInfo, err
}
var _ relation.GroupModelInterface = (*GroupGorm)(nil)
func GetGroupInfoByGroupID(groupID string) (*Group, error) {
var groupInfo Group
err := GroupDB.Where("group_id=?", groupID).Take(&groupInfo).Error
return &groupInfo, err
type GroupGorm struct {
DB *gorm.DB
}
func SetGroupInfo(groupInfo Group) error {
return GroupDB.Where("group_id=?", groupInfo.GroupID).Updates(&groupInfo).Error
func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
return &GroupGorm{DB: db}
}
type GroupWithNum struct {
Group
MemberCount int `gorm:"column:num"`
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...any) (err error) {
defer func() {
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
}()
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
}
func GetGroupsByName(groupName string, pageNumber, showNumber int32) ([]GroupWithNum, int64, error) {
var groups []GroupWithNum
var count int64
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)
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 (g *GroupGorm) Delete(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.GroupModel{}).Error, "")
//}
func GetGroups(pageNumber, showNumber int) ([]GroupWithNum, error) {
var groups []GroupWithNum
if err := GroupDB.Select("groups.*, (select count(*) from group_members where group_members.group_id=groups.group_id) as num").
Limit(showNumber).Offset(showNumber * (pageNumber - 1)).Find(&groups).Error; err != nil {
return groups, err
}
return groups, nil
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, "")
}
func OperateGroupStatus(groupId string, groupStatus int32) error {
group := Group{
GroupID: groupId,
Status: groupStatus,
}
if err := SetGroupInfo(group); err != nil {
return err
}
return nil
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (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 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 (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...any) (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 UpdateGroupInfoDefaultZero(groupID string, args map[string]interface{}) error {
return GroupDB.Where("group_id = ? ", groupID).Updates(args).Error
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...any) (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 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
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (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,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
//
//func UpdateGroupRequest(groupRequest GroupRequest) error {
// if groupRequest.HandledTime.Unix() < 0 {
// groupRequest.HandledTime = utils.UnixSecondToTime(0)
// }
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupRequest.GroupID, groupRequest.UserID).Updates(&groupRequest).Error
//}
//
//func InsertIntoGroupRequest(toInsertInfo GroupRequest) error {
// DelGroupRequestByGroupIDAndUserID(toInsertInfo.GroupID, toInsertInfo.UserID)
// if toInsertInfo.HandledTime.Unix() < 0 {
// toInsertInfo.HandledTime = utils.UnixSecondToTime(0)
// }
// 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) {
// var groupRequest GroupRequest
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("user_id=? and group_id=?", userID, groupID).Take(&groupRequest).Error
// if err != nil {
// return nil, err
// }
// return &groupRequest, nil
//}
//
//func DelGroupRequestByGroupIDAndUserID(groupID, userID string) error {
// return DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=? and user_id=?", groupID, userID).Delete(GroupRequest{}).Error
//}
//
//func GetGroupRequestByGroupID(groupID string) ([]GroupRequest, error) {
// var groupRequestList []GroupRequest
// err := DB.DB.MysqlDB.DefaultGormDB().Table("group_requests").Where("group_id=?", groupID).Find(&groupRequestList).Error
// if err != nil {
// return nil, err
// }
// 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
import (
"Open_IM/pkg/common/db/table/relation"
"Open_IM/pkg/common/tracelog"
"Open_IM/pkg/utils"
"context"
"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 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 (g *GroupRequestGorm) UpdateMap(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.GroupRequestModel{}).Where("group_id = ? and user_id = ? ", groupID, userID).Updates(args).Error, utils.GetSelfFuncName())
//}
//
//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 (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (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 ...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 InsertGroupRequest(groupId, fromUser, fromUserNickName, fromUserFaceUrl, toUser, requestMsg, handledMsg string, handleStatus int) error {
// return nil
//func (g *GroupRequestGorm) Find(ctx context.Context, groupRequests []*relation.GroupRequestModel, tx ...any) (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 ...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...)
}
func getDBConn(db *gorm.DB, tx []*gorm.DB) *gorm.DB {
func getDBConn(db *gorm.DB, tx []any) *gorm.DB {
if len(tx) > 0 {
return tx[0]
if txDb, ok := tx[0].(*gorm.DB); ok {
return txDb
}
}
return db
}

@ -1,6 +1,9 @@
package relation
import "time"
import (
"context"
"time"
)
const (
GroupModelTableName = "groups"
@ -29,4 +32,11 @@ func (GroupModel) TableName() string {
}
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
import "time"
import (
"context"
"time"
)
const (
GroupMemberModelTableName = "group_members"
@ -25,4 +28,17 @@ func (GroupMemberModel) TableName() string {
}
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
import "time"
import (
"context"
"time"
)
const (
GroupRequestModelTableName = "group_requests"
@ -25,4 +28,12 @@ func (GroupRequestModel) TableName() string {
}
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 {
// tx is your transaction object
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) 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) (*UserToSuperGroupModel, error)
DeleteSuperGroup(ctx context.Context, groupID string, tx ...interface{}) error
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...any) error
FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*SuperGroupModel, err error)
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error
GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*UserToSuperGroupModel, error)
DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) 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"
)
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 {
MgoClient *mongo.Client
MgoDB *mongo.Database
@ -18,42 +25,161 @@ type SuperGroupMongoDriver struct {
userToSuperGroupCollection *mongo.Collection
}
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)}
// func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string, tx ...interface{}) error {
// superGroup := unrelation.SuperGroupModel{
// 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 (s *SuperGroupMongoDriver) Transaction(ctx context.Context, fn func(s unrelation.SuperGroupModelInterface, tx any) error) error {
sess, err := s.MgoClient.StartSession()
if err != nil {
return err
}
txCtx := mongo.NewSessionContext(ctx, sess)
defer sess.EndSession(txCtx)
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 (db *SuperGroupMongoDriver) CreateSuperGroup(sCtx mongo.SessionContext, groupID string, initMemberIDs []string) error {
superGroup := unrelation.SuperGroupModel{
//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,
}
_, err := db.superGroupCollection.InsertOne(sCtx, superGroup)
})
if err != nil {
return err
}
upsert := true
opts := &options.UpdateOptions{
Upsert: &upsert,
}
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 {
return err
}
}
return nil
}
//func (db *SuperGroupMongoDriver) GetSuperGroup(ctx context.Context, groupID string) (*unrelation.SuperGroupModel, error) {
// superGroup := unrelation.SuperGroupModel{}
// err := db.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&superGroup)
// return &superGroup, err
//}
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string, tx ...any) (group *unrelation.SuperGroupModel, err error) {
ctx = s.getTxCtx(ctx, tx)
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
return nil, utils.Wrap(err, "")
}
return group, nil
}
func (db *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*unrelation.SuperGroupModel, err error) {
cursor, err := db.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*unrelation.SuperGroupModel, err error) {
ctx = s.getTxCtx(ctx, tx)
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
"$in": groupIDs,
}})
if err != nil {
@ -66,10 +192,11 @@ func (db *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []
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())
return db.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}}})
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
@ -79,7 +206,7 @@ func (db *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupI
Upsert: &upsert,
}
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 {
_ = sCtx.AbortTransaction(ctx)
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())
return db.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}}})
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 = db.RemoveGroupFromUser(sCtx, groupID, userIDs)
err = s.RemoveGroupFromUser(sCtx, groupID, userIDs)
if err != nil {
_ = sCtx.AbortTransaction(ctx)
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
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, "")
}
func (db *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
opts := options.Session().SetDefaultReadConcern(readconcern.Majority())
return db.MgoDB.Client().UseSessionWithOptions(ctx, opts, func(sCtx mongo.SessionContext) error {
superGroup := &unrelation.SuperGroupModel{}
_, err := db.superGroupCollection.DeleteOne(sCtx, bson.M{"group_id": groupID})
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
ctx = s.getTxCtx(ctx, tx)
group, err := s.TakeSuperGroup(ctx, groupID, tx...)
if err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
}
if err = db.RemoveGroupFromUser(sCtx, groupID, superGroup.MemberIDs); err != nil {
_ = sCtx.AbortTransaction(ctx)
return err
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
return utils.Wrap(err, "")
}
return sCtx.CommitTransaction(ctx)
})
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
}
func (db *SuperGroupMongoDriver) RemoveGroupFromUser(sCtx context.Context, groupID string, userIDs []string) error {
_, err := db.userToSuperGroupCollection.UpdateOne(sCtx, bson.M{"user_id": bson.M{"$in": userIDs}}, bson.M{"$pull": bson.M{"group_id_list": groupID}})
return err
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
// ctx = s.getTxCtx(ctx, tx)
// 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…
Cancel
Save