Skip to content

Commit

Permalink
Parse homeserver.software into enum
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 11, 2022
1 parent 5aa8d91 commit bc7a01c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mautrix/bridge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..util.async_getter_lock import async_getter_lock
from .bridge import Bridge
from .bridge import Bridge, HomeserverSoftware
from .config import BaseBridgeConfig
from .custom_puppet import (
AutologinError,
Expand All @@ -21,6 +21,7 @@
__all__ = [
"async_getter_lock",
"Bridge",
"HomeserverSoftware",
"BaseBridgeConfig",
"AutologinError",
"CustomPuppetError",
Expand Down
21 changes: 21 additions & 0 deletions mautrix/bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing import Any
from abc import ABC, abstractmethod
from enum import Enum
import sys

from aiohttp import web
Expand All @@ -30,6 +31,20 @@
uvloop = None


class HomeserverSoftware(Enum):
STANDARD = "standard"
ASMUX = "asmux"
HUNGRY = "hungry"

@property
def is_hungry(self) -> bool:
return self == self.HUNGRY

@property
def is_asmux(self) -> bool:
return self == self.ASMUX


class Bridge(Program, ABC):
db: Database
az: AppService
Expand All @@ -43,6 +58,7 @@ class Bridge(Program, ABC):
repo_url: str
markdown_version: str
manhole: br.commands.manhole.ManholeState | None
homeserver_software: HomeserverSoftware

def __init__(
self,
Expand Down Expand Up @@ -104,6 +120,11 @@ def prepare(self) -> None:
"Loaded config overrides from environment: %s", list(self.config.env.keys())
)
super().prepare()
try:
self.homeserver_software = HomeserverSoftware(self.config["homeserver.software"])
except Exception:
self.log.fatal("Invalid value for homeserver.software in config")
sys.exit(11)
self.prepare_db()
self.prepare_appservice()
self.prepare_bridge()
Expand Down
8 changes: 8 additions & 0 deletions mautrix/bridge/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ async def check_versions(self) -> None:
self.minimum_spec_version,
)
sys.exit(18)
if self.bridge.homeserver_software.is_hungry and not self.versions.supports(
"com.beeper.hungry"
):
self.log.fatal(
"The config claims the homeserver is hungryserv, "
"but the /versions response didn't confirm it"
)
sys.exit(18)

async def wait_for_connection(self) -> None:
self.log.info("Ensuring connectivity to homeserver")
Expand Down
2 changes: 1 addition & 1 deletion mautrix/bridge/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async def update_direct_chats(self, dms: dict[UserID, list[RoomID]] | None = Non
self.log.debug("Updating m.direct list on homeserver")
replace = dms is None
dms = dms or await self.get_direct_chats()
if self.bridge.config.get("homeserver.software", "standard") == "asmux":
if self.bridge.homeserver_software.is_asmux:
# This uses a secret endpoint for atomically updating the DM list
await puppet.intent.api.request(
Method.PUT if replace else Method.PATCH,
Expand Down

0 comments on commit bc7a01c

Please sign in to comment.