Remove the message type enum.
This commit is contained in:
parent
9f70a1440b
commit
bf0bffc879
4 changed files with 17 additions and 33 deletions
4
main.py
4
main.py
|
@ -249,7 +249,9 @@ def receive_cb(server_name, file_descriptor):
|
||||||
prnt_debug(DebugType.MESSAGING, server,
|
prnt_debug(DebugType.MESSAGING, server,
|
||||||
("{prefix}Received message of type {t} and "
|
("{prefix}Received message of type {t} and "
|
||||||
"status {s}").format(
|
"status {s}").format(
|
||||||
prefix=W.prefix("error"), t=message.type, s=status))
|
prefix=W.prefix("error"),
|
||||||
|
t=message.__class__.__name__,
|
||||||
|
s=status))
|
||||||
|
|
||||||
# Message done, reset the parser state.
|
# Message done, reset the parser state.
|
||||||
server.reset_parser()
|
server.reset_parser()
|
||||||
|
|
|
@ -38,19 +38,6 @@ import matrix.events as MatrixEvents
|
||||||
MATRIX_API_PATH = "/_matrix/client/r0" # type: str
|
MATRIX_API_PATH = "/_matrix/client/r0" # type: str
|
||||||
|
|
||||||
|
|
||||||
@unique
|
|
||||||
class MessageType(Enum):
|
|
||||||
LOGIN = 0
|
|
||||||
SYNC = 1
|
|
||||||
SEND = 2
|
|
||||||
TOPIC = 3
|
|
||||||
REDACT = 4
|
|
||||||
ROOM_MSG = 5
|
|
||||||
JOIN = 6
|
|
||||||
PART = 7
|
|
||||||
INVITE = 8
|
|
||||||
|
|
||||||
|
|
||||||
class MatrixClient:
|
class MatrixClient:
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
@ -217,13 +204,11 @@ class MatrixMessage():
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
message_type, # type: MessageType
|
|
||||||
request_func, # type: Callable[[...], HttpRequest]
|
request_func, # type: Callable[[...], HttpRequest]
|
||||||
func_args,
|
func_args,
|
||||||
):
|
):
|
||||||
# type: (...) -> None
|
# type: (...) -> None
|
||||||
# yapf: disable
|
# yapf: disable
|
||||||
self.type = message_type # type: MessageType
|
|
||||||
|
|
||||||
self.request = None # type: HttpRequest
|
self.request = None # type: HttpRequest
|
||||||
self.response = None # type: HttpResponse
|
self.response = None # type: HttpResponse
|
||||||
|
@ -268,7 +253,7 @@ class MatrixLoginMessage(MatrixMessage):
|
||||||
if device_id:
|
if device_id:
|
||||||
data["device_id"] = device_id
|
data["device_id"] = device_id
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.LOGIN, client.login, data)
|
MatrixMessage.__init__(self, client.login, data)
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixLoginEvent.from_dict, server)
|
object_hook = partial(MatrixEvents.MatrixLoginEvent.from_dict, server)
|
||||||
|
@ -287,7 +272,7 @@ class MatrixSyncMessage(MatrixMessage):
|
||||||
if limit:
|
if limit:
|
||||||
data["sync_filter"] = {"room": {"timeline": {"limit": limit}}}
|
data["sync_filter"] = {"room": {"timeline": {"limit": limit}}}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.SYNC, client.sync, data)
|
MatrixMessage.__init__(self, client.sync, data)
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixSyncEvent.from_dict, server)
|
object_hook = partial(MatrixEvents.MatrixSyncEvent.from_dict, server)
|
||||||
|
@ -312,8 +297,7 @@ class MatrixSendMessage(MatrixMessage):
|
||||||
if self.formatted_message.is_formatted:
|
if self.formatted_message.is_formatted:
|
||||||
data["formatted_content"] = self.formatted_message.to_html()
|
data["formatted_content"] = self.formatted_message.to_html()
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.SEND, client.room_send_message,
|
MatrixMessage.__init__(self, client.room_send_message, data)
|
||||||
data)
|
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(
|
object_hook = partial(
|
||||||
|
@ -334,7 +318,7 @@ class MatrixTopicMessage(MatrixMessage):
|
||||||
|
|
||||||
data = {"room_id": self.room_id, "topic": self.topic}
|
data = {"room_id": self.room_id, "topic": self.topic}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.TOPIC, client.room_topic, data)
|
MatrixMessage.__init__(self, client.room_topic, data)
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(
|
object_hook = partial(
|
||||||
|
@ -359,8 +343,7 @@ class MatrixRedactMessage(MatrixMessage):
|
||||||
if reason:
|
if reason:
|
||||||
data["reason"] = reason
|
data["reason"] = reason
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.REDACT, client.room_redact,
|
MatrixMessage.__init__(self, client.room_redact, data)
|
||||||
data)
|
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(
|
object_hook = partial(
|
||||||
|
@ -385,8 +368,7 @@ class MatrixBacklogMessage(MatrixMessage):
|
||||||
"limit": limit
|
"limit": limit
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.ROOM_MSG,
|
MatrixMessage.__init__(self, client.room_get_messages, data)
|
||||||
client.room_get_messages, data)
|
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixBacklogEvent.from_dict, server,
|
object_hook = partial(MatrixEvents.MatrixBacklogEvent.from_dict, server,
|
||||||
|
@ -402,7 +384,7 @@ class MatrixJoinMessage(MatrixMessage):
|
||||||
|
|
||||||
data = {"room_id": self.room_id}
|
data = {"room_id": self.room_id}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.JOIN, client.room_join, data)
|
MatrixMessage.__init__(self, client.room_join, data)
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixJoinEvent.from_dict, server,
|
object_hook = partial(MatrixEvents.MatrixJoinEvent.from_dict, server,
|
||||||
|
@ -418,7 +400,7 @@ class MatrixPartMessage(MatrixMessage):
|
||||||
|
|
||||||
data = {"room_id": self.room_id}
|
data = {"room_id": self.room_id}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.PART, client.room_leave, data)
|
MatrixMessage.__init__(self, client.room_leave, data)
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixPartEvent.from_dict, server,
|
object_hook = partial(MatrixEvents.MatrixPartEvent.from_dict, server,
|
||||||
|
@ -435,8 +417,7 @@ class MatrixInviteMessage(MatrixMessage):
|
||||||
|
|
||||||
data = {"room_id": self.room_id, "user_id": self.user_id}
|
data = {"room_id": self.room_id, "user_id": self.user_id}
|
||||||
|
|
||||||
MatrixMessage.__init__(self, MessageType.INVITE, client.room_invite,
|
MatrixMessage.__init__(self, client.room_invite, data)
|
||||||
data)
|
|
||||||
|
|
||||||
def decode_body(self, server):
|
def decode_body(self, server):
|
||||||
object_hook = partial(MatrixEvents.MatrixInviteEvent.from_dict, server,
|
object_hook = partial(MatrixEvents.MatrixInviteEvent.from_dict, server,
|
||||||
|
|
|
@ -24,7 +24,7 @@ import matrix.globals
|
||||||
from matrix.globals import W, OPTIONS, SERVERS
|
from matrix.globals import W, OPTIONS, SERVERS
|
||||||
|
|
||||||
from matrix.utf import utf8_decode
|
from matrix.utf import utf8_decode
|
||||||
from matrix.api import (MessageType, MatrixTopicMessage, MatrixRedactMessage,
|
from matrix.api import (MatrixTopicMessage, MatrixRedactMessage,
|
||||||
MatrixBacklogMessage, MatrixJoinMessage,
|
MatrixBacklogMessage, MatrixJoinMessage,
|
||||||
MatrixPartMessage, MatrixInviteMessage)
|
MatrixPartMessage, MatrixInviteMessage)
|
||||||
from matrix.utils import key_from_value, tags_from_line_data
|
from matrix.utils import key_from_value, tags_from_line_data
|
||||||
|
|
|
@ -173,7 +173,8 @@ class MatrixServer:
|
||||||
prnt_debug(DebugType.MESSAGING, self,
|
prnt_debug(DebugType.MESSAGING, self,
|
||||||
("{prefix} Failed sending message of type {t}. "
|
("{prefix} Failed sending message of type {t}. "
|
||||||
"Adding to queue").format(
|
"Adding to queue").format(
|
||||||
prefix=W.prefix("error"), t=message.type))
|
prefix=W.prefix("error"),
|
||||||
|
t=message.__class__.__name__))
|
||||||
self.send_queue.append(message)
|
self.send_queue.append(message)
|
||||||
|
|
||||||
def try_send(self, message):
|
def try_send(self, message):
|
||||||
|
@ -425,7 +426,7 @@ class MatrixServer:
|
||||||
"\n Receive delay: {r} ms"
|
"\n Receive delay: {r} ms"
|
||||||
"\n Handling time: {h} ms"
|
"\n Handling time: {h} ms"
|
||||||
"\n Total time: {total} ms").format(
|
"\n Total time: {total} ms").format(
|
||||||
t=message.type,
|
t=message.__class__.__name__,
|
||||||
c=creation_date,
|
c=creation_date,
|
||||||
s=(message.send_time - message.creation_time) * 1000,
|
s=(message.send_time - message.creation_time) * 1000,
|
||||||
r=(message.receive_time - message.send_time) * 1000,
|
r=(message.receive_time - message.send_time) * 1000,
|
||||||
|
@ -522,7 +523,7 @@ def matrix_timer_cb(server_name, remaining_calls):
|
||||||
prnt_debug(
|
prnt_debug(
|
||||||
DebugType.MESSAGING,
|
DebugType.MESSAGING,
|
||||||
server, ("Timer hook found message of type {t} in queue. Sending "
|
server, ("Timer hook found message of type {t} in queue. Sending "
|
||||||
"out.".format(t=message.type)))
|
"out.".format(t=message.__class__.__name__)))
|
||||||
|
|
||||||
if not server.send(message):
|
if not server.send(message):
|
||||||
# We got an error while sending the last message return the message
|
# We got an error while sending the last message return the message
|
||||||
|
|
Loading…
Add table
Reference in a new issue