65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
import time
|
|
from typing import Any
|
|
|
|
import httpx
|
|
import psycopg
|
|
|
|
from ..settings import settings
|
|
from ..utils.name_generator import NameGenerator
|
|
from .comms_guest_names import _CommsGuestNameMixin
|
|
from .comms_protocol import _canon_user
|
|
from .comms_room_ops import _CommsRoomOpsMixin
|
|
|
|
|
|
class CommsService(_CommsGuestNameMixin, _CommsRoomOpsMixin):
|
|
"""Maintain Matrix/MAS guest naming and room hygiene.
|
|
|
|
Inputs: Matrix/MAS endpoints, service credentials, and optional database access
|
|
from settings. Outputs: scheduled maintenance actions plus small status dicts
|
|
for scheduler logging.
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
client_factory: type[httpx.Client] = httpx.Client,
|
|
name_generator: NameGenerator | None = None,
|
|
) -> None:
|
|
self._client_factory = client_factory
|
|
self._name_generator = name_generator or NameGenerator()
|
|
|
|
@property
|
|
def _settings(self) -> Any:
|
|
return settings
|
|
|
|
def _pick_guest_name(self, existing: set[str]) -> str | None:
|
|
return self._name_generator.unique(existing)
|
|
|
|
def _client(self) -> httpx.Client:
|
|
return self._client_factory(timeout=settings.comms_timeout_sec)
|
|
|
|
def _admin_token(self, fallback: str) -> str:
|
|
token = getattr(settings, "comms_synapse_admin_token", "")
|
|
return token if token else fallback
|
|
|
|
def _connect_synapse_db(self) -> Any:
|
|
return psycopg.connect(
|
|
host=settings.comms_synapse_db_host,
|
|
port=settings.comms_synapse_db_port,
|
|
dbname=settings.comms_synapse_db_name,
|
|
user=settings.comms_synapse_db_user,
|
|
password=settings.comms_synapse_db_password,
|
|
)
|
|
|
|
def _sleep(self, seconds: float) -> None:
|
|
time.sleep(seconds)
|
|
|
|
def _time(self) -> float:
|
|
return time.time()
|
|
|
|
|
|
comms = CommsService()
|
|
|
|
__all__ = ["CommsService", "_canon_user", "comms", "psycopg", "settings"]
|