本次提交对 Mattermost 适配器进行了全面升级: 1. 重构为多账号架构,支持同时管理多个 Mattermost 机器人账号 2. 更新安全策略默认配置为配对模式和白名单模式 3. 新增 WebSocket 心跳、重连配置项与连接监控 4. 扩展 Agent 工具支持 pin/unpin、获取反应、搜索消息等操作 5. 重构交互按钮构建逻辑,新增分页与提供商筛选功能 6. 优化 SSRF 防护代码,复用公共工具库实现 7. 新增配置兼容性迁移与可变白名单项检测 8. 完善错误处理与日志输出,添加重复消息去重统计 9. 新增发送临时消息(ephemeral)支持 10. 修复提及检测逻辑,正确处理用户名大小写
60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
|
|
from yuxi.channels.models import Attachment, MessageType
|
|
|
|
_MENTION_RE = re.compile(r"@(\w[\w.-]*)")
|
|
_URL_RE = re.compile(r"https?://[^\s<>\"')\]}]+")
|
|
|
|
|
|
def _parse_json_field(data: dict, field: str) -> dict:
|
|
value = data.get(field, "")
|
|
if isinstance(value, str):
|
|
try:
|
|
return json.loads(value)
|
|
except (json.JSONDecodeError, TypeError):
|
|
return {}
|
|
return value if isinstance(value, dict) else {}
|
|
|
|
|
|
def parse_post_json(data: dict) -> dict:
|
|
return _parse_json_field(data, "post")
|
|
|
|
|
|
def parse_channel_json(data: dict) -> dict:
|
|
return _parse_json_field(data, "channel")
|
|
|
|
|
|
def extract_mentions(text: str) -> list[str]:
|
|
if not text:
|
|
return []
|
|
return _MENTION_RE.findall(text)
|
|
|
|
|
|
def check_bot_mentioned(text: str, bot_username: str) -> bool:
|
|
if not text or not bot_username:
|
|
return False
|
|
return f"@{bot_username.casefold()}" in text.casefold()
|
|
|
|
|
|
def extract_urls(text: str) -> list[str]:
|
|
if not text:
|
|
return []
|
|
return _URL_RE.findall(text)
|
|
|
|
|
|
def detect_message_type(post_data: dict) -> MessageType:
|
|
file_ids = post_data.get("file_ids", [])
|
|
if file_ids:
|
|
return MessageType.FILE
|
|
return MessageType.TEXT
|
|
|
|
|
|
def extract_attachments(post_data: dict) -> list[Attachment]:
|
|
file_ids = post_data.get("file_ids", [])
|
|
if not file_ids:
|
|
return []
|
|
return [Attachment(type="file", file_id=fid) for fid in file_ids]
|