This commit is contained in:
root
2026-10-16 13:51:00 +08:00
parent 6bf949949c
commit 8fc899d0a9
3830 changed files with 11228 additions and 188377 deletions
+8 -1
View File
@@ -7,10 +7,14 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.common import SensitiveWord
from app.services import config_service
CACHE_KEY = "words:sensitive"
CACHE_TTL = 60
# 敏感词过滤总开关(system_configs 可配,后台系统配置页可热改;"0" 关闭)
FILTER_ENABLED_KEY = "sensitive.filter.enabled"
class DFAMatcher:
"""确定性有穷自动机敏感词匹配器"""
@@ -69,9 +73,12 @@ async def get_matcher(db: AsyncSession, redis: aioredis.Redis) -> DFAMatcher:
async def find_sensitive(db: AsyncSession, redis: aioredis.Redis, text: str) -> str | None:
"""检查文本,返回命中的敏感词或 None"""
"""检查文本,返回命中的敏感词或 None;后台关闭过滤开关时直接放行"""
if not text:
return None
enabled = await config_service.get_config(db, redis, FILTER_ENABLED_KEY, "1")
if str(enabled) == "0":
return None
matcher = await get_matcher(db, redis)
return matcher.find(text)