From f1e87fe9b0a5c42c44112eec9a88eecc0382652f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 7 Aug 2018 19:07:54 +0200 Subject: [PATCH] server: Add topic setting. --- matrix/commands.py | 3 ++- matrix/server.py | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/matrix/commands.py b/matrix/commands.py index 7fc1ec8..a601602 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -204,8 +204,9 @@ def matrix_topic_command_cb(data, buffer, args): return W.WEECHAT_RC_OK topic = "" if parsed_args.delete else " ".join(parsed_args.topic) + content = {"topic": topic} + server.room_send_state(room, content, "m.room.topic") - # TODO set the new topic return W.WEECHAT_RC_OK diff --git a/matrix/server.py b/matrix/server.py index 95e4299..323e3c7 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -29,6 +29,7 @@ from nio import ( LoginResponse, SyncRepsponse, RoomSendResponse, + RoomPutStateResponse, TransportResponse, TransportType, LocalProtocolError @@ -476,6 +477,14 @@ class MatrixServer(object): W.prnt(self.server_buffer, msg) + def room_send_state(self, room_buffer, body, event_type): + _, request = self.client.room_put_state( + room_buffer.room.room_id, + event_type, + body + ) + self.send_or_queue(request) + def room_send_message(self, room_buffer, formatted, msgtype="m.text"): # type: (RoomBuffer, Formatted) -> None if msgtype == "m.emote": @@ -584,6 +593,15 @@ class MatrixServer(object): self.next_batch = response.next_batch self.schedule_sync() + def handle_transport_response(self, response): + self.error(("Error with response of type type: {}, " + "error code {}").format( + response.request_info.type, response.status_code)) + + # TODO better error handling. + if response.request_info.type == "sync": + self.disconnect() + def handle_response(self, response): # type: (MatrixMessage) -> None self.lag = response.elapsed * 1000 @@ -598,17 +616,20 @@ class MatrixServer(object): W.bar_item_update("lag") if isinstance(response, TransportResponse): - self.error("Error in response, code: {}".format( - response.status_code)) - self.disconnect() + self.handle_transport_response(response) elif isinstance(response, LoginResponse): self._handle_login(response) + elif isinstance(response, SyncRepsponse): self._handle_sync(response) + elif isinstance(response, RoomSendResponse): self.handle_own_messages(response) + elif isinstance(response, RoomPutStateResponse): + pass + return def create_room_buffer(self, room_id):