mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-10-09 10:46:33 +08:00
group db tx
This commit is contained in:
parent
6ef8228064
commit
762c49625c
@ -69,10 +69,10 @@ func KickGroupMember(c *gin.Context) {
|
||||
// 默认 全部自动
|
||||
NewRpc(NewApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember).Execute()
|
||||
// 可以自定义编辑请求和响应
|
||||
a := NewRpc(NewApiBind[api_struct.KickGroupMemberReq, api_struct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
a.Before(func(apiReq *api_struct.KickGroupMemberReq, rpcReq *group.KickGroupMemberReq, bind func() error) error {
|
||||
a := NewRpc(NewApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
a.Before(func(apiReq *apistruct.KickGroupMemberReq, rpcReq *group.KickGroupMemberReq, bind func() error) error {
|
||||
return bind()
|
||||
}).After(func(rpcResp *group.KickGroupMemberResp, apiResp *api_struct.KickGroupMemberResp, bind func() error) error {
|
||||
}).After(func(rpcResp *group.KickGroupMemberResp, apiResp *apistruct.KickGroupMemberResp, bind func() error) error {
|
||||
return bind()
|
||||
}).Execute()
|
||||
}
|
||||
|
@ -12,10 +12,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
//func GetUsersInfo(ctx context.Context, args ...interface{}) ([]*sdkws.UserInfo, error) {
|
||||
// return nil, errors.New("TODO:GetUserInfo")
|
||||
//}
|
||||
|
||||
func NewUserCheck(zk discoveryRegistry.SvcDiscoveryRegistry) *UserCheck {
|
||||
return &UserCheck{
|
||||
zk: zk,
|
||||
|
@ -3,6 +3,7 @@ package group
|
||||
import (
|
||||
"Open_IM/internal/common/check"
|
||||
"Open_IM/internal/common/notification"
|
||||
"Open_IM/internal/tx"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/controller"
|
||||
@ -44,7 +45,15 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
|
||||
return err
|
||||
}
|
||||
pbGroup.RegisterGroupServer(server, &groupServer{
|
||||
GroupInterface: controller.NewGroupInterface(controller.NewGroupDatabase(db, redis.GetClient(), mongo.GetClient())),
|
||||
GroupInterface: controller.NewGroupController(
|
||||
relation.NewGroupDB(db),
|
||||
relation.NewGroupMemberDB(db),
|
||||
relation.NewGroupRequest(db),
|
||||
tx.NewGorm(db),
|
||||
tx.NewMongo(mongo.GetClient()),
|
||||
unrelation.NewSuperGroupMongoDriver(mongo.GetClient()),
|
||||
redis.GetClient(),
|
||||
),
|
||||
UserCheck: check.NewUserCheck(client),
|
||||
ConversationChecker: check.NewConversationChecker(client),
|
||||
})
|
||||
@ -52,7 +61,7 @@ func Start(client *openKeeper.ZkClient, server *grpc.Server) error {
|
||||
}
|
||||
|
||||
type groupServer struct {
|
||||
GroupInterface controller.GroupInterface
|
||||
GroupInterface controller.GroupController
|
||||
UserCheck *check.UserCheck
|
||||
Notification *notification.Check
|
||||
ConversationChecker *check.ConversationChecker
|
||||
|
19
internal/tx/gorm.go
Normal file
19
internal/tx/gorm.go
Normal file
@ -0,0 +1,19 @@
|
||||
package tx
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewGorm(db *gorm.DB) Tx {
|
||||
return &_Gorm{tx: db}
|
||||
}
|
||||
|
||||
type _Gorm struct {
|
||||
tx *gorm.DB
|
||||
}
|
||||
|
||||
func (g *_Gorm) Transaction(fn func(tx any) error) error {
|
||||
return g.tx.Transaction(func(tx *gorm.DB) error {
|
||||
return fn(tx)
|
||||
})
|
||||
}
|
31
internal/tx/mongo.go
Normal file
31
internal/tx/mongo.go
Normal file
@ -0,0 +1,31 @@
|
||||
package tx
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
func NewMongo(client *mongo.Client) CtxTx {
|
||||
return &_Mongo{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type _Mongo struct {
|
||||
client *mongo.Client
|
||||
}
|
||||
|
||||
func (m *_Mongo) Transaction(ctx context.Context, fn func(ctx context.Context) error) error {
|
||||
sess, err := m.client.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), "")
|
||||
}
|
11
internal/tx/tx.go
Normal file
11
internal/tx/tx.go
Normal file
@ -0,0 +1,11 @@
|
||||
package tx
|
||||
|
||||
import "context"
|
||||
|
||||
type Tx interface {
|
||||
Transaction(fn func(tx any) error) error
|
||||
}
|
||||
|
||||
type CtxTx interface {
|
||||
Transaction(ctx context.Context, fn func(ctx context.Context) error) error
|
||||
}
|
25
pkg/common/db/cache/group.go
vendored
25
pkg/common/db/cache/group.go
vendored
@ -2,7 +2,7 @@ package cache
|
||||
|
||||
import (
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
unrelation2 "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@ -51,17 +51,36 @@ type GroupCache interface {
|
||||
DelGroupMemberNum(ctx context.Context, groupID string) (err error)
|
||||
}
|
||||
|
||||
type GroupCacheRedisInterface interface {
|
||||
GetGroupsInfo(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
GetGroupInfo(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
BatchDelJoinedSuperGroupIDs(ctx context.Context, userIDs []string) (err error)
|
||||
DelJoinedSuperGroupIDs(ctx context.Context, userID string) (err error)
|
||||
GetJoinedSuperGroupIDs(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error)
|
||||
GetGroupMembersHash(ctx context.Context, groupID string) (hashCodeUint64 uint64, err error)
|
||||
GetGroupMemberHash1(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error)
|
||||
DelGroupMembersHash(ctx context.Context, groupID string) (err error)
|
||||
GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error)
|
||||
DelGroupMemberIDs(ctx context.Context, groupID string) (err error)
|
||||
DelJoinedGroupID(ctx context.Context, userID string) (err error)
|
||||
GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
DelGroupMemberInfo(ctx context.Context, groupID, userID string) (err error)
|
||||
DelGroupMemberNum(ctx context.Context, groupID string) (err error)
|
||||
DelGroupInfo(ctx context.Context, groupID string) (err error)
|
||||
DelGroupsInfo(ctx context.Context, groupIDs []string) error
|
||||
}
|
||||
|
||||
type GroupCacheRedis struct {
|
||||
group relationTb.GroupModelInterface
|
||||
groupMember relationTb.GroupMemberModelInterface
|
||||
groupRequest relationTb.GroupRequestModelInterface
|
||||
mongoDB *unrelation.SuperGroupMongoDriver
|
||||
mongoDB unrelation2.SuperGroupModelInterface
|
||||
expireTime time.Duration
|
||||
redisClient *RedisClient
|
||||
rcClient *rockscache.Client
|
||||
}
|
||||
|
||||
func NewGroupCacheRedis(rdb redis.UniversalClient, groupDB relationTb.GroupModelInterface, groupMemberDB relationTb.GroupMemberModelInterface, groupRequestDB relationTb.GroupRequestModelInterface, mongoClient *unrelation.SuperGroupMongoDriver, opts rockscache.Options) *GroupCacheRedis {
|
||||
func NewGroupCacheRedis(rdb redis.UniversalClient, groupDB relationTb.GroupModelInterface, groupMemberDB relationTb.GroupMemberModelInterface, groupRequestDB relationTb.GroupRequestModelInterface, mongoClient unrelation2.SuperGroupModelInterface, opts rockscache.Options) GroupCacheRedisInterface {
|
||||
return &GroupCacheRedis{rcClient: rockscache.NewClient(rdb, opts), expireTime: groupExpireTime,
|
||||
group: groupDB, groupMember: groupMemberDB, groupRequest: groupRequestDB, redisClient: NewRedisClient(rdb),
|
||||
mongoDB: mongoClient,
|
||||
|
@ -1,25 +1,21 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"Open_IM/internal/tx"
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/common/db/cache"
|
||||
"Open_IM/pkg/common/db/relation"
|
||||
relationTb "Open_IM/pkg/common/db/table/relation"
|
||||
unRelationTb "Open_IM/pkg/common/db/table/unrelation"
|
||||
"Open_IM/pkg/common/db/unrelation"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/dtm-labs/rockscache"
|
||||
_ "github.com/dtm-labs/rockscache"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
//type GroupInterface GroupDataBaseInterface
|
||||
|
||||
type GroupInterface interface {
|
||||
type GroupController interface {
|
||||
// Group
|
||||
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
@ -45,174 +41,7 @@ type GroupInterface interface {
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error)
|
||||
// SuperGroup
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (superGroup *unRelationTb.UserToSuperGroupModel, err error)
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||
DeleteSuperGroup(ctx context.Context, groupID string) error
|
||||
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
}
|
||||
|
||||
var _ GroupInterface = (*GroupController)(nil)
|
||||
|
||||
func NewGroupInterface(database GroupDataBaseInterface) GroupInterface {
|
||||
return &GroupController{database: database}
|
||||
}
|
||||
|
||||
type GroupController struct {
|
||||
database GroupDataBaseInterface
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error) {
|
||||
return g.database.FindGroupMemberUserID(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
return g.database.CreateGroup(ctx, groups, groupMembers)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
return g.database.TakeGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
return g.database.FindGroup(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error) {
|
||||
return g.database.SearchGroup(ctx, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||||
return g.database.UpdateGroup(ctx, groupID, data)
|
||||
}
|
||||
|
||||
func (g *GroupController) DismissGroup(ctx context.Context, groupID string) error {
|
||||
return g.database.DismissGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
||||
return g.database.GetGroupIDsByGroupType(ctx, groupType)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error) {
|
||||
return g.database.TakeGroupMember(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error) {
|
||||
return g.database.TakeGroupOwner(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error) {
|
||||
return g.database.FindGroupMember(ctx, groupIDs, userIDs, roleLevels)
|
||||
}
|
||||
|
||||
func (g *GroupController) PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error) {
|
||||
return g.database.PageGroupMember(ctx, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error) {
|
||||
return g.database.SearchGroupMember(ctx, keyword, groupIDs, userIDs, roleLevels, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||
return g.database.HandlerGroupRequest(ctx, groupID, userID, handledMsg, handleResult, member)
|
||||
}
|
||||
|
||||
func (g *GroupController) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.database.DeleteGroupMember(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error) {
|
||||
return g.database.MapGroupMemberUserID(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error) {
|
||||
return g.database.MapGroupMemberNum(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error {
|
||||
return g.database.TransferGroupOwner(ctx, groupID, oldOwnerUserID, newOwnerUserID, roleLevel)
|
||||
}
|
||||
|
||||
func (g *GroupController) UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error {
|
||||
return g.database.UpdateGroupMembers(ctx, data)
|
||||
}
|
||||
|
||||
func (g *GroupController) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
|
||||
return g.database.UpdateGroupMember(ctx, groupID, userID, data)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error {
|
||||
return g.database.CreateGroupRequest(ctx, requests)
|
||||
}
|
||||
|
||||
func (g *GroupController) TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) {
|
||||
return g.database.TakeGroupRequest(ctx, groupID, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error) {
|
||||
return g.database.PageGroupRequestUser(ctx, userID, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error) {
|
||||
return g.database.FindSuperGroup(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) FindJoinSuperGroup(ctx context.Context, userID string) (*unRelationTb.UserToSuperGroupModel, error) {
|
||||
return g.database.FindJoinSuperGroup(ctx, userID)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
||||
return g.database.CreateSuperGroup(ctx, groupID, initMemberIDList)
|
||||
}
|
||||
|
||||
func (g *GroupController) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||
return g.database.DeleteSuperGroup(ctx, groupID)
|
||||
}
|
||||
|
||||
func (g *GroupController) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.database.DeleteSuperGroupMember(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
func (g *GroupController) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.database.CreateSuperGroupMember(ctx, groupID, userIDs)
|
||||
}
|
||||
|
||||
type Group interface {
|
||||
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error)
|
||||
}
|
||||
|
||||
type GroupMember interface {
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error)
|
||||
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
|
||||
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
|
||||
UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error
|
||||
}
|
||||
|
||||
type GroupRequest interface {
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error)
|
||||
}
|
||||
|
||||
type SuperGroup interface {
|
||||
// SuperGroupModelInterface
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (*unRelationTb.UserToSuperGroupModel, error)
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||
@ -221,82 +50,40 @@ type SuperGroup interface {
|
||||
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
}
|
||||
|
||||
type GroupDataBase1 interface {
|
||||
Group
|
||||
GroupMember
|
||||
GroupRequest
|
||||
SuperGroup
|
||||
}
|
||||
|
||||
type GroupDataBaseInterface interface {
|
||||
CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error
|
||||
TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error)
|
||||
FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error)
|
||||
SearchGroup(ctx context.Context, keyword string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupModel, error)
|
||||
UpdateGroup(ctx context.Context, groupID string, data map[string]any) error
|
||||
DismissGroup(ctx context.Context, groupID string) error // 解散群,并删除群成员
|
||||
GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error)
|
||||
|
||||
// GroupMember
|
||||
TakeGroupMember(ctx context.Context, groupID string, userID string) (groupMember *relationTb.GroupMemberModel, err error)
|
||||
TakeGroupOwner(ctx context.Context, groupID string) (*relationTb.GroupMemberModel, error)
|
||||
FindGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) ([]*relationTb.GroupMemberModel, error)
|
||||
FindGroupMemberUserID(ctx context.Context, groupID string) ([]string, error)
|
||||
PageGroupMember(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
SearchGroupMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (uint32, []*relationTb.GroupMemberModel, error)
|
||||
HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error
|
||||
DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
MapGroupMemberUserID(ctx context.Context, groupIDs []string) (map[string]*relationTb.GroupSimpleUserID, error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string) (map[string]uint32, error)
|
||||
TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error // 转让群
|
||||
UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error
|
||||
UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error
|
||||
// GroupRequest
|
||||
CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error
|
||||
TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error)
|
||||
PageGroupRequestUser(ctx context.Context, userID string, pageNumber, showNumber int32) (uint32, []*relationTb.GroupRequestModel, error)
|
||||
// SuperGroup
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) ([]*unRelationTb.SuperGroupModel, error)
|
||||
FindJoinSuperGroup(ctx context.Context, userID string) (*unRelationTb.UserToSuperGroupModel, error)
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error
|
||||
DeleteSuperGroup(ctx context.Context, groupID string) error
|
||||
DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error
|
||||
}
|
||||
|
||||
func NewGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, mgoClient *mongo.Client) GroupDataBaseInterface {
|
||||
groupDB := relation.NewGroupDB(db)
|
||||
groupMemberDB := relation.NewGroupMemberDB(db)
|
||||
groupRequestDB := relation.NewGroupRequest(db)
|
||||
newDB := *db
|
||||
SuperGroupMongoDriver := unrelation.NewSuperGroupMongoDriver(mgoClient)
|
||||
func NewGroupController(
|
||||
group relationTb.GroupModelInterface,
|
||||
member relationTb.GroupMemberModelInterface,
|
||||
request relationTb.GroupRequestModelInterface,
|
||||
tx tx.Tx,
|
||||
ctxTx tx.CtxTx,
|
||||
super unRelationTb.SuperGroupModelInterface,
|
||||
client redis.UniversalClient,
|
||||
) GroupController {
|
||||
database := &GroupDataBase{
|
||||
groupDB: groupDB,
|
||||
groupMemberDB: groupMemberDB,
|
||||
groupRequestDB: groupRequestDB,
|
||||
db: &newDB,
|
||||
cache: cache.NewGroupCacheRedis(rdb, groupDB, groupMemberDB, groupRequestDB, SuperGroupMongoDriver, rockscache.Options{
|
||||
groupDB: group,
|
||||
groupMemberDB: member,
|
||||
groupRequestDB: request,
|
||||
tx: tx,
|
||||
ctxTx: ctxTx,
|
||||
cache: cache.NewGroupCacheRedis(client, group, member, request, super, rockscache.Options{
|
||||
RandomExpireAdjustment: 0.2,
|
||||
DisableCacheRead: false,
|
||||
DisableCacheDelete: false,
|
||||
StrongConsistency: true,
|
||||
}),
|
||||
mongoDB: SuperGroupMongoDriver,
|
||||
mongoDB: super,
|
||||
}
|
||||
return database
|
||||
}
|
||||
|
||||
var _ GroupDataBaseInterface = (*GroupDataBase)(nil)
|
||||
|
||||
type GroupDataBase struct {
|
||||
groupDB relationTb.GroupModelInterface
|
||||
groupMemberDB relationTb.GroupMemberModelInterface
|
||||
groupRequestDB relationTb.GroupRequestModelInterface
|
||||
db *gorm.DB
|
||||
|
||||
//cache cache.GroupCache
|
||||
cache *cache.GroupCacheRedis
|
||||
mongoDB *unrelation.SuperGroupMongoDriver
|
||||
tx tx.Tx
|
||||
ctxTx tx.CtxTx
|
||||
cache cache.GroupCacheRedisInterface
|
||||
mongoDB unRelationTb.SuperGroupModelInterface
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
||||
@ -329,21 +116,16 @@ func (g *GroupDataBase) FindGroupMemberUserID(ctx context.Context, groupID strin
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.GroupModel, groupMembers []*relationTb.GroupMemberModel) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if len(groups) > 0 {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
if err := g.groupDB.NewTx(tx).Create(ctx, groups); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(groupMembers) > 0 {
|
||||
if err := g.groupMemberDB.Create(ctx, groupMembers, tx); err != nil {
|
||||
if err := g.groupMemberDB.NewTx(tx).Create(ctx, groupMembers); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := g.cache.DelJoinedGroupIDs(ctx, utils.Slice(groupMembers, func(e *relationTb.GroupMemberModel) string {
|
||||
// return e.UserID
|
||||
//})); err != nil {
|
||||
// return err
|
||||
//}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -351,9 +133,6 @@ func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relationTb.Gr
|
||||
|
||||
func (g *GroupDataBase) TakeGroup(ctx context.Context, groupID string) (group *relationTb.GroupModel, err error) {
|
||||
return g.cache.GetGroupInfo(ctx, groupID)
|
||||
//return cache.GetCache(ctx, g.rcClient, g.getGroupInfoKey(groupID), g.expireTime, func(ctx context.Context) (*relationTb.GroupModel, error) {
|
||||
// return g.group.Take(ctx, groupID)
|
||||
//})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) FindGroup(ctx context.Context, groupIDs []string) (groups []*relationTb.GroupModel, err error) {
|
||||
@ -365,8 +144,8 @@ func (g *GroupDataBase) SearchGroup(ctx context.Context, keyword string, pageNum
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data map[string]any) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.UpdateMap(ctx, groupID, data, tx); err != nil {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if err := g.groupDB.NewTx(tx).UpdateMap(ctx, groupID, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupInfo(ctx, groupID); err != nil {
|
||||
@ -377,11 +156,11 @@ func (g *GroupDataBase) UpdateGroup(ctx context.Context, groupID string, data ma
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DismissGroup(ctx context.Context, groupID string) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed, tx); err != nil {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if err := g.groupDB.NewTx(tx).UpdateStatus(ctx, groupID, constant.GroupStatusDismissed); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.groupMemberDB.DeleteGroup(ctx, []string{groupID}, tx); err != nil {
|
||||
if err := g.groupMemberDB.NewTx(tx).DeleteGroup(ctx, []string{groupID}); err != nil {
|
||||
return err
|
||||
}
|
||||
userIDs, err := g.cache.GetGroupMemberIDs(ctx, groupID)
|
||||
@ -416,12 +195,12 @@ func (g *GroupDataBase) SearchGroupMember(ctx context.Context, keyword string, g
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, member *relationTb.GroupMemberModel) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupRequestDB.UpdateHandler(ctx, groupID, userID, handledMsg, handleResult, tx); err != nil {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if err := g.groupRequestDB.NewTx(tx).UpdateHandler(ctx, groupID, userID, handledMsg, handleResult); err != nil {
|
||||
return err
|
||||
}
|
||||
if member != nil {
|
||||
if err := g.groupMemberDB.Create(ctx, []*relationTb.GroupMemberModel{member}, tx); err != nil {
|
||||
if err := g.groupMemberDB.NewTx(tx).Create(ctx, []*relationTb.GroupMemberModel{member}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, []string{userID}); err != nil {
|
||||
@ -433,8 +212,8 @@ func (g *GroupDataBase) HandlerGroupRequest(ctx context.Context, groupID string,
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs, tx); err != nil {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if err := g.groupMemberDB.NewTx(tx).Delete(ctx, groupID, userIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.delGroupMemberCache(ctx, groupID, userIDs); err != nil {
|
||||
@ -453,15 +232,15 @@ func (g *GroupDataBase) MapGroupMemberNum(ctx context.Context, groupIDs []string
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) TransferGroupOwner(ctx context.Context, groupID string, oldOwnerUserID, newOwnerUserID string, roleLevel int32) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
rowsAffected, err := g.groupMemberDB.UpdateRoleLevel(ctx, groupID, oldOwnerUserID, roleLevel, tx)
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
rowsAffected, err := g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, oldOwnerUserID, roleLevel)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if rowsAffected != 1 {
|
||||
return utils.Wrap(fmt.Errorf("oldOwnerUserID %s rowsAffected = %d", oldOwnerUserID, rowsAffected), "")
|
||||
}
|
||||
rowsAffected, err = g.groupMemberDB.UpdateRoleLevel(ctx, groupID, newOwnerUserID, constant.GroupOwner, tx)
|
||||
rowsAffected, err = g.groupMemberDB.NewTx(tx).UpdateRoleLevel(ctx, groupID, newOwnerUserID, constant.GroupOwner)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -476,8 +255,8 @@ func (g *GroupDataBase) TransferGroupOwner(ctx context.Context, groupID string,
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID string, userID string, data map[string]any) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupMemberDB.Update(ctx, groupID, userID, data, tx); err != nil {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
if err := g.groupMemberDB.NewTx(tx).Update(ctx, groupID, userID, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMemberInfo(ctx, groupID, userID); err != nil {
|
||||
@ -488,9 +267,9 @@ func (g *GroupDataBase) UpdateGroupMember(ctx context.Context, groupID string, u
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) UpdateGroupMembers(ctx context.Context, data []*relationTb.BatchUpdateGroupMember) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
return g.tx.Transaction(func(tx any) error {
|
||||
for _, item := range data {
|
||||
if err := g.groupMemberDB.Update(ctx, item.GroupID, item.UserID, item.Map, tx); err != nil {
|
||||
if err := g.groupMemberDB.NewTx(tx).Update(ctx, item.GroupID, item.UserID, item.Map); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.cache.DelGroupMemberInfo(ctx, item.GroupID, item.UserID); err != nil {
|
||||
@ -522,25 +301,25 @@ func (g *GroupDataBase) FindJoinSuperGroup(ctx context.Context, userID string) (
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDList []string) error {
|
||||
return unrelation.MongoTransaction(ctx, g.mongoDB.MgoClient, func(tx mongo.SessionContext) error {
|
||||
return g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDList, tx)
|
||||
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||||
return g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDList)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||
return unrelation.MongoTransaction(ctx, g.mongoDB.MgoClient, func(tx mongo.SessionContext) error {
|
||||
return g.mongoDB.DeleteSuperGroup(ctx, groupID, tx)
|
||||
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||||
return g.mongoDB.DeleteSuperGroup(ctx, groupID)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) DeleteSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return unrelation.MongoTransaction(ctx, g.mongoDB.MgoClient, func(tx mongo.SessionContext) error {
|
||||
return g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs, tx)
|
||||
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||||
return g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs)
|
||||
})
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateSuperGroupMember(ctx context.Context, groupID string, userIDs []string) error {
|
||||
return unrelation.MongoTransaction(ctx, g.mongoDB.MgoClient, func(tx mongo.SessionContext) error {
|
||||
return g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs, tx)
|
||||
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
|
||||
return g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs)
|
||||
})
|
||||
}
|
||||
|
@ -19,49 +19,53 @@ 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) {
|
||||
func (g *GroupMemberGorm) NewTx(tx any) relation.GroupMemberModelInterface {
|
||||
return &GroupMemberGorm{DB: tx.(*gorm.DB)}
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Create(ctx context.Context, groupMemberList []*relation.GroupMemberModel) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupMemberList", groupMemberList)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groupMemberList).Error, "")
|
||||
return utils.Wrap(g.DB.Create(&groupMemberList).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string, tx ...any) (err error) {
|
||||
func (g *GroupMemberGorm) Delete(ctx context.Context, groupID string, userIDs []string) (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, "")
|
||||
return utils.Wrap(g.DB.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) {
|
||||
func (g *GroupMemberGorm) DeleteGroup(ctx context.Context, groupIDs []string) (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, "")
|
||||
return utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Delete(&relation.GroupMemberModel{}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]any, tx ...any) (err error) {
|
||||
func (g *GroupMemberGorm) Update(ctx context.Context, groupID string, userID string, data map[string]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, "")
|
||||
return utils.Wrap(g.DB.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) {
|
||||
func (g *GroupMemberGorm) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32) (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{
|
||||
db := g.DB.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) {
|
||||
func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) (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)
|
||||
db := g.DB
|
||||
if len(groupIDs) > 0 {
|
||||
db = db.Where("group_id in (?)", groupIDs)
|
||||
}
|
||||
@ -74,41 +78,41 @@ func (g *GroupMemberGorm) Find(ctx context.Context, groupIDs []string, userIDs [
|
||||
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) {
|
||||
func (g *GroupMemberGorm) Take(ctx context.Context, groupID string, userID string) (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, "")
|
||||
return groupMember, utils.Wrap(g.DB.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) {
|
||||
func (g *GroupMemberGorm) TakeOwner(ctx context.Context, groupID string) (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, "")
|
||||
return groupMember, utils.Wrap(g.DB.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 uint32, groupList []*relation.GroupMemberModel, err error) {
|
||||
func (g *GroupMemberGorm) SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, 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)
|
||||
db := g.DB
|
||||
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) {
|
||||
func (g *GroupMemberGorm) MapGroupMemberNum(ctx context.Context, groupIDs []string) (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")
|
||||
return mapCount(g.DB.Where("group_id in (?)", groupIDs), "group_id")
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string, tx ...any) (groupUsers map[string][]string, err error) {
|
||||
func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupIDs", groupIDs, "groupUsers", groupUsers)
|
||||
}()
|
||||
@ -116,7 +120,7 @@ func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string,
|
||||
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 {
|
||||
if err := g.DB.Model(&relation.GroupMemberModel{}).Where("group_id in (?)", groupIDs).Find(&items).Error; err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
groupUsers = make(map[string][]string)
|
||||
@ -126,9 +130,9 @@ func (g *GroupMemberGorm) FindJoinUserID(ctx context.Context, groupIDs []string,
|
||||
return groupUsers, nil
|
||||
}
|
||||
|
||||
func (g *GroupMemberGorm) FindMemberUserID(ctx context.Context, groupID string, tx ...any) (userIDs []string, err error) {
|
||||
func (g *GroupMemberGorm) FindMemberUserID(ctx context.Context, groupID string) (userIDs []string, err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groupID", groupID, "userIDs", userIDs)
|
||||
}()
|
||||
return userIDs, utils.Wrap(getDBConn(g.DB, tx).Model(&relation.GroupMemberModel{}).Where("group_id = ?", groupID).Pluck("user_id", &userIDs).Error, "")
|
||||
return userIDs, utils.Wrap(g.DB.Model(&relation.GroupMemberModel{}).Where("group_id = ?", groupID).Pluck("user_id", &userIDs).Error, "")
|
||||
}
|
||||
|
@ -18,54 +18,51 @@ func NewGroupDB(db *gorm.DB) relation.GroupModelInterface {
|
||||
return &GroupGorm{DB: db}
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel, tx ...any) (err error) {
|
||||
func (g *GroupGorm) NewTx(tx any) relation.GroupModelInterface {
|
||||
return &GroupGorm{DB: tx.(*gorm.DB)}
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Create(ctx context.Context, groups []*relation.GroupModel) (err error) {
|
||||
defer func() {
|
||||
tracelog.SetCtxDebug(ctx, utils.GetFuncName(1), err, "groups", groups)
|
||||
}()
|
||||
return utils.Wrap(getDBConn(g.DB, tx).Create(&groups).Error, "")
|
||||
return utils.Wrap(g.DB.Create(&groups).Error, "")
|
||||
}
|
||||
|
||||
//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 (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}, tx ...any) (err error) {
|
||||
func (g *GroupGorm) UpdateMap(ctx context.Context, groupID string, args map[string]interface{}) (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, "")
|
||||
return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(args).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32, tx ...any) (err error) {
|
||||
func (g *GroupGorm) UpdateStatus(ctx context.Context, groupID string, status int32) (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, "")
|
||||
return utils.Wrap(g.DB.Where("group_id = ?", groupID).Model(&relation.GroupModel{}).Updates(map[string]any{"status": status}).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string, tx ...any) (groups []*relation.GroupModel, err error) {
|
||||
func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (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, "")
|
||||
return groups, utils.Wrap(g.DB.Where("group_id in (?)", groupIDs).Find(&groups).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Take(ctx context.Context, groupID string, tx ...any) (group *relation.GroupModel, err error) {
|
||||
func (g *GroupGorm) Take(ctx context.Context, groupID string) (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, "")
|
||||
return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "")
|
||||
}
|
||||
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32, tx ...any) (total uint32, groups []*relation.GroupModel, err error) {
|
||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, 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)
|
||||
return gormSearch[relation.GroupModel](g.DB, []string{"name"}, keyword, pageNumber, showNumber)
|
||||
}
|
||||
|
||||
func (g *GroupGorm) GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error) {
|
||||
|
@ -8,78 +8,50 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var _ relation.GroupRequestModelInterface = (*GroupRequestGorm)(nil)
|
||||
|
||||
type GroupRequestGorm struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
func (g *GroupRequestGorm) NewTx(tx any) relation.GroupRequestModelInterface {
|
||||
return &GroupRequestGorm{
|
||||
DB: tx.(*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) {
|
||||
func (g *GroupRequestGorm) Create(ctx context.Context, groupRequests []*relation.GroupRequestModel) (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())
|
||||
return utils.Wrap(g.DB.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 (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 (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (err error) {
|
||||
func (g *GroupRequestGorm) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32) (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{
|
||||
return utils.Wrap(g.DB.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 (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) {
|
||||
func (g *GroupRequestGorm) Take(ctx context.Context, groupID string, userID string) (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())
|
||||
return groupRequest, utils.Wrap(g.DB.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 uint32, groups []*relation.GroupRequestModel, err error) {
|
||||
func (g *GroupRequestGorm) Page(ctx context.Context, userID string, pageNumber, showNumber int32) (total uint32, 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)
|
||||
return gormSearch[relation.GroupRequestModel](g.DB.Where("user_id = ?", userID), nil, "", pageNumber, showNumber)
|
||||
}
|
||||
|
@ -32,11 +32,12 @@ func (GroupModel) TableName() string {
|
||||
}
|
||||
|
||||
type GroupModelInterface interface {
|
||||
Create(ctx context.Context, groups []*GroupModel, 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 uint32, groups []*GroupModel, err error)
|
||||
NewTx(tx any) GroupModelInterface
|
||||
Create(ctx context.Context, groups []*GroupModel) (err error)
|
||||
UpdateMap(ctx context.Context, groupID string, args map[string]interface{}) (err error)
|
||||
UpdateStatus(ctx context.Context, groupID string, status int32) (err error)
|
||||
Find(ctx context.Context, groupIDs []string) (groups []*GroupModel, err error)
|
||||
Take(ctx context.Context, groupID string) (group *GroupModel, err error)
|
||||
Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*GroupModel, err error)
|
||||
GetGroupIDsByGroupType(ctx context.Context, groupType int) (groupIDs []string, err error)
|
||||
}
|
||||
|
@ -28,16 +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)
|
||||
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)
|
||||
FindMemberUserID(ctx context.Context, groupID string, tx ...any) (userIDs []string, 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 uint32, 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)
|
||||
NewTx(tx any) GroupMemberModelInterface
|
||||
Create(ctx context.Context, groupMemberList []*GroupMemberModel) (err error)
|
||||
Delete(ctx context.Context, groupID string, userIDs []string) (err error)
|
||||
DeleteGroup(ctx context.Context, groupIDs []string) (err error)
|
||||
Update(ctx context.Context, groupID string, userID string, data map[string]any) (err error)
|
||||
UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32) (rowsAffected int64, err error)
|
||||
Find(ctx context.Context, groupIDs []string, userIDs []string, roleLevels []int32) (groupList []*GroupMemberModel, err error)
|
||||
FindMemberUserID(ctx context.Context, groupID string) (userIDs []string, err error)
|
||||
Take(ctx context.Context, groupID string, userID string) (groupMember *GroupMemberModel, err error)
|
||||
TakeOwner(ctx context.Context, groupID string) (groupMember *GroupMemberModel, err error)
|
||||
SearchMember(ctx context.Context, keyword string, groupIDs []string, userIDs []string, roleLevels []int32, pageNumber, showNumber int32) (total uint32, groupList []*GroupMemberModel, err error)
|
||||
MapGroupMemberNum(ctx context.Context, groupIDs []string) (count map[string]uint32, err error)
|
||||
FindJoinUserID(ctx context.Context, groupIDs []string) (groupUsers map[string][]string, err error)
|
||||
}
|
||||
|
@ -28,8 +28,9 @@ func (GroupRequestModel) TableName() string {
|
||||
}
|
||||
|
||||
type GroupRequestModelInterface interface {
|
||||
Create(ctx context.Context, groupRequests []*GroupRequestModel, tx ...any) (err error)
|
||||
UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32, tx ...any) (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 uint32, groups []*GroupRequestModel, err error)
|
||||
NewTx(tx any) GroupRequestModelInterface
|
||||
Create(ctx context.Context, groupRequests []*GroupRequestModel) (err error)
|
||||
UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32) (err error)
|
||||
Take(ctx context.Context, groupID string, userID string) (groupRequest *GroupRequestModel, err error)
|
||||
Page(ctx context.Context, userID string, pageNumber, showNumber int32) (total uint32, groups []*GroupRequestModel, err error)
|
||||
}
|
||||
|
@ -28,11 +28,12 @@ func (UserToSuperGroupModel) TableName() string {
|
||||
}
|
||||
|
||||
type SuperGroupModelInterface interface {
|
||||
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
|
||||
CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error
|
||||
TakeSuperGroup(ctx context.Context, groupID string) (group *SuperGroupModel, err error)
|
||||
FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*SuperGroupModel, err error)
|
||||
AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) error
|
||||
RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) error
|
||||
GetSuperGroupByUserID(ctx context.Context, userID string) (*UserToSuperGroupModel, error)
|
||||
DeleteSuperGroup(ctx context.Context, groupID string) error
|
||||
RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error
|
||||
}
|
||||
|
@ -11,9 +11,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo/readconcern"
|
||||
)
|
||||
|
||||
var _ unrelation.SuperGroupModelInterface = (*SuperGroupMongoDriver)(nil)
|
||||
|
||||
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) *SuperGroupMongoDriver {
|
||||
func NewSuperGroupMongoDriver(mgoClient *mongo.Client) unrelation.SuperGroupModelInterface {
|
||||
mgoDB := mgoClient.Database(config.Config.Mongo.DBDatabase)
|
||||
return &SuperGroupMongoDriver{MgoDB: mgoDB, MgoClient: mgoClient, superGroupCollection: mgoDB.Collection(unrelation.CSuperGroup), userToSuperGroupCollection: mgoDB.Collection(unrelation.CUserToSuperGroup)}
|
||||
}
|
||||
@ -25,109 +23,7 @@ type SuperGroupMongoDriver struct {
|
||||
userToSuperGroupCollection *mongo.Collection
|
||||
}
|
||||
|
||||
// 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(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 = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID string, initMemberIDs []string) error {
|
||||
_, err := s.superGroupCollection.InsertOne(ctx, &unrelation.SuperGroupModel{
|
||||
GroupID: groupID,
|
||||
MemberIDs: initMemberIDs,
|
||||
@ -146,16 +42,14 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string, tx ...any) (group *unrelation.SuperGroupModel, err error) {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) TakeSuperGroup(ctx context.Context, groupID string) (group *unrelation.SuperGroupModel, err error) {
|
||||
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
||||
return nil, utils.Wrap(err, "")
|
||||
}
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string, tx ...any) (groups []*unrelation.SuperGroupModel, err error) {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []string) (groups []*unrelation.SuperGroupModel, err error) {
|
||||
cursor, err := s.superGroupCollection.Find(ctx, bson.M{"group_id": bson.M{
|
||||
"$in": groupIDs,
|
||||
}})
|
||||
@ -170,8 +64,7 @@ func (s *SuperGroupMongoDriver) FindSuperGroup(ctx context.Context, groupIDs []s
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID string, userIDs []string) 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}}})
|
||||
@ -194,8 +87,7 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string, tx ...any) error {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, groupID string, userIDs []string) 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}}})
|
||||
@ -212,16 +104,14 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string, tx ...any) (*unrelation.UserToSuperGroupModel, error) {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(ctx context.Context, userID string) (*unrelation.UserToSuperGroupModel, error) {
|
||||
var user unrelation.UserToSuperGroupModel
|
||||
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||
return &user, utils.Wrap(err, "")
|
||||
}
|
||||
|
||||
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
ctx = getTxCtx(ctx, tx)
|
||||
group, err := s.TakeSuperGroup(ctx, groupID, tx...)
|
||||
func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string) error {
|
||||
group, err := s.TakeSuperGroup(ctx, groupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -231,26 +121,7 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st
|
||||
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
|
||||
}
|
||||
|
||||
//func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID string, tx ...any) error {
|
||||
// ctx = 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 = getTxCtx(ctx, tx)
|
||||
func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID string, userIDs []string) error {
|
||||
_, 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