2018-06-13 22:41:05 +00:00
|
|
|
// maubot - A plugin-based Matrix bot system written in Go.
|
|
|
|
// Copyright (C) 2018 Tulir Asokan
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package matrix
|
|
|
|
|
|
|
|
import (
|
2018-06-14 08:11:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"maubot.xyz"
|
2018-06-13 22:41:05 +00:00
|
|
|
"maunium.net/go/gomatrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Event struct {
|
2018-06-15 15:34:38 +00:00
|
|
|
*maubot.Event
|
2018-06-13 22:41:05 +00:00
|
|
|
Client *Client
|
|
|
|
}
|
|
|
|
|
2018-06-18 22:25:47 +00:00
|
|
|
func roundtripContent(rawContent map[string]interface{}) (content *maubot.Content) {
|
|
|
|
content = &maubot.Content{}
|
2018-06-14 08:11:24 +00:00
|
|
|
if len(rawContent) == 0 {
|
|
|
|
content.Raw = rawContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, _ := json.Marshal(&rawContent)
|
|
|
|
json.Unmarshal(data, &content)
|
|
|
|
content.Raw = rawContent
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:34:38 +00:00
|
|
|
func (client *Client) ParseEvent(mxEvent *gomatrix.Event) *Event {
|
2018-06-14 07:29:37 +00:00
|
|
|
var stateKey string
|
2018-06-15 15:34:38 +00:00
|
|
|
if mxEvent.StateKey != nil {
|
|
|
|
stateKey = *mxEvent.StateKey
|
|
|
|
}
|
|
|
|
event := &Event{
|
|
|
|
Client: client,
|
2018-06-14 07:29:37 +00:00
|
|
|
}
|
2018-06-14 08:11:24 +00:00
|
|
|
mbEvent := &maubot.Event{
|
2018-06-15 15:34:38 +00:00
|
|
|
EventFuncs: event,
|
2018-06-14 07:29:37 +00:00
|
|
|
StateKey: stateKey,
|
2018-06-15 15:34:38 +00:00
|
|
|
Sender: mxEvent.Sender,
|
|
|
|
Type: maubot.EventType(mxEvent.Type),
|
|
|
|
Timestamp: mxEvent.Timestamp,
|
|
|
|
ID: mxEvent.ID,
|
|
|
|
RoomID: mxEvent.RoomID,
|
2018-06-18 22:25:47 +00:00
|
|
|
Content: *roundtripContent(mxEvent.Content),
|
2018-06-15 15:34:38 +00:00
|
|
|
Redacts: mxEvent.Redacts,
|
2018-06-14 08:11:24 +00:00
|
|
|
Unsigned: maubot.Unsigned{
|
2018-06-15 15:34:38 +00:00
|
|
|
PrevContent: roundtripContent(mxEvent.Unsigned.PrevContent),
|
|
|
|
PrevSender: mxEvent.Unsigned.PrevSender,
|
|
|
|
ReplacesState: mxEvent.Unsigned.ReplacesState,
|
|
|
|
Age: mxEvent.Unsigned.Age,
|
2018-06-14 07:29:37 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-14 08:11:24 +00:00
|
|
|
RemoveReplyFallback(mbEvent)
|
2018-06-15 15:34:38 +00:00
|
|
|
event.Event = mbEvent
|
|
|
|
return event
|
2018-06-14 07:29:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 10:36:53 +00:00
|
|
|
func (evt *Event) MarkRead() error {
|
|
|
|
return evt.Client.MarkRead(evt.RoomID, evt.ID)
|
|
|
|
}
|
|
|
|
|
2018-06-13 22:41:05 +00:00
|
|
|
func (evt *Event) Reply(text string) (string, error) {
|
2018-06-15 15:34:38 +00:00
|
|
|
return evt.ReplyContent(RenderMarkdown(text))
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:11:24 +00:00
|
|
|
func (evt *Event) ReplyContent(content maubot.Content) (string, error) {
|
2018-06-15 15:34:38 +00:00
|
|
|
return evt.SendContent(SetReply(content, evt))
|
2018-06-14 08:11:24 +00:00
|
|
|
}
|
|
|
|
|
2018-06-13 22:41:05 +00:00
|
|
|
func (evt *Event) SendMessage(text string) (string, error) {
|
2018-06-15 15:34:38 +00:00
|
|
|
return evt.SendContent(RenderMarkdown(text))
|
2018-06-14 08:11:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (evt *Event) SendContent(content maubot.Content) (string, error) {
|
|
|
|
return evt.SendRawEvent(maubot.EventMessage, content)
|
2018-06-13 22:41:05 +00:00
|
|
|
}
|
|
|
|
|
2018-06-14 08:11:24 +00:00
|
|
|
func (evt *Event) SendRawEvent(evtType maubot.EventType, content interface{}) (string, error) {
|
|
|
|
resp, err := evt.Client.SendMessageEvent(evt.RoomID, string(evtType), content)
|
2018-06-13 22:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return resp.EventID, nil
|
|
|
|
}
|