mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-06-30 06:48:14 +08:00
全局黑名单用户
This commit is contained in:
parent
706c6000cb
commit
d1f83cf2be
@ -27,12 +27,12 @@ func NewUserGlobalBlackApi(blacklistDB controller.UserGlobalBlackDatabase, userD
|
||||
}
|
||||
|
||||
type addGlobalBlacklistReq struct {
|
||||
Nicknames []string `json:"nicknames" binding:"required,min=1"`
|
||||
Reason string `json:"reason"`
|
||||
UserIDs []string `json:"userIDs" binding:"required,min=1"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
type removeGlobalBlacklistReq struct {
|
||||
Nicknames []string `json:"nicknames" binding:"required,min=1"`
|
||||
UserIDs []string `json:"userIDs" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
type getGlobalBlacklistReq struct {
|
||||
@ -64,24 +64,25 @@ func (b *UserGlobalBlackApi) AddGlobalBlacklist(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
operatorID := mcontext.GetOpUserID(c)
|
||||
blacks := make([]*model.UserGlobalBlack, 0, len(req.Nicknames))
|
||||
for _, nickname := range req.Nicknames {
|
||||
users, err := b.userDB.TakeByNickname(c, nickname)
|
||||
if err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
if len(users) == 0 {
|
||||
apiresp.GinError(c, errs.ErrRecordNotFound.WrapMsg("nickname not found", "nickname", nickname))
|
||||
return
|
||||
}
|
||||
if len(users) > 1 {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg("nickname matched multiple users", "nickname", nickname))
|
||||
foundUsers, err := b.userDB.Find(c, req.UserIDs)
|
||||
if err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
userMap := make(map[string]*model.User, len(foundUsers))
|
||||
for _, u := range foundUsers {
|
||||
userMap[u.UserID] = u
|
||||
}
|
||||
blacks := make([]*model.UserGlobalBlack, 0, len(req.UserIDs))
|
||||
for _, userID := range req.UserIDs {
|
||||
u, ok := userMap[userID]
|
||||
if !ok {
|
||||
apiresp.GinError(c, errs.ErrRecordNotFound.WrapMsg("userID not found", "userID", userID))
|
||||
return
|
||||
}
|
||||
blacks = append(blacks, &model.UserGlobalBlack{
|
||||
UserID: users[0].UserID,
|
||||
Nickname: users[0].Nickname,
|
||||
UserID: u.UserID,
|
||||
Nickname: u.Nickname,
|
||||
OperatorID: operatorID,
|
||||
Reason: req.Reason,
|
||||
})
|
||||
@ -119,24 +120,7 @@ func (b *UserGlobalBlackApi) RemoveGlobalBlacklist(c *gin.Context) {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
userIDs := make([]string, 0, len(req.Nicknames))
|
||||
for _, nickname := range req.Nicknames {
|
||||
users, err := b.userDB.TakeByNickname(c, nickname)
|
||||
if err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
if len(users) == 0 {
|
||||
apiresp.GinError(c, errs.ErrRecordNotFound.WrapMsg("nickname not found", "nickname", nickname))
|
||||
return
|
||||
}
|
||||
if len(users) > 1 {
|
||||
apiresp.GinError(c, errs.ErrArgs.WrapMsg("nickname matched multiple users", "nickname", nickname))
|
||||
return
|
||||
}
|
||||
userIDs = append(userIDs, users[0].UserID)
|
||||
}
|
||||
if err := b.blacklistDB.RemoveBlack(c, userIDs); err != nil {
|
||||
if err := b.blacklistDB.RemoveBlack(c, req.UserIDs); err != nil {
|
||||
apiresp.GinError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -12,8 +12,8 @@ import (
|
||||
type UserGlobalBlackDatabase interface {
|
||||
// AddBlack 将用户加入全局黑名单
|
||||
AddBlack(ctx context.Context, blacks []*model.UserGlobalBlack) error
|
||||
// RemoveBlack 按昵称将用户从全局黑名单移除
|
||||
RemoveBlack(ctx context.Context, nicknames []string) error
|
||||
// RemoveBlack 按 userID 将用户从全局黑名单移除
|
||||
RemoveBlack(ctx context.Context, userIDs []string) error
|
||||
// IsBlocked 检查用户是否在全局黑名单
|
||||
IsBlocked(ctx context.Context, userID string) (bool, error)
|
||||
// GetBlackList 分页获取黑名单列表
|
||||
@ -32,8 +32,8 @@ func (u *userGlobalBlackDatabase) AddBlack(ctx context.Context, blacks []*model.
|
||||
return u.db.Add(ctx, blacks)
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) RemoveBlack(ctx context.Context, nicknames []string) error {
|
||||
return u.db.Remove(ctx, nicknames)
|
||||
func (u *userGlobalBlackDatabase) RemoveBlack(ctx context.Context, userIDs []string) error {
|
||||
return u.db.Remove(ctx, userIDs)
|
||||
}
|
||||
|
||||
func (u *userGlobalBlackDatabase) IsBlocked(ctx context.Context, userID string) (bool, error) {
|
||||
|
||||
@ -11,8 +11,8 @@ import (
|
||||
type UserGlobalBlack interface {
|
||||
// Add 批量添加用户到全局黑名单
|
||||
Add(ctx context.Context, blacks []*model.UserGlobalBlack) error
|
||||
// Remove 按昵称从全局黑名单移除用户
|
||||
Remove(ctx context.Context, nicknames []string) error
|
||||
// Remove 按 userID 从全局黑名单移除用户
|
||||
Remove(ctx context.Context, userIDs []string) error
|
||||
// Find 查询指定用户是否在黑名单(返回在黑名单中的记录)
|
||||
Find(ctx context.Context, userIDs []string) ([]*model.UserGlobalBlack, error)
|
||||
// IsBlocked 检查单个用户是否在黑名单
|
||||
|
||||
@ -1,14 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# 统一通过 API 新链路管理全局黑名单(按 nickname)
|
||||
# 统一通过 API 新链路管理全局黑名单(按 userID)
|
||||
#
|
||||
# 用法:
|
||||
# 1) 添加
|
||||
# ./scripts/global_blacklist_api.sh add "alice,bob" [reason]
|
||||
# ./scripts/global_blacklist_api.sh add "user001,user002" [reason]
|
||||
#
|
||||
# 2) 删除
|
||||
# ./scripts/global_blacklist_api.sh remove "alice,bob"
|
||||
# ./scripts/global_blacklist_api.sh remove "user001,user002"
|
||||
#
|
||||
# 3) 查询
|
||||
# ./scripts/global_blacklist_api.sh list [pageNumber] [showNumber]
|
||||
@ -26,7 +26,7 @@ ADMIN_USER_ID="${ADMIN_USER_ID:-imAdmin}"
|
||||
OPERATION_ID="${OPERATION_ID:-gb_$(date +%s)_$RANDOM}"
|
||||
|
||||
ACTION="${1:-}"
|
||||
NICKNAMES_RAW="${2:-}"
|
||||
USERIDS_RAW="${2:-}"
|
||||
REASON="${3:-manual_by_api_script}"
|
||||
PAGE_NUMBER="${2:-1}"
|
||||
SHOW_NUMBER="${3:-20}"
|
||||
@ -43,7 +43,7 @@ trim() {
|
||||
printf '%s' "$s"
|
||||
}
|
||||
|
||||
nicknames_csv_to_json_array() {
|
||||
userids_csv_to_json_array() {
|
||||
local csv="$1"
|
||||
local arr_json="["
|
||||
local first=1
|
||||
@ -63,7 +63,7 @@ nicknames_csv_to_json_array() {
|
||||
arr_json="${arr_json}]"
|
||||
|
||||
if [[ "$arr_json" == "[]" ]]; then
|
||||
die "nicknames 为空,请传入逗号分隔昵称,如 \"alice,bob\""
|
||||
die "userIDs 为空,请传入逗号分隔的 userID,如 \"user001,user002\""
|
||||
fi
|
||||
printf '%s' "$arr_json"
|
||||
}
|
||||
@ -128,8 +128,8 @@ call_api() {
|
||||
if [[ -z "$ACTION" ]]; then
|
||||
cat <<'EOF'
|
||||
用法:
|
||||
添加: ./scripts/global_blacklist_api.sh add "alice,bob" [reason]
|
||||
删除: ./scripts/global_blacklist_api.sh remove "alice,bob"
|
||||
添加: ./scripts/global_blacklist_api.sh add "user001,user002" [reason]
|
||||
删除: ./scripts/global_blacklist_api.sh remove "user001,user002"
|
||||
查询: ./scripts/global_blacklist_api.sh list [pageNumber] [showNumber]
|
||||
EOF
|
||||
exit 1
|
||||
@ -142,17 +142,17 @@ fi
|
||||
|
||||
case "$ACTION" in
|
||||
add)
|
||||
[[ -z "$NICKNAMES_RAW" ]] && die "add 需要 nicknames 参数"
|
||||
NICKNAMES_JSON="$(nicknames_csv_to_json_array "$NICKNAMES_RAW")"
|
||||
BODY="{\"nicknames\":${NICKNAMES_JSON},\"reason\":\"${REASON}\"}"
|
||||
[[ -z "$USERIDS_RAW" ]] && die "add 需要 userIDs 参数"
|
||||
USERIDS_JSON="$(userids_csv_to_json_array "$USERIDS_RAW")"
|
||||
BODY="{\"userIDs\":${USERIDS_JSON},\"reason\":\"${REASON}\"}"
|
||||
echo ">>> POST /user/add_global_blacklist"
|
||||
call_api "/user/add_global_blacklist" "$BODY" "$ADMIN_TOKEN"
|
||||
;;
|
||||
|
||||
remove)
|
||||
[[ -z "$NICKNAMES_RAW" ]] && die "remove 需要 nicknames 参数"
|
||||
NICKNAMES_JSON="$(nicknames_csv_to_json_array "$NICKNAMES_RAW")"
|
||||
BODY="{\"nicknames\":${NICKNAMES_JSON}}"
|
||||
[[ -z "$USERIDS_RAW" ]] && die "remove 需要 userIDs 参数"
|
||||
USERIDS_JSON="$(userids_csv_to_json_array "$USERIDS_RAW")"
|
||||
BODY="{\"userIDs\":${USERIDS_JSON}}"
|
||||
echo ">>> POST /user/remove_global_blacklist"
|
||||
call_api "/user/remove_global_blacklist" "$BODY" "$ADMIN_TOKEN"
|
||||
;;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user