fix: address bot identity review regressions

This commit is contained in:
晴猫
2026-03-15 07:51:31 +09:00
parent c8dc9ddb60
commit 4f8ab0abb1
8 changed files with 77 additions and 17 deletions

View File

@@ -7,9 +7,9 @@ import traceback
from sqlalchemy import and_, func, not_, or_
from sqlmodel import col, select
from src.chat.message_receive.message import SessionMessage
from src.common.database.database import get_db_session
from src.common.database.database_model import Messages
from src.chat.message_receive.message import SessionMessage
from src.common.logger import get_logger
logger = get_logger(__name__)
@@ -162,17 +162,26 @@ def find_messages(
after_time=after_time,
)
if filter_bot:
from src.chat.utils.utils import get_all_bot_accounts
from src.chat.utils.utils import _get_configured_qq_account, get_all_bot_accounts
bot_accounts = get_all_bot_accounts()
exclusion_conditions: list[Any] = []
if bot_accounts:
bot_identity_predicate = or_(
*[
and_(Messages.platform == platform_name, Messages.user_id == account)
for platform_name, account in bot_accounts.items()
]
exclusion_conditions.append(
or_(
*[
and_(Messages.platform == platform_name, Messages.user_id == account)
for platform_name, account in bot_accounts.items()
]
)
)
conditions.append(not_(bot_identity_predicate))
# 兼容旧数据:历史机器人消息在所有平台上都使用 QQ 账号进行存储。
if qq_fallback := _get_configured_qq_account():
exclusion_conditions.append(Messages.user_id == qq_fallback)
if exclusion_conditions:
conditions.append(not_(or_(*exclusion_conditions)))
if filter_command:
conditions.append(Messages.is_command == False) # noqa: E712

View File

@@ -1,4 +1,5 @@
# TODO: 这个兼容包装层后续可以删除,统一直接使用 src.chat.utils.utils.is_bot_self
# TODO: 这个包装层后续可以删除,统一直接使用 src.chat.utils.utils.is_bot_self
# 注意:参数顺序已从旧版 (user_id, platform) 变更为 (platform, user_id),与统一接口一致
def is_bot_self(platform: str, user_id: str) -> bool:
"""
判断用户 ID 是否是机器人自己。