Refactor the SEND message type.
This commit is contained in:
parent
f83e02df13
commit
39c8591926
3 changed files with 25 additions and 16 deletions
13
main.py
13
main.py
|
@ -409,23 +409,14 @@ def room_input_cb(server_name, buffer, input_data):
|
|||
|
||||
formatted_data = colors.parse_input_line(input_data)
|
||||
|
||||
body = {
|
||||
"msgtype": "m.text",
|
||||
"body": colors.formatted_to_plain(formatted_data)
|
||||
}
|
||||
|
||||
if colors.formatted(formatted_data):
|
||||
body["format"] = "org.matrix.custom.html"
|
||||
body["formatted_body"] = colors.formatted_to_html(formatted_data)
|
||||
|
||||
extra_data = {
|
||||
"author": server.user,
|
||||
"message": colors.formatted_to_weechat(W, formatted_data),
|
||||
"room_id": room_id
|
||||
}
|
||||
|
||||
message = MatrixMessage(server, OPTIONS, MessageType.SEND,
|
||||
data=body, room_id=room_id,
|
||||
message = MatrixMessage(server, OPTIONS, MessageType.SEND, room_id=room_id,
|
||||
formatted_message=formatted_data,
|
||||
extra_data=extra_data)
|
||||
|
||||
server.send_or_queue(message)
|
||||
|
|
|
@ -29,6 +29,7 @@ except ImportError:
|
|||
from matrix.globals import OPTIONS
|
||||
|
||||
from matrix.http import RequestType, HttpRequest
|
||||
from matrix.colors import formatted_to_plain, formatted_to_html, is_formatted
|
||||
|
||||
MATRIX_API_PATH = "/_matrix/client/r0" # type: str
|
||||
|
||||
|
@ -100,10 +101,19 @@ class MatrixClient:
|
|||
|
||||
return HttpRequest(RequestType.GET, self.host, path)
|
||||
|
||||
def room_send_message(self, room_id, content):
|
||||
def room_send_message(self, room_id, content, formatted_content=None):
|
||||
# type: (str, Dict[str, str]) -> HttpRequest
|
||||
query_parameters = {"access_token": self.access_token}
|
||||
|
||||
body = {
|
||||
"msgtype": "m.text",
|
||||
"body": content
|
||||
}
|
||||
|
||||
if formatted_content:
|
||||
body["format"] = "org.matrix.custom.html"
|
||||
body["formatted_body"] = formatted_content
|
||||
|
||||
path = ("{api}/rooms/{room}/send/m.room.message/"
|
||||
"{tx_id}?{query_parameters}").format(
|
||||
api=MATRIX_API_PATH,
|
||||
|
@ -111,7 +121,7 @@ class MatrixClient:
|
|||
tx_id=quote(str(self._get_txn_id())),
|
||||
query_parameters=urlencode(query_parameters))
|
||||
|
||||
return HttpRequest(RequestType.PUT, self.host, path, content)
|
||||
return HttpRequest(RequestType.PUT, self.host, path, body)
|
||||
|
||||
def room_topic(self, room_id, topic):
|
||||
# type: (str, str) -> HttpRequest
|
||||
|
@ -249,7 +259,15 @@ class MatrixMessage:
|
|||
self.request = server.client.sync(server.next_batch, sync_filter)
|
||||
|
||||
elif message_type == MessageType.SEND:
|
||||
self.request = server.client.room_send_message(room_id, data)
|
||||
assert self.formatted_message
|
||||
|
||||
data = {"content": formatted_to_plain(self.formatted_message)}
|
||||
|
||||
if is_formatted(self.formatted_message):
|
||||
data["formatted_content"] = formatted_to_html(
|
||||
self.formatted_message)
|
||||
|
||||
self.request = server.client.room_send_message(room_id, **data)
|
||||
|
||||
elif message_type == MessageType.TOPIC:
|
||||
assert self.topic
|
||||
|
|
|
@ -722,7 +722,7 @@ def parse_input_line(line):
|
|||
return substrings
|
||||
|
||||
|
||||
def formatted(strings):
|
||||
def is_formatted(strings):
|
||||
# type: (List[FormattedString]) -> bool
|
||||
for string in strings:
|
||||
if string.attributes != DEFAULT_ATRIBUTES:
|
||||
|
@ -781,7 +781,7 @@ def formatted_to_html(strings):
|
|||
return "".join(html_string)
|
||||
|
||||
|
||||
# TODO do we want at least some formating using unicode
|
||||
# TODO do we want at least some formatting using unicode
|
||||
# (strikethrough, quotes)?
|
||||
def formatted_to_plain(strings):
|
||||
# type: (List[FormattedString]) -> str
|
||||
|
|
Loading…
Add table
Reference in a new issue