fix: pre-3.12 enum check, create _callbacks dict
This commit is contained in:
parent
e63d9434e5
commit
f570579fb4
2 changed files with 12 additions and 1 deletions
|
@ -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)
|
||||
)
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue