Add SendEvent class.

This commit is contained in:
poljar (Damir Jelić) 2018-02-13 15:50:30 +01:00
parent bafa7d23b7
commit 6b23dee045
4 changed files with 84 additions and 32 deletions

View file

@ -332,6 +332,16 @@ class MatrixSendMessage(MatrixMessage):
data
)
def decode_body(self, server):
object_hook = partial(
MatrixEvents.MatrixSendEvent.from_dict,
server,
self.room_id,
self.formatted_message,
)
return self._decode(server, object_hook)
class MatrixTopicMessage(MatrixMessage):
def __init__(self, client, room_id, topic):

View file

@ -17,7 +17,10 @@
from __future__ import unicode_literals
from builtins import str
import time
from matrix.globals import W, OPTIONS
from matrix.utils import color_for_tags
class MatrixEvent():
@ -79,3 +82,59 @@ class MatrixLoginEvent(MatrixEvent):
server,
"Error logging in: Invalid JSON response from server.",
fatal=True)
class MatrixSendEvent(MatrixEvent):
def __init__(self, server, room_id, event_id, message):
self.room_id = room_id
self.event_id = event_id
self.message = message
MatrixEvent.__init__(self, server)
def execute(self):
room_id = self.room_id
author = self.server.user
event_id = self.event_id
weechat_message = self.message.to_weechat()
date = int(time.time())
# This message will be part of the next sync, we already printed it out
# so ignore it in the sync.
self.server.ignore_event_list.append(event_id)
tag = ("notify_none,no_highlight,self_msg,log1,nick_{a},"
"prefix_nick_{color},matrix_id_{event_id},"
"matrix_message").format(
a=author,
color=color_for_tags("weechat.color.chat_nick_self"),
event_id=event_id)
message = "{author}\t{msg}".format(author=author, msg=weechat_message)
buf = self.server.buffers[room_id]
W.prnt_date_tags(buf, date, tag, message)
@classmethod
def from_dict(cls, server, room_id, message, parsed_dict):
try:
return cls(
server,
room_id,
parsed_dict["event_id"],
message
)
except KeyError:
try:
message = "Error sending message: {}.".format(parsed_dict["error"])
return MatrixErrorEvent(
server,
message,
fatal=False
)
except KeyError:
return MatrixErrorEvent(
server,
("Error sending message: Invalid JSON response "
"from server."),
fatal=False)

View file

@ -34,7 +34,12 @@ from matrix.api import (
MatrixUser
)
from matrix.utils import server_buffer_prnt, tags_from_line_data, prnt_debug
from matrix.utils import (
server_buffer_prnt,
tags_from_line_data,
prnt_debug,
color_for_tags
)
from matrix.plugin_options import RedactType, DebugType
@ -171,13 +176,6 @@ def date_from_age(age):
return date
def color_for_tags(color):
if color == "weechat.color.chat_nick_self":
option = W.config_get(color)
return W.config_string(option)
return color
def matrix_handle_room_text_message(server, room_id, event, old=False):
# type: (MatrixServer, str, Dict[str, Any], bool) -> None
tag = ""
@ -702,30 +700,8 @@ def matrix_handle_message(
server.sync()
elif message_type is MessageType.SEND:
room_id = message.room_id
author = server.user
weechat_message = message.formatted_message.to_weechat()
date = int(time.time())
# TODO the event_id can be missing if sending has failed for
# some reason
event_id = response["event_id"]
# This message will be part of the next sync, we already printed it out
# so ignore it in the sync.
server.ignore_event_list.append(event_id)
tag = ("notify_none,no_highlight,self_msg,log1,nick_{a},"
"prefix_nick_{color},matrix_id_{event_id},"
"matrix_message").format(
a=author,
color=color_for_tags("weechat.color.chat_nick_self"),
event_id=event_id)
data = "{author}\t{msg}".format(author=author, msg=weechat_message)
buf = server.buffers[room_id]
W.prnt_date_tags(buf, date, tag, data)
event = message.event
event.execute()
elif message_type == MessageType.ROOM_MSG:
# Response has no messages, that is we already got the oldest message

View file

@ -112,3 +112,10 @@ def server_buffer_set_title(server):
ip=ip_string)
W.buffer_set(server.server_buffer, "title", title)
def color_for_tags(color):
if color == "weechat.color.chat_nick_self":
option = W.config_get(color)
return W.config_string(option)
return color