ForcePilot/backend/package/yuxi/channel/extensions/tencent_im/security.py

33 lines
988 B
Python
Raw Normal View History

import logging
from yuxi.channel.extensions.tencent_im.types import TencentIMAccount
logger = logging.getLogger(__name__)
class TencentIMSecurity:
VALID_POLICIES = {"open", "pairing", "allowlist", "disabled"}
def __init__(self, account: TencentIMAccount | None = None):
if account and account.dm_policy in self.VALID_POLICIES:
self._policy = account.dm_policy
else:
self._policy = "open"
self._allowlist: set[str] = set(account.allow_from) if account else set()
def resolve_dm_policy(self) -> str:
return self._policy
def check_allowlist(self, user_id: str) -> bool:
if self._policy == "open":
return True
if self._policy == "disabled":
return False
return user_id in self._allowlist
def add_to_allowlist(self, user_id: str):
self._allowlist.add(user_id)
def remove_from_allowlist(self, user_id: str):
self._allowlist.discard(user_id)