From e9e15b9e5d64fb3da412b20be4d35af8078a0ad7 Mon Sep 17 00:00:00 2001 From: hawklin2017 <32898629+hawklin2017@users.noreply.github.com> Date: Sat, 11 Apr 2026 21:44:22 +0800 Subject: [PATCH] banned user --- internal/rpc/relation/friend.go | 32 +++++++++++++++++++ internal/rpc/user/user.go | 16 ++++++++++ .../storage/controller/user_global_black.go | 6 ++++ 3 files changed, 54 insertions(+) diff --git a/internal/rpc/relation/friend.go b/internal/rpc/relation/friend.go index b32a8c50d..a0e5c63e9 100644 --- a/internal/rpc/relation/friend.go +++ b/internal/rpc/relation/friend.go @@ -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 diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index f0788215d..777538925 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -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 } diff --git a/pkg/common/storage/controller/user_global_black.go b/pkg/common/storage/controller/user_global_black.go index ba1448237..2a6d114d4 100644 --- a/pkg/common/storage/controller/user_global_black.go +++ b/pkg/common/storage/controller/user_global_black.go @@ -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) +}