From 4537635db3e674307f62632441f3e8bb3a212091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Wed, 14 Feb 2018 11:51:57 +0100 Subject: [PATCH] Add topic event class. --- matrix/api.py | 10 ++++++++++ matrix/events.py | 20 ++++++++++++++++++++ matrix/messages.py | 16 +++++++--------- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/matrix/api.py b/matrix/api.py index 10ee858..b01e2f5 100644 --- a/matrix/api.py +++ b/matrix/api.py @@ -360,6 +360,16 @@ class MatrixTopicMessage(MatrixMessage): data ) + def decode_body(self, server): + object_hook = partial( + MatrixEvents.MatrixTopicEvent.from_dict, + server, + self.room_id, + self.topic, + ) + + return self._decode(server, object_hook) + class MatrixRedactMessage(MatrixMessage): def __init__(self, client, room_id, event_id, reason=None): diff --git a/matrix/events.py b/matrix/events.py index 8b6d4af..cbbb5f7 100644 --- a/matrix/events.py +++ b/matrix/events.py @@ -143,3 +143,23 @@ class MatrixSendEvent(MatrixEvent): False, parsed_dict ) + + +class MatrixTopicEvent(MatrixEvent): + def __init__(self, server, room_id, event_id, topic): + self.room_id = room_id + self.topic = topic + self.event_id = event_id + MatrixEvent.__init__(self, server) + + @classmethod + def from_dict(cls, server, room_id, topic, parsed_dict): + try: + return cls(server, room_id, parsed_dict["event_id"], topic) + except KeyError: + return MatrixErrorEvent.from_dict( + server, + "Error setting topic", + False, + parsed_dict + ) diff --git a/matrix/messages.py b/matrix/messages.py index e1cf632..cdfb469 100644 --- a/matrix/messages.py +++ b/matrix/messages.py @@ -683,6 +683,10 @@ def matrix_handle_message( event = message.event event.execute() + elif message_type is MessageType.TOPIC: + event = message.event + event.execute() + elif message_type is MessageType.SYNC: next_batch = response['next_batch'] @@ -717,8 +721,7 @@ def matrix_handle_message( room.prev_batch = response['end'] # Nothing to do here, we'll handle topic changes and redactions in the sync - elif (message_type == MessageType.TOPIC or - message_type == MessageType.REDACT): + elif message_type == MessageType.REDACT: pass else: @@ -765,14 +768,9 @@ def handle_http_response(server, message): event.execute() elif message.type == MessageType.TOPIC: - response = message.decoded_response - reason = ("." if not response or not response["error"] else - ": {r}.".format(r=response["error"])) + event = message.event + event.execute() - error_message = ("{prefix}Can't set topic{reason}").format( - prefix=W.prefix("network"), - reason=reason) - server_buffer_prnt(server, error_message) else: error_message = ("{prefix}Unhandled 403 error, please inform the " "developers about this: {error}").format(