From d02abd079fe58bae467672dc7d2a140f9f07bb85 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 19 Feb 2020 01:14:02 +0200 Subject: Add support for editing messages --- matrix/event/event.go | 8 ++++++++ matrix/matrix.go | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'matrix') diff --git a/matrix/event/event.go b/matrix/event/event.go index 143d35d..8506c9c 100644 --- a/matrix/event/event.go +++ b/matrix/event/event.go @@ -25,6 +25,14 @@ type Event struct { Gomuks GomuksContent `json:"-"` } +func (evt *Event) SomewhatDangerousCopy() *Event { + base := *evt.Event + return &Event{ + Event: &base, + Gomuks: evt.Gomuks, + } +} + func Wrap(event *mautrix.Event) *Event { return &Event{Event: event} } diff --git a/matrix/matrix.go b/matrix/matrix.go index f77a257..c33a749 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -218,7 +218,7 @@ func (c *Container) SingleSignOn() error { if err != nil { return err } - err = <- errChan + err = <-errChan return err } @@ -690,7 +690,7 @@ func (c *Container) MarkRead(roomID, eventID string) { var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") -func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) *event.Event { +func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string, edit *event.Event) *event.Event { content := format.RenderMarkdown(text) content.MsgType = msgtype @@ -698,6 +698,19 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag content.Body = mentionRegex.ReplaceAllString(content.Body, "$1") content.Body = roomRegex.ReplaceAllString(content.Body, "$1") + if edit != nil { + contentCopy := content + content.NewContent = &contentCopy + content.Body = "* " + content.Body + if len(content.FormattedBody) > 0 { + content.FormattedBody = "* " + content.FormattedBody + } + content.RelatesTo = &mautrix.RelatesTo{ + Type: mautrix.RelReplace, + EventID: edit.ID, + } + } + txnID := c.client.TxnID() localEcho := event.Wrap(&mautrix.Event{ ID: txnID, @@ -711,6 +724,10 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag }, }) localEcho.Gomuks.OutgoingState = event.StateLocalEcho + if edit != nil { + localEcho.ID = edit.ID + localEcho.Gomuks.Edits = []*event.Event{localEcho} + } return localEcho } -- cgit v1.2.3