mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-06-24 19:08:21 +08:00
banned user
This commit is contained in:
parent
7e65b21c5e
commit
e9e15b9e5d
@ -51,6 +51,7 @@ type friendServer struct {
|
||||
relation.UnimplementedFriendServer
|
||||
db controller.FriendDatabase
|
||||
blackDatabase controller.BlackDatabase
|
||||
globalBlackDB controller.UserGlobalBlackDatabase
|
||||
notificationSender *FriendNotificationSender
|
||||
RegisterCenter discovery.SvcDiscoveryRegistry
|
||||
config *Config
|
||||
@ -97,6 +98,11 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
|
||||
return err
|
||||
}
|
||||
|
||||
globalBlackMongoDB, err := mgo.NewUserGlobalBlackMongo(mgocli.GetDB())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
userConn, err := client.GetConn(ctx, config.Share.RpcRegisterName.User)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -133,6 +139,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
|
||||
blackMongoDB,
|
||||
redis.NewBlackCacheRedis(rdb, &config.LocalCacheConfig, blackMongoDB, redis.GetRocksCacheOptions()),
|
||||
),
|
||||
globalBlackDB: controller.NewUserGlobalBlackDatabase(globalBlackMongoDB),
|
||||
notificationSender: notificationSender,
|
||||
RegisterCenter: client,
|
||||
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 {
|
||||
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)
|
||||
if err != nil {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.checkUsersNotGlobalBlocked(ctx, req.FriendUserIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
friends, err := s.getFriend(ctx, req.OwnerUserID, req.FriendUserIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -320,6 +333,25 @@ func (s *friendServer) GetDesignatedFriends(ctx context.Context, req *relation.G
|
||||
}, 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) {
|
||||
if len(friendUserIDs) == 0 {
|
||||
return nil, nil
|
||||
|
||||
@ -62,6 +62,7 @@ type userServer struct {
|
||||
webhookClient *webhook.Client
|
||||
groupClient *rpcli.GroupClient
|
||||
relationClient *rpcli.RelationClient
|
||||
globalBlackDB controller.UserGlobalBlackDatabase
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@ -109,6 +110,10 @@ func Start(ctx context.Context, config *Config, client registry.SvcDiscoveryRegi
|
||||
msgClient := rpcli.NewMsgClient(msgConn)
|
||||
userCache := redis.NewUserCacheRedis(rdb, &config.LocalCacheConfig, userDB, redis.GetRocksCacheOptions())
|
||||
database := controller.NewUserDatabase(userDB, userCache, mgocli.GetTx())
|
||||
globalBlackMgo, err := mgo.NewUserGlobalBlackMongo(mgocli.GetDB())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
localcache.InitLocalCache(&config.LocalCacheConfig)
|
||||
u := &userServer{
|
||||
online: redis.NewUserOnline(rdb),
|
||||
@ -121,6 +126,7 @@ func Start(ctx context.Context, config *Config, client registry.SvcDiscoveryRegi
|
||||
|
||||
groupClient: rpcli.NewGroupClient(groupConn),
|
||||
relationClient: rpcli.NewRelationClient(friendConn),
|
||||
globalBlackDB: controller.NewUserGlobalBlackDatabase(globalBlackMgo),
|
||||
}
|
||||
pbuser.RegisterUserServer(server, u)
|
||||
return u.db.InitOnce(context.Background(), users)
|
||||
@ -133,6 +139,16 @@ func (s *userServer) GetDesignateUsers(ctx context.Context, req *pbuser.GetDesig
|
||||
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)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@ -16,6 +16,8 @@ type UserGlobalBlackDatabase interface {
|
||||
RemoveBlack(ctx context.Context, userIDs []string) error
|
||||
// IsBlocked 检查用户是否在全局黑名单
|
||||
IsBlocked(ctx context.Context, userID string) (bool, error)
|
||||
// FindBlocked 批量查询哪些 userID 在全局黑名单中,返回被封禁的记录
|
||||
FindBlocked(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error)
|
||||
// GetBlackList 分页获取黑名单列表
|
||||
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) {
|
||||
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