Add event classes.

This commit is contained in:
poljar (Damir Jelić) 2018-02-13 11:36:00 +01:00
parent 7da03db7c1
commit e9c388ca4d
3 changed files with 64 additions and 4 deletions

View file

@ -27,6 +27,7 @@ except ImportError:
from urllib.parse import quote, urlencode
from matrix.http import RequestType, HttpRequest
import matrix.events as MatrixEvents
MATRIX_API_PATH = "/_matrix/client/r0" # type: str
@ -266,6 +267,20 @@ class MatrixLoginMessage(MatrixMessage):
data
)
def to_event(self, server):
response = self.decoded_response
try:
access_token = response["access_token"]
user_id = response["user_id"]
return (
True,
MatrixEvents.MatrixLoginEvent(server, user_id, access_token)
)
except KeyError as error:
return (False, error)
class MatrixSyncMessage(MatrixMessage):
def __init__(self, client, next_batch=None, limit=None):

42
matrix/events.py Normal file
View file

@ -0,0 +1,42 @@
# -*- coding: utf-8 -*-
# Copyright © 2018 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import unicode_literals
from builtins import str
from matrix.globals import W, OPTIONS
class MatrixEvent():
def __init__(self, server):
self.server = server
def execute(self):
pass
class MatrixLoginEvent():
def __init__(self, server, user_id, access_token):
self.user_id = user_id
self.access_token = access_token
MatrixEvent.__init__(self, server)
def execute(self):
self.server.access_token = self.access_token
self.server.user_id = self.user_id
self.server.client.access_token = self.access_token
self.server.sync()

View file

@ -682,11 +682,14 @@ def matrix_handle_message(
response = message.decoded_response
if message_type is MessageType.LOGIN:
server.access_token = response["access_token"]
server.user_id = response["user_id"]
server.client.access_token = server.access_token
ret, event = message.to_event(server)
server.sync()
if ret:
event.execute()
else:
message = ("{prefix}Error while parsing login response.")
W.prnt(server.server_buffer, message)
server.disconnect(reconnect=False)
elif message_type is MessageType.SYNC:
next_batch = response['next_batch']