ForcePilot/backend/package/yuxi/channels/adapters/matrix/group_mentions.py
Kris 2063145ce1 feat(matrix): 新增完整的Matrix适配器工具集
新增了20+个Matrix相关工具模块,包含线程处理、自动加入、消息去重、配置管理、安全策略、媒体处理、命令解析等功能,完善Matrix适配器基础能力
2026-05-12 00:45:54 +08:00

49 lines
1.7 KiB
Python

from __future__ import annotations
from typing import Any
from .rooms_config import get_room_config
class GroupMentionStrategy:
def __init__(self, config: dict[str, Any]):
self._config = config
def should_require_mention(self, room_id: str) -> bool:
room_cfg = get_room_config(room_id, self._config)
return room_cfg.get("requireMention", self._config.get("requireMention", True))
def is_at_mentioned(self, user_id: str, room_id: str, body: str, formatted_body: str) -> bool:
return user_id in formatted_body or user_id in body
def evaluate_room_policy(self, room_id: str, sender_id: str, body: str, formatted_body: str) -> bool:
if not self.should_require_mention(room_id):
return True
if self.is_at_mentioned(self._config.get("user_id", ""), room_id, body, formatted_body):
return True
return False
def get_room_mention_policy(self, room_id: str) -> dict[str, Any]:
room_cfg = get_room_config(room_id, self._config)
return {
"require_mention": room_cfg.get("requireMention", self._config.get("requireMention", True)),
"allowed_roles": room_cfg.get("allowedRoles", []),
"mention_exempt_users": room_cfg.get("mentionExemptUsers", []),
}
def build_room_mention_config(
room_id: str,
require_mention: bool = True,
allowed_roles: list[str] | None = None,
mention_exempt_users: list[str] | None = None,
) -> dict[str, Any]:
result: dict[str, Any] = {"requireMention": require_mention}
if allowed_roles:
result["allowedRoles"] = allowed_roles
if mention_exempt_users:
result["mentionExemptUsers"] = mention_exempt_users
return {room_id: result}