fix: pre-3.12 enum check, create _callbacks dict

This commit is contained in:
əlemi 2024-01-29 17:44:12 +01:00
parent e63d9434e5
commit f570579fb4
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 12 additions and 1 deletions

View file

@ -30,6 +30,7 @@ class AppService:
logger: logging.Logger | None = None, logger: logging.Logger | None = None,
): ):
self._rx = web.Application() self._rx = web.Application()
self._callbacks = {}
self.as_token = as_token self.as_token = as_token
self.hs_token = hs_token self.hs_token = hs_token
@ -53,10 +54,12 @@ class AppService:
body = await request.json() body = await request.json()
for doc in body["events"]: for doc in body["events"]:
event = Event(**doc) 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"]) self.logger.warn("unhandled event of type %s, ignoring", event["type"])
continue continue
self.logger.debug("dispatching event %s for %s", event.type) 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( asyncio.get_event_loop().create_task(
self._callbacks[event.type][event.room_id](event) self._callbacks[event.type][event.room_id](event)
) )

View file

@ -12,6 +12,14 @@ class EventType(Enum):
MEMBER = "m.room.member" MEMBER = "m.room.member"
REDACTION = "m.room.redaction" REDACTION = "m.room.redaction"
@staticmethod
def all() -> set[str]:
return {
"m.room.message",
"m.room.member",
"m.room.redaction",
}
@dataclass @dataclass
class Event: class Event:
content: dict[str, Any] content: dict[str, Any]