From 34bf4ad65ea5cdcfeb485537bc4c2094dc7c6a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Wed, 14 Mar 2018 14:21:30 +0100 Subject: [PATCH] rooms: Refactor out the event parsing in RoomInfo. --- matrix/rooms.py | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/matrix/rooms.py b/matrix/rooms.py index b7fc4dc..5964e1b 100644 --- a/matrix/rooms.py +++ b/matrix/rooms.py @@ -242,6 +242,32 @@ class RoomInfo(): return None, None + @staticmethod + def parse_event(event_dict): + # type: (Dict[Any, Any]) -> (RoomEvent, RoomEvent) + message_event = None + state_event = None + + if event_dict["type"] == "m.room.message": + message_event = RoomInfo._message_from_event(event_dict) + elif event_dict["type"] == "m.room.member": + state_event, message_event = ( + RoomInfo._membership_from_dict(event_dict)) + elif event_dict["type"] == "m.room.power_levels": + message_event = RoomPowerLevels.from_dict(event_dict) + elif event_dict["type"] == "m.room.topic": + message_event = RoomTopicEvent.from_dict(event_dict) + elif event_dict["type"] == "m.room.redaction": + message_event = RoomRedactionEvent.from_dict(event_dict) + elif event_dict["type"] == "m.room.name": + message_event = RoomNameEvent.from_dict(event_dict) + elif event_dict["type"] == "m.room.canonical_alias": + message_event = RoomAliasEvent.from_dict(event_dict) + elif event_dict["type"] == "m.room.encryption": + message_event = RoomEncryptionEvent.from_dict(event_dict) + + return message_event, state_event + @staticmethod def _parse_events(parsed_dict): membership_events = [] @@ -249,25 +275,9 @@ class RoomInfo(): try: for event in parsed_dict: - if event["type"] == "m.room.message": - other_events.append(RoomInfo._message_from_event(event)) - elif event["type"] == "m.room.member": - m_event, msg = RoomInfo._membership_from_dict(event) - membership_events.append(m_event) - if msg: - other_events.append(msg) - elif event["type"] == "m.room.power_levels": - other_events.append(RoomPowerLevels.from_dict(event)) - elif event["type"] == "m.room.topic": - other_events.append(RoomTopicEvent.from_dict(event)) - elif event["type"] == "m.room.redaction": - other_events.append(RoomRedactionEvent.from_dict(event)) - elif event["type"] == "m.room.name": - other_events.append(RoomNameEvent.from_dict(event)) - elif event["type"] == "m.room.canonical_alias": - other_events.append(RoomAliasEvent.from_dict(event)) - elif event["type"] == "m.room.encryption": - other_events.append(RoomEncryptionEvent.from_dict(event)) + m_event, s_event = RoomInfo.parse_event(event) + membership_events.append(m_event) + other_events.append(s_event) except (ValueError, TypeError, KeyError) as error: message = ("{prefix}matrix: Error parsing " "room event of type {type}: {error}").format(