mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-07-09 05:41:08 +08:00
banned user
This commit is contained in:
parent
7e65b21c5e
commit
e9e15b9e5d
@ -51,6 +51,7 @@ type friendServer struct {
|
|||||||
relation.UnimplementedFriendServer
|
relation.UnimplementedFriendServer
|
||||||
db controller.FriendDatabase
|
db controller.FriendDatabase
|
||||||
blackDatabase controller.BlackDatabase
|
blackDatabase controller.BlackDatabase
|
||||||
|
globalBlackDB controller.UserGlobalBlackDatabase
|
||||||
notificationSender *FriendNotificationSender
|
notificationSender *FriendNotificationSender
|
||||||
RegisterCenter discovery.SvcDiscoveryRegistry
|
RegisterCenter discovery.SvcDiscoveryRegistry
|
||||||
config *Config
|
config *Config
|
||||||
@ -97,6 +98,11 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
globalBlackMongoDB, err := mgo.NewUserGlobalBlackMongo(mgocli.GetDB())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
userConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.User)
|
userConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.User)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -133,6 +139,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
|
|||||||
blackMongoDB,
|
blackMongoDB,
|
||||||
redis.NewBlackCacheRedis(rdb, &config.LocalCacheConfig, blackMongoDB, redis.GetRocksCacheOptions()),
|
redis.NewBlackCacheRedis(rdb, &config.LocalCacheConfig, blackMongoDB, redis.GetRocksCacheOptions()),
|
||||||
),
|
),
|
||||||
|
globalBlackDB: controller.NewUserGlobalBlackDatabase(globalBlackMongoDB),
|
||||||
notificationSender: notificationSender,
|
notificationSender: notificationSender,
|
||||||
RegisterCenter: client,
|
RegisterCenter: client,
|
||||||
config: config,
|
config: config,
|
||||||
@ -296,6 +303,9 @@ func (s *friendServer) GetFriendInfo(ctx context.Context, req *relation.GetFrien
|
|||||||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := s.checkUsersNotGlobalBlocked(ctx, req.FriendUserIDs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
friends, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
|
friends, err := s.db.FindFriendsWithError(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -311,6 +321,9 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *relation.G
|
|||||||
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
if err := authverify.CheckAccessV3(ctx, req.OwnerUserID, s.config.Share.IMAdminUserID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := s.checkUsersNotGlobalBlocked(ctx, req.FriendUserIDs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
friends, err := s.getFriend(ctx, req.OwnerUserID, req.FriendUserIDs)
|
friends, err := s.getFriend(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -320,6 +333,25 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *relation.G
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkUsersNotGlobalBlocked returns ErrUserBlocked if any of the given userIDs are in the global blacklist.
|
||||||
|
func (s *friendServer) checkUsersNotGlobalBlocked(ctx context.Context, userIDs []string) error {
|
||||||
|
if len(userIDs) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
blocked, err := s.globalBlackDB.FindBlocked(ctx, userIDs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(blocked) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
bannedIDs := make([]string, 0, len(blocked))
|
||||||
|
for _, b := range blocked {
|
||||||
|
bannedIDs = append(bannedIDs, b.UserID)
|
||||||
|
}
|
||||||
|
return servererrs.ErrUserBlocked.WrapMsg("user is banned", "userIDs", bannedIDs)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *friendServer) getFriend(ctx context.Context, ownerUserID string, friendUserIDs []string) ([]*sdkws.FriendInfo, error) {
|
func (s *friendServer) getFriend(ctx context.Context, ownerUserID string, friendUserIDs []string) ([]*sdkws.FriendInfo, error) {
|
||||||
if len(friendUserIDs) == 0 {
|
if len(friendUserIDs) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
@ -62,6 +62,7 @@ type userServer struct {
|
|||||||
webhookClient *webhook.Client
|
webhookClient *webhook.Client
|
||||||
groupClient *rpcli.GroupClient
|
groupClient *rpcli.GroupClient
|
||||||
relationClient *rpcli.RelationClient
|
relationClient *rpcli.RelationClient
|
||||||
|
globalBlackDB controller.UserGlobalBlackDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
@ -109,6 +110,10 @@ func Start(ctx context.Context, config *Config, client registry.SvcDiscoveryRegi
|
|||||||
msgClient := rpcli.NewMsgClient(msgConn)
|
msgClient := rpcli.NewMsgClient(msgConn)
|
||||||
userCache := redis.NewUserCacheRedis(rdb, &config.LocalCacheConfig, userDB, redis.GetRocksCacheOptions())
|
userCache := redis.NewUserCacheRedis(rdb, &config.LocalCacheConfig, userDB, redis.GetRocksCacheOptions())
|
||||||
database := controller.NewUserDatabase(userDB, userCache, mgocli.GetTx())
|
database := controller.NewUserDatabase(userDB, userCache, mgocli.GetTx())
|
||||||
|
globalBlackMgo, err := mgo.NewUserGlobalBlackMongo(mgocli.GetDB())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
localcache.InitLocalCache(&config.LocalCacheConfig)
|
localcache.InitLocalCache(&config.LocalCacheConfig)
|
||||||
u := &userServer{
|
u := &userServer{
|
||||||
online: redis.NewUserOnline(rdb),
|
online: redis.NewUserOnline(rdb),
|
||||||
@ -121,6 +126,7 @@ func Start(ctx context.Context, config *Config, client registry.SvcDiscoveryRegi
|
|||||||
|
|
||||||
groupClient: rpcli.NewGroupClient(groupConn),
|
groupClient: rpcli.NewGroupClient(groupConn),
|
||||||
relationClient: rpcli.NewRelationClient(friendConn),
|
relationClient: rpcli.NewRelationClient(friendConn),
|
||||||
|
globalBlackDB: controller.NewUserGlobalBlackDatabase(globalBlackMgo),
|
||||||
}
|
}
|
||||||
pbuser.RegisterUserServer(server, u)
|
pbuser.RegisterUserServer(server, u)
|
||||||
return u.db.InitOnce(context.Background(), users)
|
return u.db.InitOnce(context.Background(), users)
|
||||||
@ -133,6 +139,16 @@ func (s *userServer) GetDesignateUsers(ctx context.Context, req *pbuser.GetDesig
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if blocked, err := s.globalBlackDB.FindBlocked(ctx, req.UserIDs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else if len(blocked) > 0 {
|
||||||
|
bannedIDs := make([]string, 0, len(blocked))
|
||||||
|
for _, b := range blocked {
|
||||||
|
bannedIDs = append(bannedIDs, b.UserID)
|
||||||
|
}
|
||||||
|
return nil, servererrs.ErrUserBlocked.WrapMsg("user is banned", "userIDs", bannedIDs)
|
||||||
|
}
|
||||||
|
|
||||||
resp.UsersInfo = convert.UsersDB2Pb(users)
|
resp.UsersInfo = convert.UsersDB2Pb(users)
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,8 @@ type UserGlobalBlackDatabase interface {
|
|||||||
RemoveBlack(ctx context.Context, userIDs []string) error
|
RemoveBlack(ctx context.Context, userIDs []string) error
|
||||||
// IsBlocked 检查用户是否在全局黑名单
|
// IsBlocked 检查用户是否在全局黑名单
|
||||||
IsBlocked(ctx context.Context, userID string) (bool, error)
|
IsBlocked(ctx context.Context, userID string) (bool, error)
|
||||||
|
// FindBlocked 批量查询哪些 userID 在全局黑名单中,返回被封禁的记录
|
||||||
|
FindBlocked(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error)
|
||||||
// GetBlackList 分页获取黑名单列表
|
// GetBlackList 分页获取黑名单列表
|
||||||
GetBlackList(ctx context.Context, pagination pagination.Pagination) (count int64, blacks []*model.UserGlobalBlack, err error)
|
GetBlackList(ctx context.Context, pagination pagination.Pagination) (count int64, blacks []*model.UserGlobalBlack, err error)
|
||||||
}
|
}
|
||||||
@ -43,3 +45,7 @@ func (u *userGlobalBlackDatabase) IsBlocked(ctx context.Context, userID string)
|
|||||||
func (u *userGlobalBlackDatabase) GetBlackList(ctx context.Context, pagination pagination.Pagination) (int64, []*model.UserGlobalBlack, error) {
|
func (u *userGlobalBlackDatabase) GetBlackList(ctx context.Context, pagination pagination.Pagination) (int64, []*model.UserGlobalBlack, error) {
|
||||||
return u.db.Page(ctx, pagination)
|
return u.db.Page(ctx, pagination)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *userGlobalBlackDatabase) FindBlocked(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error) {
|
||||||
|
return u.db.Find(ctx, userIDs)
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user