49 lines
1.7 KiB
Python
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}
|