Move the sync and login functions into the server class.

This commit is contained in:
poljar (Damir Jelić) 2018-02-12 14:53:19 +01:00
parent da13abc5fa
commit 390d08b229
4 changed files with 25 additions and 31 deletions

View file

@ -33,7 +33,6 @@ from matrix.utf import utf8_decode
from matrix.http import HttpResponse from matrix.http import HttpResponse
from matrix.api import ( from matrix.api import (
MessageType, MessageType,
matrix_login,
MatrixSendMessage MatrixSendMessage
) )
from matrix.messages import handle_http_response from matrix.messages import handle_http_response
@ -332,7 +331,7 @@ def finalize_connection(server):
server.connected = True server.connected = True
server.connecting = False server.connecting = False
matrix_login(server) server.login()
@utf8_decode @utf8_decode

View file

@ -26,8 +26,6 @@ try:
except ImportError: except ImportError:
from urllib.parse import quote, urlencode from urllib.parse import quote, urlencode
from matrix.globals import OPTIONS
from matrix.http import RequestType, HttpRequest from matrix.http import RequestType, HttpRequest
MATRIX_API_PATH = "/_matrix/client/r0" # type: str MATRIX_API_PATH = "/_matrix/client/r0" # type: str
@ -421,23 +419,3 @@ class MatrixRoom:
self.prev_batch = "" # type: str self.prev_batch = "" # type: str
self.users = dict() # type: Dict[str, MatrixUser] self.users = dict() # type: Dict[str, MatrixUser]
self.encrypted = False # type: bool 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)

View file

@ -30,7 +30,6 @@ from matrix.globals import W, OPTIONS
from matrix.api import ( from matrix.api import (
MessageType, MessageType,
matrix_sync,
MatrixRoom, MatrixRoom,
MatrixUser MatrixUser
) )
@ -687,14 +686,14 @@ def matrix_handle_message(
server.user_id = response["user_id"] server.user_id = response["user_id"]
server.client.access_token = server.access_token server.client.access_token = server.access_token
matrix_sync(server) server.sync()
elif message_type is MessageType.SYNC: elif message_type is MessageType.SYNC:
next_batch = response['next_batch'] next_batch = response['next_batch']
# we got the same batch again, nothing to do # we got the same batch again, nothing to do
if next_batch == server.next_batch: if next_batch == server.next_batch:
matrix_sync(server) server.sync()
return return
room_info = response['rooms'] room_info = response['rooms']
@ -703,7 +702,7 @@ def matrix_handle_message(
server.next_batch = next_batch server.next_batch = next_batch
# TODO add a delay to this # TODO add a delay to this
matrix_sync(server) server.sync()
elif message_type is MessageType.SEND: elif message_type is MessageType.SEND:
room_id = message.room_id room_id = message.room_id
@ -792,7 +791,7 @@ def handle_http_response(server, message):
# TODO handle try again response # TODO handle try again response
elif status_code == 504: elif status_code == 504:
if message.type == MessageType.SYNC: if message.type == MessageType.SYNC:
matrix_sync(server) server.sync()
elif status_code == 403: elif status_code == 403:
if message.type == MessageType.LOGIN: if message.type == MessageType.LOGIN:

View file

@ -33,8 +33,8 @@ from matrix.utils import (
create_server_buffer create_server_buffer
) )
from matrix.utf import utf8_decode from matrix.utf import utf8_decode
from matrix.globals import W, SERVERS from matrix.globals import W, SERVERS, OPTIONS
from matrix.api import MatrixClient from matrix.api import MatrixClient, MatrixSyncMessage, MatrixLoginMessage
class MatrixServer: class MatrixServer:
@ -392,6 +392,24 @@ class MatrixServer:
return True 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 @utf8_decode
def matrix_config_server_read_cb( def matrix_config_server_read_cb(