diff --git a/matrix/api.py b/matrix/api.py index a77e78b..14dc2db 100644 --- a/matrix/api.py +++ b/matrix/api.py @@ -165,11 +165,46 @@ class MatrixClient: path = ("{api}/rooms/{room}/messages?{query_parameters}").format( api=MATRIX_API_PATH, - room=room_id, + room=quote(room_id), query_parameters=urlencode(query_parameters)) return HttpRequest(RequestType.GET, self.host, path) + def room_join(self, room_id): + query_parameters = {"access_token": self.access_token} + + path = ("{api}/rooms/{room_id}/join?" + "{query_parameters}").format( + api=MATRIX_API_PATH, + room_id=quote(room_id), + query_parameters=urlencode(query_parameters)) + + return HttpRequest(RequestType.POST, self.host, path) + + def room_leave(self, room_id): + query_parameters = {"access_token": self.access_token} + + path = ("{api}/rooms/{room_id}/leave?" + "{query_parameters}").format( + api=MATRIX_API_PATH, + room_id=quote(room_id), + query_parameters=urlencode(query_parameters)) + + return HttpRequest(RequestType.POST, self.host, path) + + def room_invite(self, room_id, user_id): + query_parameters = {"access_token": self.access_token} + + content = {"user_id": user_id} + + path = ("{api}/rooms/{room_id}/invite?" + "{query_parameters}").format( + api=MATRIX_API_PATH, + room_id=quote(room_id), + query_parameters=urlencode(query_parameters)) + + return HttpRequest(RequestType.POST, self.host, path, content) + class MatrixMessage: def __init__( @@ -193,8 +228,6 @@ class MatrixMessage: self.send_time = None # type: float self.receive_time = None # type: float - host = ':'.join([server.address, str(server.port)]) - if message_type == MessageType.LOGIN: self.request = server.client.login( server.user, @@ -231,46 +264,13 @@ class MatrixMessage: ) elif message_type == MessageType.JOIN: - path = ("{api}/rooms/{room_id}/join?" - "access_token={access_token}").format( - api=MATRIX_API_PATH, - room_id=room_id, - access_token=server.access_token) - - self.request = HttpRequest( - RequestType.POST, - host, - path, - data - ) + self.request = server.client.room_join(room_id) elif message_type == MessageType.PART: - path = ("{api}/rooms/{room_id}/leave?" - "access_token={access_token}").format( - api=MATRIX_API_PATH, - room_id=room_id, - access_token=server.access_token) - - self.request = HttpRequest( - RequestType.POST, - host, - path, - data - ) + self.request = server.client.room_leave(room_id) elif message_type == MessageType.INVITE: - path = ("{api}/rooms/{room}/invite?" - "access_token={access_token}").format( - api=MATRIX_API_PATH, - room=room_id, - access_token=server.access_token) - - self.request = HttpRequest( - RequestType.POST, - host, - path, - data - ) + self.request = server.client.room_invite(room_id, data) class MatrixUser: diff --git a/matrix/commands.py b/matrix/commands.py index c4777a5..9bf6bfb 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -256,14 +256,12 @@ def matrix_command_invite_cb(data, buffer, command): _, invitee = split_args room_id = key_from_value(server.buffers, buf) - body = {"user_id": invitee} - message = MatrixMessage( server, OPTIONS, MessageType.INVITE, room_id=room_id, - data=body + data=invitee ) server.send_or_queue(message) @@ -319,8 +317,6 @@ def event_id_from_line(buf, target_number): def matrix_redact_command_cb(data, buffer, args): for server in SERVERS.values(): if buffer in server.buffers.values(): - body = {} - room_id = key_from_value(server.buffers, buffer) matches = re.match(r"(\d+)(:\".*\")? ?(.*)?", args) diff --git a/matrix/messages.py b/matrix/messages.py index 28f2f3b..37e73bb 100644 --- a/matrix/messages.py +++ b/matrix/messages.py @@ -31,7 +31,6 @@ from matrix.globals import W, OPTIONS from matrix.api import ( MessageType, matrix_sync, - MatrixMessage, MatrixRoom, MatrixUser )