From dbee49476d47bb391dc6af0b774f9fd101b0c917 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 9 Apr 2019 18:45:41 +0300 Subject: Start refactoring various things in message rendering and sending --- matrix/matrix.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'matrix') diff --git a/matrix/matrix.go b/matrix/matrix.go index b9816f2..960e92b 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -303,6 +303,7 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { debug.Printf("Failed to add event %s to history: %v", evt.ID, err) } + // TODO switch to roomView.AddEvent message := mainView.ParseEvent(roomView, evt) if message != nil { roomView.AddMessage(message, ifc.AppendMessage) @@ -494,10 +495,7 @@ func (c *Container) SendMessage(roomID string, msgtype mautrix.MessageType, text var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") -// SendMarkdownMessage sends a message with the given text to the given room. -// -// If the given text contains markdown formatting symbols, it will be rendered into HTML before sending. -// Otherwise, it will be sent as plain text. +// SendMarkdownMessage sends a message with the given markdown text to the given room. func (c *Container) SendMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) (string, error) { defer debug.Recover() @@ -509,8 +507,23 @@ func (c *Container) SendMarkdownMessage(roomID string, msgtype mautrix.MessageTy content.Body = roomRegex.ReplaceAllString(content.Body, "$1") c.SendTyping(roomID, false) - resp, err := c.client.SendMessageEvent(roomID, mautrix.EventMessage, content) + roomView := c.ui.MainView().GetRoom(roomID) + txnID := c.client.TxnID() + localEcho := roomView.ParseEvent(&mautrix.Event{ + Sender: c.config.UserID, + Type: mautrix.EventMessage, + Timestamp: time.Now().UnixNano() / 1e6, + RoomID: roomID, + Content: content, + Unsigned: mautrix.Unsigned{ + TransactionID: txnID, + OutgoingState: mautrix.EventStateLocalEcho, + }, + }) + roomView.AppendMessage(localEcho) + resp, err := c.client.SendMessageEvent(roomID, mautrix.EventMessage, content, mautrix.ReqSendEvent{TransactionID: txnID}) if err != nil { + roomView.MarkMessageFailed(localEcho, err) return "", err } return resp.EventID, nil -- cgit v1.2.3