Move interfaces to maubot package and other stuff to app/
This commit is contained in:
parent
ef8fffaff8
commit
3a27831112
14 changed files with 329 additions and 198 deletions
|
@ -17,7 +17,9 @@
|
|||
package matrix
|
||||
|
||||
import (
|
||||
"maubot.xyz/interfaces"
|
||||
"encoding/json"
|
||||
|
||||
"maubot.xyz"
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
|
@ -26,43 +28,64 @@ type Event struct {
|
|||
Client *Client
|
||||
}
|
||||
|
||||
func (evt *Event) Interface() *interfaces.Event {
|
||||
func roundtripContent(rawContent map[string]interface{}) (content maubot.Content) {
|
||||
if len(rawContent) == 0 {
|
||||
content.Raw = rawContent
|
||||
return
|
||||
}
|
||||
data, _ := json.Marshal(&rawContent)
|
||||
json.Unmarshal(data, &content)
|
||||
content.Raw = rawContent
|
||||
return
|
||||
}
|
||||
|
||||
func (evt *Event) Interface() *maubot.Event {
|
||||
var stateKey string
|
||||
if evt.StateKey != nil {
|
||||
stateKey = *evt.StateKey
|
||||
}
|
||||
return &interfaces.Event{
|
||||
mbEvent := &maubot.Event{
|
||||
EventFuncs: evt,
|
||||
StateKey: stateKey,
|
||||
Sender: evt.Sender,
|
||||
Type: evt.Type,
|
||||
Type: maubot.EventType(evt.Type),
|
||||
Timestamp: evt.Timestamp,
|
||||
ID: evt.ID,
|
||||
RoomID: evt.RoomID,
|
||||
Content: evt.Content,
|
||||
Content: roundtripContent(evt.Content),
|
||||
Redacts: evt.Redacts,
|
||||
Unsigned: interfaces.Unsigned{
|
||||
PrevContent: evt.Unsigned.PrevContent,
|
||||
Unsigned: maubot.Unsigned{
|
||||
PrevContent: roundtripContent(evt.Unsigned.PrevContent),
|
||||
PrevSender: evt.Unsigned.PrevSender,
|
||||
ReplacesState: evt.Unsigned.ReplacesState,
|
||||
Age: evt.Unsigned.Age,
|
||||
},
|
||||
}
|
||||
RemoveReplyFallback(mbEvent)
|
||||
return mbEvent
|
||||
}
|
||||
|
||||
func (evt *Event) Reply(text string) (string, error) {
|
||||
return evt.SendEvent(
|
||||
return evt.SendRawEvent(maubot.EventMessage,
|
||||
SetReply(
|
||||
RenderMarkdown(text),
|
||||
evt.Event))
|
||||
}
|
||||
|
||||
func (evt *Event) SendMessage(text string) (string, error) {
|
||||
return evt.SendEvent(RenderMarkdown(text))
|
||||
func (evt *Event) ReplyContent(content maubot.Content) (string, error) {
|
||||
return evt.SendRawEvent(maubot.EventMessage, SetReply(content, evt.Event))
|
||||
}
|
||||
|
||||
func (evt *Event) SendEvent(content map[string]interface{}) (string, error) {
|
||||
resp, err := evt.Client.SendMessageEvent(evt.RoomID, "m.room.message", content)
|
||||
func (evt *Event) SendMessage(text string) (string, error) {
|
||||
return evt.SendRawEvent(maubot.EventMessage, RenderMarkdown(text))
|
||||
}
|
||||
|
||||
func (evt *Event) SendContent(content maubot.Content) (string, error) {
|
||||
return evt.SendRawEvent(maubot.EventMessage, content)
|
||||
}
|
||||
|
||||
func (evt *Event) SendRawEvent(evtType maubot.EventType, content interface{}) (string, error) {
|
||||
resp, err := evt.Client.SendMessageEvent(evt.RoomID, string(evtType), content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -19,9 +19,10 @@ package matrix
|
|||
import (
|
||||
"strings"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
"maubot.xyz"
|
||||
)
|
||||
|
||||
func RenderMarkdown(text string) map[string]interface{} {
|
||||
func RenderMarkdown(text string) maubot.Content {
|
||||
parser := blackfriday.New(
|
||||
blackfriday.WithExtensions(blackfriday.NoIntraEmphasis |
|
||||
blackfriday.Tables |
|
||||
|
@ -43,10 +44,10 @@ func RenderMarkdown(text string) map[string]interface{} {
|
|||
renderer.RenderFooter(&buf, ast)
|
||||
htmlBody := buf.String()
|
||||
|
||||
return map[string]interface{}{
|
||||
"formatted_body": htmlBody,
|
||||
"format": "org.matrix.custom.html",
|
||||
"msgtype": "m.text",
|
||||
"body": HTMLToText(htmlBody),
|
||||
return maubot.Content{
|
||||
FormattedBody: htmlBody,
|
||||
Format: maubot.FormatHTML,
|
||||
MsgType: maubot.MsgText,
|
||||
Body: HTMLToText(htmlBody),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,14 +17,15 @@
|
|||
package matrix
|
||||
|
||||
import (
|
||||
"maubot.xyz"
|
||||
"maubot.xyz/database"
|
||||
"maubot.xyz/interfaces"
|
||||
"maunium.net/go/gomatrix"
|
||||
log "maunium.net/go/maulogger"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
*gomatrix.Client
|
||||
syncer *MaubotSyncer
|
||||
|
||||
DB *database.MatrixClient
|
||||
}
|
||||
|
@ -40,9 +41,10 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
|
|||
DB: db,
|
||||
}
|
||||
|
||||
client.Syncer = NewMaubotSyncer(client, client.Store)
|
||||
client.syncer = NewMaubotSyncer(client, client.Store)
|
||||
client.Client.Syncer = client.syncer
|
||||
|
||||
client.AddEventHandler(gomatrix.StateMember, client.onJoin)
|
||||
client.AddEventHandler(maubot.StateMember, client.onJoin)
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
@ -50,19 +52,19 @@ func NewClient(db *database.MatrixClient) (*Client, error) {
|
|||
func (client *Client) ParseEvent(evt *gomatrix.Event) *Event {
|
||||
return &Event{
|
||||
Client: client,
|
||||
Event: evt,
|
||||
Event: evt,
|
||||
}
|
||||
}
|
||||
|
||||
func (client *Client) AddEventHandler(evt string, handler interfaces.EventHandler) {
|
||||
client.Syncer.(*MaubotSyncer).OnEventType(evt, handler)
|
||||
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
|
||||
client.syncer.OnEventType(evt, handler)
|
||||
}
|
||||
|
||||
func (client *Client) onJoin(evt *interfaces.Event) bool {
|
||||
func (client *Client) onJoin(evt *maubot.Event) bool {
|
||||
if !client.DB.AutoJoinRooms || evt.StateKey != client.DB.UserID {
|
||||
return true
|
||||
}
|
||||
if membership, _ := evt.Content["membership"].(string); membership == "invite" {
|
||||
if evt.Content.Membership == "invite" {
|
||||
client.JoinRoom(evt.RoomID)
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -17,11 +17,13 @@
|
|||
package matrix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"fmt"
|
||||
"maunium.net/go/gomatrix"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"maubot.xyz"
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
var HTMLReplyFallbackRegex = regexp.MustCompile(`^<mx-reply>[\s\S]+?</mx-reply>`)
|
||||
|
@ -42,13 +44,13 @@ func TrimReplyFallbackText(text string) string {
|
|||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func RemoveReplyFallback(evt *gomatrix.Event) {
|
||||
if format, ok := evt.Content["format"].(string); ok && format == "org.matrix.custom.html" {
|
||||
htmlBody, _ := evt.Content["formatted_body"].(string)
|
||||
evt.Content["formatted_body"] = TrimReplyFallbackHTML(htmlBody)
|
||||
func RemoveReplyFallback(evt *maubot.Event) {
|
||||
if len(evt.Content.RelatesTo.InReplyTo.EventID) > 0 {
|
||||
if evt.Content.Format == maubot.FormatHTML {
|
||||
evt.Content.FormattedBody = TrimReplyFallbackHTML(evt.Content.FormattedBody)
|
||||
}
|
||||
evt.Content.Body = TrimReplyFallbackText(evt.Content.Body)
|
||||
}
|
||||
plainBody, _ := evt.Content["body"].(string)
|
||||
evt.Content["body"] = TrimReplyFallbackText(plainBody)
|
||||
}
|
||||
|
||||
const ReplyFormat = `<mx-reply><blockquote>
|
||||
|
@ -86,22 +88,18 @@ func ReplyFallbackText(evt *gomatrix.Event) string {
|
|||
return fallbackText.String()
|
||||
}
|
||||
|
||||
func SetReply(content map[string]interface{}, inReplyTo *gomatrix.Event) map[string]interface{} {
|
||||
content["m.relates_to"] = map[string]interface{}{
|
||||
"m.in_reply_to": map[string]interface{}{
|
||||
"event_id": inReplyTo.ID,
|
||||
"room_id": inReplyTo.RoomID,
|
||||
},
|
||||
func SetReply(content maubot.Content, inReplyTo *gomatrix.Event) maubot.Content {
|
||||
content.RelatesTo.InReplyTo.EventID = inReplyTo.ID
|
||||
content.RelatesTo.InReplyTo.RoomID = inReplyTo.RoomID
|
||||
|
||||
if content.MsgType == maubot.MsgText || content.MsgType == maubot.MsgNotice {
|
||||
if len(content.FormattedBody) == 0 || content.Format != maubot.FormatHTML {
|
||||
content.FormattedBody = html.EscapeString(content.Body)
|
||||
content.Format = maubot.FormatHTML
|
||||
}
|
||||
content.FormattedBody = ReplyFallbackHTML(inReplyTo) + content.FormattedBody
|
||||
content.Body = ReplyFallbackText(inReplyTo) + content.Body
|
||||
}
|
||||
|
||||
body, _ := content["body"].(string)
|
||||
content["body"] = ReplyFallbackText(inReplyTo) + body
|
||||
|
||||
htmlBody, ok := content["formatted_body"].(string)
|
||||
if !ok {
|
||||
htmlBody = html.EscapeString(body)
|
||||
content["format"] = "org.matrix.custom.html"
|
||||
}
|
||||
content["formatted_body"] = ReplyFallbackHTML(inReplyTo) + htmlBody
|
||||
return content
|
||||
}
|
||||
|
|
|
@ -6,14 +6,14 @@ import (
|
|||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"maubot.xyz/interfaces"
|
||||
"maubot.xyz"
|
||||
"maunium.net/go/gomatrix"
|
||||
)
|
||||
|
||||
type MaubotSyncer struct {
|
||||
Client *Client
|
||||
Store gomatrix.Storer
|
||||
listeners map[string][]interfaces.EventHandler
|
||||
listeners map[maubot.EventType][]maubot.EventHandler
|
||||
}
|
||||
|
||||
// NewDefaultSyncer returns an instantiated DefaultSyncer
|
||||
|
@ -21,7 +21,7 @@ func NewMaubotSyncer(client *Client, store gomatrix.Storer) *MaubotSyncer {
|
|||
return &MaubotSyncer{
|
||||
Client: client,
|
||||
Store: store,
|
||||
listeners: make(map[string][]interfaces.EventHandler),
|
||||
listeners: make(map[maubot.EventType][]maubot.EventHandler),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -73,10 +73,10 @@ func (s *MaubotSyncer) ProcessResponse(res *gomatrix.RespSync, since string) (er
|
|||
|
||||
// OnEventType allows callers to be notified when there are new events for the given event type.
|
||||
// There are no duplicate checks.
|
||||
func (s *MaubotSyncer) OnEventType(eventType string, callback interfaces.EventHandler) {
|
||||
func (s *MaubotSyncer) OnEventType(eventType maubot.EventType, callback maubot.EventHandler) {
|
||||
_, exists := s.listeners[eventType]
|
||||
if !exists {
|
||||
s.listeners[eventType] = []interfaces.EventHandler{}
|
||||
s.listeners[eventType] = []maubot.EventHandler{}
|
||||
}
|
||||
s.listeners[eventType] = append(s.listeners[eventType], callback)
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ func (s *MaubotSyncer) getOrCreateRoom(roomID string) *gomatrix.Room {
|
|||
|
||||
func (s *MaubotSyncer) notifyListeners(mxEvent *gomatrix.Event) {
|
||||
event := s.Client.ParseEvent(mxEvent)
|
||||
listeners, exists := s.listeners[event.Type]
|
||||
listeners, exists := s.listeners[maubot.EventType(event.Type)]
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue