ForcePilot/backend/package/yuxi/channel/extensions/matrix/status.py

68 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from .config import _apply_env_overrides, _dict_to_account
from .types import MatrixProbe
from .utils import get_nio
logger = logging.getLogger(__name__)
async def probe(config: dict, account_id: str = "default") -> MatrixProbe:
nio = get_nio()
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
if not account.homeserver or not account.access_token:
return MatrixProbe(
ok=False,
homeserver=account.homeserver,
error="homeserver or access_token not configured",
)
client = nio.AsyncClient(
homeserver=account.homeserver,
user=account.user_id,
)
client.access_token = account.access_token
try:
resp = await client.whoami()
return MatrixProbe(
ok=True,
user_id=resp.user_id,
device_id=resp.device_id or account.device_id,
homeserver=account.homeserver,
)
except Exception as e:
logger.warning("Matrix probe failed: %s", e)
return MatrixProbe(
ok=False,
homeserver=account.homeserver,
error=str(e),
)
finally:
await client.close()
def build_summary(config: dict, account_id: str = "default") -> dict:
aid = account_id or "default"
account_data = config.get("accounts", {}).get(aid, {})
account = _dict_to_account(account_data)
account = _apply_env_overrides(account)
return {
"channel": "matrix",
"account_id": account.account_id,
"homeserver": account.homeserver,
"user_id": account.user_id,
"device_name": account.device_name,
"encryption": account.encryption,
"dm_policy": account.dm_policy,
"group_policy": account.group_policy,
"streaming": account.streaming,
}