from __future__ import annotations from typing import Any class MatrixAccount: def __init__(self, account_id: str, config: dict[str, Any]): self.account_id = account_id self.config = config self._selected = False @property def user_id(self) -> str: return self.config.get("user_id", "") @property def homeserver(self) -> str: return self.config.get("homeserver", "https://matrix.org") @property def is_default(self) -> bool: return self.config.get("isDefault", False) @property def enabled(self) -> bool: return self.config.get("enabled", True) def to_dict(self) -> dict[str, Any]: return { "account_id": self.account_id, "user_id": self.user_id, "homeserver": self.homeserver, "enabled": self.enabled, "is_default": self.is_default, } def __repr__(self) -> str: return f"MatrixAccount({self.account_id}: {self.user_id} @ {self.homeserver})" class MultiAccountManager: def __init__(self, config: dict[str, Any]): self._config = config self._accounts: dict[str, MatrixAccount] = {} self._default_account_id: str | None = None self._load_accounts() def _load_accounts(self) -> None: accounts_raw = self._config.get("accounts", {}) for account_id, account_config in accounts_raw.items(): if isinstance(account_config, dict): account = MatrixAccount(account_id, account_config) self._accounts[account_id] = account if account.is_default: self._default_account_id = account_id if not self._default_account_id and self._accounts: self._default_account_id = next(iter(self._accounts)) default_account = self._config.get("defaultAccount") if default_account and default_account in self._accounts: self._default_account_id = default_account def get(self, account_id: str | None = None) -> MatrixAccount | None: if account_id: return self._accounts.get(account_id) return self.default_account @property def default_account(self) -> MatrixAccount | None: if self._default_account_id: return self._accounts.get(self._default_account_id) return None @property def default_account_id(self) -> str | None: return self._default_account_id def select_account(self, account_id: str) -> bool: if account_id in self._accounts: self._default_account_id = account_id return True return False def list_accounts(self) -> list[MatrixAccount]: return list(self._accounts.values()) def resolve_room_account(self, room_id: str) -> str | None: rooms_config = self._config.get("rooms", {}) room_cfg = rooms_config.get(room_id, {}) return room_cfg.get("account") def promote_to_default(self, account_id: str) -> bool: if account_id in self._accounts: self._default_account_id = account_id self._accounts[account_id]._selected = True for aid, acc in self._accounts.items(): acc.config["isDefault"] = aid == account_id return True return False @property def account_count(self) -> int: return len(self._accounts) @property def has_multi_account(self) -> bool: return len(self._accounts) > 1