up
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user