diff --git a/main.py b/main.py index bf3ae4b..3933a1e 100644 --- a/main.py +++ b/main.py @@ -33,7 +33,6 @@ from matrix.utf import utf8_decode from matrix.http import HttpResponse from matrix.api import ( MessageType, - matrix_login, MatrixSendMessage ) from matrix.messages import handle_http_response @@ -332,7 +331,7 @@ def finalize_connection(server): server.connected = True server.connecting = False - matrix_login(server) + server.login() @utf8_decode diff --git a/matrix/api.py b/matrix/api.py index 05efae2..23b0c92 100644 --- a/matrix/api.py +++ b/matrix/api.py @@ -26,8 +26,6 @@ try: except ImportError: from urllib.parse import quote, urlencode -from matrix.globals import OPTIONS - from matrix.http import RequestType, HttpRequest MATRIX_API_PATH = "/_matrix/client/r0" # type: str @@ -421,23 +419,3 @@ class MatrixRoom: self.prev_batch = "" # type: str self.users = dict() # type: Dict[str, MatrixUser] self.encrypted = False # type: bool - - -def matrix_sync(server): - message = MatrixSyncMessage( - server.client, - server.next_batch, - OPTIONS.sync_limit - ) - server.send_queue.append(message) - - -def matrix_login(server): - # type: (MatrixServer) -> None - message = MatrixLoginMessage( - server.client, - server.user, - server.password, - server.device_name - ) - server.send_or_queue(message) diff --git a/matrix/messages.py b/matrix/messages.py index 076925d..f9c10f9 100644 --- a/matrix/messages.py +++ b/matrix/messages.py @@ -30,7 +30,6 @@ from matrix.globals import W, OPTIONS from matrix.api import ( MessageType, - matrix_sync, MatrixRoom, MatrixUser ) @@ -687,14 +686,14 @@ def matrix_handle_message( server.user_id = response["user_id"] server.client.access_token = server.access_token - matrix_sync(server) + server.sync() elif message_type is MessageType.SYNC: next_batch = response['next_batch'] # we got the same batch again, nothing to do if next_batch == server.next_batch: - matrix_sync(server) + server.sync() return room_info = response['rooms'] @@ -703,7 +702,7 @@ def matrix_handle_message( server.next_batch = next_batch # TODO add a delay to this - matrix_sync(server) + server.sync() elif message_type is MessageType.SEND: room_id = message.room_id @@ -792,7 +791,7 @@ def handle_http_response(server, message): # TODO handle try again response elif status_code == 504: if message.type == MessageType.SYNC: - matrix_sync(server) + server.sync() elif status_code == 403: if message.type == MessageType.LOGIN: diff --git a/matrix/server.py b/matrix/server.py index cfe8436..3a0b8d0 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -33,8 +33,8 @@ from matrix.utils import ( create_server_buffer ) from matrix.utf import utf8_decode -from matrix.globals import W, SERVERS -from matrix.api import MatrixClient +from matrix.globals import W, SERVERS, OPTIONS +from matrix.api import MatrixClient, MatrixSyncMessage, MatrixLoginMessage class MatrixServer: @@ -392,6 +392,24 @@ class MatrixServer: return True + def sync(self): + message = MatrixSyncMessage( + self.client, + self.next_batch, + OPTIONS.sync_limit + ) + self.send_queue.append(message) + + def login(self): + # type: (MatrixServer) -> None + message = MatrixLoginMessage( + self.client, + self.user, + self.password, + self.device_name + ) + self.send_or_queue(message) + @utf8_decode def matrix_config_server_read_cb(