From 5b04962ac715f59a9d402e263f5012f72f2054da Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 15 Jun 2018 18:34:38 +0300 Subject: [PATCH] Use *maubot.Event everywhere to remove reply fallback bugs --- matrix/event.go | 47 +++++++++++++++++++++++---------------------- matrix/matrix.go | 9 +-------- matrix/replyutil.go | 16 +++++++-------- matrix/sync.go | 2 +- 4 files changed, 33 insertions(+), 41 deletions(-) diff --git a/matrix/event.go b/matrix/event.go index 1fa39af..3abb801 100644 --- a/matrix/event.go +++ b/matrix/event.go @@ -24,7 +24,7 @@ import ( ) type Event struct { - *gomatrix.Event + *maubot.Event Client *Client } @@ -39,30 +39,34 @@ func roundtripContent(rawContent map[string]interface{}) (content maubot.Content return } -func (evt *Event) Interface() *maubot.Event { +func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event { var stateKey string - if evt.StateKey != nil { - stateKey = *evt.StateKey + if mxEvent.StateKey != nil { + stateKey = *mxEvent.StateKey + } + event := &Event{ + Client: client, } mbEvent := &maubot.Event{ - EventFuncs: evt, + EventFuncs: event, StateKey: stateKey, - Sender: evt.Sender, - Type: maubot.EventType(evt.Type), - Timestamp: evt.Timestamp, - ID: evt.ID, - RoomID: evt.RoomID, - Content: roundtripContent(evt.Content), - Redacts: evt.Redacts, + Sender: mxEvent.Sender, + Type: maubot.EventType(mxEvent.Type), + Timestamp: mxEvent.Timestamp, + ID: mxEvent.ID, + RoomID: mxEvent.RoomID, + Content: roundtripContent(mxEvent.Content), + Redacts: mxEvent.Redacts, Unsigned: maubot.Unsigned{ - PrevContent: roundtripContent(evt.Unsigned.PrevContent), - PrevSender: evt.Unsigned.PrevSender, - ReplacesState: evt.Unsigned.ReplacesState, - Age: evt.Unsigned.Age, + PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent), + PrevSender: mxEvent.Unsigned.PrevSender, + ReplacesState: mxEvent.Unsigned.ReplacesState, + Age: mxEvent.Unsigned.Age, }, } RemoveReplyFallback(mbEvent) - return mbEvent + event.Event = mbEvent + return event } func (evt *Event) MarkRead() error { @@ -70,18 +74,15 @@ func (evt *Event) MarkRead() error { } func (evt *Event) Reply(text string) (string, error) { - return evt.SendRawEvent(maubot.EventMessage, - SetReply( - RenderMarkdown(text), - evt.Event)) + return evt.ReplyContent(RenderMarkdown(text)) } func (evt *Event) ReplyContent(content maubot.Content) (string, error) { - return evt.SendRawEvent(maubot.EventMessage, SetReply(content, evt.Event)) + return evt.SendContent(SetReply(content, evt)) } func (evt *Event) SendMessage(text string) (string, error) { - return evt.SendRawEvent(maubot.EventMessage, RenderMarkdown(text)) + return evt.SendContent(RenderMarkdown(text)) } func (evt *Event) SendContent(content maubot.Content) (string, error) { diff --git a/matrix/matrix.go b/matrix/matrix.go index c51fdb9..df1e9e4 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -49,13 +49,6 @@ func NewClient(db *database.MatrixClient) (*Client, error) { return client, nil } -func (client *Client) ParseEvent(evt *gomatrix.Event) *Event { - return &Event{ - Client: client, - Event: evt, - } -} - func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) { client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult { if evt.Sender == client.UserID { @@ -71,7 +64,7 @@ func (client *Client) GetEvent(roomID, eventID string) *maubot.Event { log.Warnf("Failed to get event %s @ %s: %v\n", eventID, roomID, err) return nil } - return client.ParseEvent(evt).Interface() + return client.ParseEvent(evt).Event } func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult { diff --git a/matrix/replyutil.go b/matrix/replyutil.go index 34ce0d2..29e7773 100644 --- a/matrix/replyutil.go +++ b/matrix/replyutil.go @@ -23,7 +23,6 @@ import ( "golang.org/x/net/html" "maubot.xyz" - "maunium.net/go/gomatrix" ) var HTMLReplyFallbackRegex = regexp.MustCompile(`^[\s\S]+?`) @@ -60,11 +59,10 @@ const ReplyFormat = `
` -func ReplyFallbackHTML(evt *gomatrix.Event) string { - body, ok := evt.Content["formatted_body"].(string) - if !ok { - body, _ = evt.Content["body"].(string) - body = html.EscapeString(body) +func ReplyFallbackHTML(evt *Event) string { + body := evt.Content.FormattedBody + if len(body) == 0 { + body = html.EscapeString(evt.Content.Body) } senderDisplayName := evt.Sender @@ -72,8 +70,8 @@ func ReplyFallbackHTML(evt *gomatrix.Event) string { return fmt.Sprintf(ReplyFormat, evt.RoomID, evt.ID, evt.Sender, senderDisplayName, body) } -func ReplyFallbackText(evt *gomatrix.Event) string { - body, _ := evt.Content["body"].(string) +func ReplyFallbackText(evt *Event) string { + body := evt.Content.Body lines := strings.Split(strings.TrimSpace(body), "\n") firstLine, lines := lines[0], lines[1:] @@ -88,7 +86,7 @@ func ReplyFallbackText(evt *gomatrix.Event) string { return fallbackText.String() } -func SetReply(content maubot.Content, inReplyTo *gomatrix.Event) maubot.Content { +func SetReply(content maubot.Content, inReplyTo *Event) maubot.Content { content.RelatesTo.InReplyTo.EventID = inReplyTo.ID content.RelatesTo.InReplyTo.RoomID = inReplyTo.RoomID diff --git a/matrix/sync.go b/matrix/sync.go index 0900184..dbdaa68 100644 --- a/matrix/sync.go +++ b/matrix/sync.go @@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) { return } for _, fn := range listeners { - if fn(event.Interface()) { + if fn(event.Event) { break } }