Use *maubot.Event everywhere to remove reply fallback bugs

This commit is contained in:
Tulir Asokan 2018-06-15 18:34:38 +03:00
parent 944b384633
commit 5b04962ac7
4 changed files with 33 additions and 41 deletions

View file

@ -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) {

View file

@ -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 {

View file

@ -23,7 +23,6 @@ import (
"golang.org/x/net/html"
"maubot.xyz"
"maunium.net/go/gomatrix"
)
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
@ -60,11 +59,10 @@ const ReplyFormat = `<mx-reply><blockquote>
</blockquote></mx-reply>
`
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

View file

@ -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
}
}