From f570579fb47f948c74bf44a66febab79b89748be Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 29 Jan 2024 17:44:12 +0100 Subject: [PATCH] fix: pre-3.12 enum check, create _callbacks dict --- src/aioappsrv/app.py | 5 ++++- src/aioappsrv/matrix.py | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/aioappsrv/app.py b/src/aioappsrv/app.py index 60b539d..ae178a5 100644 --- a/src/aioappsrv/app.py +++ b/src/aioappsrv/app.py @@ -30,6 +30,7 @@ class AppService: logger: logging.Logger | None = None, ): self._rx = web.Application() + self._callbacks = {} self.as_token = as_token self.hs_token = hs_token @@ -53,10 +54,12 @@ class AppService: body = await request.json() for doc in body["events"]: event = Event(**doc) - if event.type not in EventType: + if event.type not in EventType.all(): # TODO: python 3.12 makes the .all() unnecessary self.logger.warn("unhandled event of type %s, ignoring", event["type"]) continue self.logger.debug("dispatching event %s for %s", event.type) + if event.type not in self._callbacks: + self._callbacks[event.type] = {} asyncio.get_event_loop().create_task( self._callbacks[event.type][event.room_id](event) ) diff --git a/src/aioappsrv/matrix.py b/src/aioappsrv/matrix.py index 15fbc66..107c9b8 100644 --- a/src/aioappsrv/matrix.py +++ b/src/aioappsrv/matrix.py @@ -12,6 +12,14 @@ class EventType(Enum): MEMBER = "m.room.member" REDACTION = "m.room.redaction" + @staticmethod + def all() -> set[str]: + return { + "m.room.message", + "m.room.member", + "m.room.redaction", + } + @dataclass class Event: content: dict[str, Any]