49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.rocketchat.client import RocketChatClient
|
||
|
|
from yuxi.channel.extensions.rocketchat.errors import RocketChatError
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class RocketChatStatusAdapter:
|
||
|
|
def __init__(self, client: RocketChatClient, account_id: str = ""):
|
||
|
|
self.client = client
|
||
|
|
self.account_id = account_id
|
||
|
|
|
||
|
|
async def probe(self, account: dict | None = None) -> bool:
|
||
|
|
try:
|
||
|
|
await self.client.fetch_me()
|
||
|
|
return True
|
||
|
|
except RocketChatError as e:
|
||
|
|
logger.warning("Rocket.Chat probe failed: %s", e)
|
||
|
|
return False
|
||
|
|
|
||
|
|
async def get_me(self) -> dict | None:
|
||
|
|
try:
|
||
|
|
return await self.client.fetch_me()
|
||
|
|
except RocketChatError:
|
||
|
|
return None
|
||
|
|
|
||
|
|
async def build_summary(self, snapshot: object | None = None) -> dict:
|
||
|
|
me = await self.get_me()
|
||
|
|
if not me:
|
||
|
|
return {
|
||
|
|
"status": "disconnected",
|
||
|
|
"account_id": self.account_id,
|
||
|
|
"server_url": self.client.server_url,
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
"status": "connected",
|
||
|
|
"account_id": self.account_id,
|
||
|
|
"server_url": self.client.server_url,
|
||
|
|
"bot_user_id": me.get("_id", ""),
|
||
|
|
"bot_username": me.get("username", ""),
|
||
|
|
"bot_name": me.get("name", ""),
|
||
|
|
}
|
||
|
|
|
||
|
|
async def check_readiness(self, account_id: str) -> bool:
|
||
|
|
return await self.probe()
|