Use *maubot.Event everywhere to remove reply fallback bugs
This commit is contained in:
parent
944b384633
commit
5b04962ac7
4 changed files with 33 additions and 41 deletions
|
@ -24,7 +24,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
*gomatrix.Event
|
*maubot.Event
|
||||||
Client *Client
|
Client *Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,30 +39,34 @@ func roundtripContent(rawContent map[string]interface{}) (content maubot.Content
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (evt *Event) Interface() *maubot.Event {
|
func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event {
|
||||||
var stateKey string
|
var stateKey string
|
||||||
if evt.StateKey != nil {
|
if mxEvent.StateKey != nil {
|
||||||
stateKey = *evt.StateKey
|
stateKey = *mxEvent.StateKey
|
||||||
|
}
|
||||||
|
event := &Event{
|
||||||
|
Client: client,
|
||||||
}
|
}
|
||||||
mbEvent := &maubot.Event{
|
mbEvent := &maubot.Event{
|
||||||
EventFuncs: evt,
|
EventFuncs: event,
|
||||||
StateKey: stateKey,
|
StateKey: stateKey,
|
||||||
Sender: evt.Sender,
|
Sender: mxEvent.Sender,
|
||||||
Type: maubot.EventType(evt.Type),
|
Type: maubot.EventType(mxEvent.Type),
|
||||||
Timestamp: evt.Timestamp,
|
Timestamp: mxEvent.Timestamp,
|
||||||
ID: evt.ID,
|
ID: mxEvent.ID,
|
||||||
RoomID: evt.RoomID,
|
RoomID: mxEvent.RoomID,
|
||||||
Content: roundtripContent(evt.Content),
|
Content: roundtripContent(mxEvent.Content),
|
||||||
Redacts: evt.Redacts,
|
Redacts: mxEvent.Redacts,
|
||||||
Unsigned: maubot.Unsigned{
|
Unsigned: maubot.Unsigned{
|
||||||
PrevContent: roundtripContent(evt.Unsigned.PrevContent),
|
PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent),
|
||||||
PrevSender: evt.Unsigned.PrevSender,
|
PrevSender: mxEvent.Unsigned.PrevSender,
|
||||||
ReplacesState: evt.Unsigned.ReplacesState,
|
ReplacesState: mxEvent.Unsigned.ReplacesState,
|
||||||
Age: evt.Unsigned.Age,
|
Age: mxEvent.Unsigned.Age,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
RemoveReplyFallback(mbEvent)
|
RemoveReplyFallback(mbEvent)
|
||||||
return mbEvent
|
event.Event = mbEvent
|
||||||
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (evt *Event) MarkRead() error {
|
func (evt *Event) MarkRead() error {
|
||||||
|
@ -70,18 +74,15 @@ func (evt *Event) MarkRead() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (evt *Event) Reply(text string) (string, error) {
|
func (evt *Event) Reply(text string) (string, error) {
|
||||||
return evt.SendRawEvent(maubot.EventMessage,
|
return evt.ReplyContent(RenderMarkdown(text))
|
||||||
SetReply(
|
|
||||||
RenderMarkdown(text),
|
|
||||||
evt.Event))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (evt *Event) ReplyContent(content maubot.Content) (string, error) {
|
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) {
|
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) {
|
func (evt *Event) SendContent(content maubot.Content) (string, error) {
|
||||||
|
|
|
@ -49,13 +49,6 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
|
||||||
return client, nil
|
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) {
|
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
|
||||||
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
|
client.syncer.OnEventType(evt, func(evt *maubot.Event) maubot.EventHandlerResult {
|
||||||
if evt.Sender == client.UserID {
|
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)
|
log.Warnf("Failed to get event %s @ %s: %v\n", eventID, roomID, err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return client.ParseEvent(evt).Interface()
|
return client.ParseEvent(evt).Event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
|
func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
|
||||||
|
|
|
@ -23,7 +23,6 @@ import (
|
||||||
|
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
"maubot.xyz"
|
"maubot.xyz"
|
||||||
"maunium.net/go/gomatrix"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
|
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
|
||||||
|
@ -60,11 +59,10 @@ const ReplyFormat = `<mx-reply><blockquote>
|
||||||
</blockquote></mx-reply>
|
</blockquote></mx-reply>
|
||||||
`
|
`
|
||||||
|
|
||||||
func ReplyFallbackHTML(evt *gomatrix.Event) string {
|
func ReplyFallbackHTML(evt *Event) string {
|
||||||
body, ok := evt.Content["formatted_body"].(string)
|
body := evt.Content.FormattedBody
|
||||||
if !ok {
|
if len(body) == 0 {
|
||||||
body, _ = evt.Content["body"].(string)
|
body = html.EscapeString(evt.Content.Body)
|
||||||
body = html.EscapeString(body)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
senderDisplayName := evt.Sender
|
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)
|
return fmt.Sprintf(ReplyFormat, evt.RoomID, evt.ID, evt.Sender, senderDisplayName, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReplyFallbackText(evt *gomatrix.Event) string {
|
func ReplyFallbackText(evt *Event) string {
|
||||||
body, _ := evt.Content["body"].(string)
|
body := evt.Content.Body
|
||||||
lines := strings.Split(strings.TrimSpace(body), "\n")
|
lines := strings.Split(strings.TrimSpace(body), "\n")
|
||||||
firstLine, lines := lines[0], lines[1:]
|
firstLine, lines := lines[0], lines[1:]
|
||||||
|
|
||||||
|
@ -88,7 +86,7 @@ func ReplyFallbackText(evt *gomatrix.Event) string {
|
||||||
return fallbackText.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.EventID = inReplyTo.ID
|
||||||
content.RelatesTo.InReplyTo.RoomID = inReplyTo.RoomID
|
content.RelatesTo.InReplyTo.RoomID = inReplyTo.RoomID
|
||||||
|
|
||||||
|
|
|
@ -135,7 +135,7 @@ func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, fn := range listeners {
|
for _, fn := range listeners {
|
||||||
if fn(event.Interface()) {
|
if fn(event.Event) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue