72 lines
2.0 KiB
Python
72 lines
2.0 KiB
Python
import datetime
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass
|
|
class BlueskyStatusSnapshot:
|
|
account_id: str
|
|
connected: bool
|
|
session_valid: bool
|
|
dm_service_reachable: bool
|
|
me_did: str = ""
|
|
me_handle: str = ""
|
|
agent_count: int = 0
|
|
last_dm_poll_at: datetime.datetime | None = None
|
|
last_dm_poll_error: str = ""
|
|
|
|
|
|
class BlueskyStatus:
|
|
def __init__(self, gateway):
|
|
self._gateway = gateway
|
|
|
|
async def probe(self, account_id: str) -> BlueskyStatusSnapshot:
|
|
handle = self._gateway.get_client(account_id)
|
|
if not handle:
|
|
return BlueskyStatusSnapshot(
|
|
account_id=account_id,
|
|
connected=False,
|
|
session_valid=False,
|
|
dm_service_reachable=False,
|
|
)
|
|
|
|
session_valid = False
|
|
me_did = ""
|
|
me_handle = ""
|
|
try:
|
|
me = handle.client.me
|
|
if me:
|
|
session_valid = True
|
|
me_did = getattr(me, "did", "")
|
|
me_handle = getattr(me, "handle", "")
|
|
except Exception:
|
|
pass
|
|
|
|
dm_reachable = False
|
|
try:
|
|
handle.dm_client.chat.bsky.convo.list_convos(params={"limit": 1})
|
|
dm_reachable = True
|
|
except Exception:
|
|
pass
|
|
|
|
return BlueskyStatusSnapshot(
|
|
account_id=account_id,
|
|
connected=True,
|
|
session_valid=session_valid,
|
|
dm_service_reachable=dm_reachable,
|
|
me_did=me_did,
|
|
me_handle=me_handle,
|
|
agent_count=len(self._gateway._active_clients),
|
|
)
|
|
|
|
def build_summary(self, snapshot: BlueskyStatusSnapshot) -> dict:
|
|
return {
|
|
"channel": "bluesky",
|
|
"account_id": snapshot.account_id,
|
|
"connected": snapshot.connected,
|
|
"session_valid": snapshot.session_valid,
|
|
"dm_service_reachable": snapshot.dm_service_reachable,
|
|
"handle": snapshot.me_handle,
|
|
"did": snapshot.me_did,
|
|
"agent_count": snapshot.agent_count,
|
|
}
|