More functions for clients

This commit is contained in:
Tulir Asokan 2018-09-21 00:35:24 +03:00
parent f7fd012218
commit 6868b08fa9
3 changed files with 110 additions and 14 deletions

View File

@ -17,6 +17,8 @@
package maubot
import (
"io"
"maunium.net/go/gomatrix"
)
@ -30,11 +32,81 @@ const (
StopCommandPropagation CommandHandlerResult = iota
)
type MatrixClient interface {
type GomatrixClient interface {
//d <method> = disabled
//r <method> = replaced
BanUser(roomID string, req *gomatrix.ReqBanUser) (resp *gomatrix.RespBanUser, err error)
//d BuildBaseURL(urlPath ...string) string
//d BuildURL(urlPath ...string) string
//d BuildURLWithQuery(urlPath []string, urlQuery map[string]string) string
//d ClearCredentials()
//d CreateFilter(filter json.RawMessage) (resp *gomatrix.RespCreateFilter, err error)
CreateRoom(req *gomatrix.ReqCreateRoom) (resp *gomatrix.RespCreateRoom, err error)
Download(mxcURL string) (io.ReadCloser, error)
DownloadBytes(mxcURL string) ([]byte, error)
ForgetRoom(roomID string) (resp *gomatrix.RespForgetRoom, err error)
GetAvatarURL() (url string, err error)
GetDisplayName(mxid string) (resp *gomatrix.RespUserDisplayName, err error)
//r GetEvent(roomID, eventID string) (resp *gomatrix.Event, err error)
GetOwnDisplayName() (resp *gomatrix.RespUserDisplayName, err error)
InviteUser(roomID string, req *gomatrix.ReqInviteUser) (resp *gomatrix.RespInviteUser, err error)
InviteUserByThirdParty(roomID string, req *gomatrix.ReqInvite3PID) (resp *gomatrix.RespInviteUser, err error)
//r JoinRoom(roomIDorAlias, serverName string, content interface{}) (resp *gomatrix.RespJoinRoom, err error)
JoinedMembers(roomID string) (resp *gomatrix.RespJoinedMembers, err error)
JoinedRooms() (resp *gomatrix.RespJoinedRooms, err error)
KickUser(roomID string, req *gomatrix.ReqKickUser) (resp *gomatrix.RespKickUser, err error)
LeaveRoom(roomID string) (resp *gomatrix.RespLeaveRoom, err error)
//d Login(req *gomatrix.ReqLogin) (resp *gomatrix.RespLogin, err error)
//d Logout() (resp *gomatrix.RespLogout, err error)
MakeRequest(method string, httpURL string, reqBody interface{}, resBody interface{}) ([]byte, error)
MarkRead(roomID, eventID string) (err error)
Messages(roomID, from, to string, dir rune, limit int) (resp *gomatrix.RespMessages, err error)
RedactEvent(roomID, eventID string, req *gomatrix.ReqRedact) (resp *gomatrix.RespSendEvent, err error)
//d Register(req *gomatrix.ReqRegister) (*gomatrix.RespRegister, *gomatrix.RespUserInteractive, error)
//d RegisterDummy(req *gomatrix.ReqRegister) (*gomatrix.RespRegister, error)
//d RegisterGuest(req *gomatrix.ReqRegister) (*gomatrix.RespRegister, *gomatrix.RespUserInteractive, error)
SendImage(roomID, body, url string) (*gomatrix.RespSendEvent, error)
//SendMassagedMessageEvent(roomID string, eventType gomatrix.EventType, contentJSON interface{}, ts int64) (resp *gomatrix.RespSendEvent, err error)
//SendMassagedStateEvent(roomID string, eventType gomatrix.EventType, stateKey string, contentJSON interface{}, ts int64) (resp *gomatrix.RespSendEvent, err error)
//r SendMessageEvent(roomID string, eventType gomatrix.EventType, contentJSON interface{}) (resp *gomatrix.RespSendEvent, err error)
SendNotice(roomID, text string) (*gomatrix.RespSendEvent, error)
SendStateEvent(roomID string, eventType gomatrix.EventType, stateKey string, contentJSON interface{}) (resp *gomatrix.RespSendEvent, err error)
SendText(roomID, text string) (*gomatrix.RespSendEvent, error)
SendVideo(roomID, body, url string) (*gomatrix.RespSendEvent, error)
SetAvatarURL(url string) (err error)
SetCredentials(userID, accessToken string)
SetDisplayName(displayName string) (err error)
SetPresence(status string) (err error)
StateEvent(roomID string, eventType gomatrix.EventType, stateKey string, outContent interface{}) (err error)
//d StopSync()
//d Sync() error
//d SyncRequest(timeout int, since, filterID string, fullState bool, setPresence string) (resp *gomatrix.RespSync, err error)
TurnServer() (resp *gomatrix.RespTurnServer, err error)
UnbanUser(roomID string, req *gomatrix.ReqUnbanUser) (resp *gomatrix.RespUnbanUser, err error)
Upload(content io.Reader, contentType string, contentLength int64) (*gomatrix.RespMediaUpload, error)
UploadBytes(data []byte, contentType string) (*gomatrix.RespMediaUpload, error)
UploadLink(link string) (*gomatrix.RespMediaUpload, error)
UserTyping(roomID string, typing bool, timeout int64) (resp *gomatrix.RespTyping, err error)
Versions() (resp *gomatrix.RespVersions, err error)
}
type MBMatrixClient interface {
AddEventHandler(gomatrix.EventType, EventHandler)
AddCommandHandler(string, CommandHandler)
SetCommandSpec(*CommandSpec)
GetEvent(string, string) *Event
GetEvent(roomID, eventID string) *Event
JoinRoom(roomIDOrAlias string) (resp *gomatrix.RespJoinRoom, err error)
SendMessage(roomID, text string) (eventID string, err error)
SendMessagef(roomID, text string, args ...interface{}) (eventID string, err error)
SendContent(roomID string, content gomatrix.Content) (eventID string, err error)
SendMessageEvent(roomID string, content interface{}) (eventID string, err error)
}
type MatrixClient interface {
GomatrixClient
MBMatrixClient
}
type EventFuncs interface {
@ -42,8 +114,9 @@ type EventFuncs interface {
Reply(string) (string, error)
ReplyContent(gomatrix.Content) (string, error)
SendMessage(string) (string, error)
SendMessagef(string, ...interface{}) (string, error)
SendContent(gomatrix.Content) (string, error)
SendRawEvent(gomatrix.EventType, interface{}) (string, error)
SendMessageEvent(evtType gomatrix.EventType, content interface{}) (eventID string, err error)
}
type Event struct {

View File

@ -55,17 +55,17 @@ func (evt *EventFuncsImpl) ReplyContent(content gomatrix.Content) (string, error
}
func (evt *EventFuncsImpl) SendMessage(text string) (string, error) {
return evt.SendContent(format.RenderMarkdown(text))
return evt.Client.SendMessage(evt.RoomID, text)
}
func (evt *EventFuncsImpl) SendMessagef(text string, args ...interface{}) (string, error) {
return evt.Client.SendMessagef(evt.RoomID, text, args...)
}
func (evt *EventFuncsImpl) SendContent(content gomatrix.Content) (string, error) {
return evt.SendRawEvent(gomatrix.EventMessage, content)
return evt.Client.SendContent(evt.RoomID, content)
}
func (evt *EventFuncsImpl) SendRawEvent(evtType gomatrix.EventType, content interface{}) (string, error) {
resp, err := evt.Client.SendMessageEvent(evt.RoomID, evtType, content)
if err != nil {
return "", err
}
return resp.EventID, nil
}
func (evt *EventFuncsImpl) SendMessageEvent(evtType gomatrix.EventType, content interface{}) (eventID string, err error) {
return evt.Client.SendMessageEvent(evt.RoomID, evtType, content)
}

View File

@ -17,9 +17,12 @@
package matrix
import (
"fmt"
"maubot.xyz"
"maubot.xyz/database"
"maunium.net/go/gomatrix"
"maunium.net/go/gomatrix/format"
log "maunium.net/go/maulogger"
)
@ -135,8 +138,28 @@ func (client *Client) onJoin(evt *maubot.Event) maubot.EventHandlerResult {
return maubot.Continue
}
func (client *Client) JoinRoom(roomID string) {
client.Client.JoinRoom(roomID, "", nil)
func (client *Client) JoinRoom(roomID string) (resp *gomatrix.RespJoinRoom, err error) {
return client.Client.JoinRoom(roomID, "", nil)
}
func (client *Client) SendMessage(roomID, text string) (string, error) {
return client.SendContent(roomID, format.RenderMarkdown(text))
}
func (client *Client) SendMessagef(roomID, text string, args ...interface{}) (string, error) {
return client.SendContent(roomID, format.RenderMarkdown(fmt.Sprintf(text, args...)))
}
func (client *Client) SendContent(roomID string, content gomatrix.Content) (string, error) {
return client.SendMessageEvent(roomID, gomatrix.EventMessage, content)
}
func (client *Client) SendMessageEvent(roomID string, evtType gomatrix.EventType, content interface{}) (string, error) {
resp, err := client.Client.SendMessageEvent(roomID, evtType, content)
if err != nil {
return "", err
}
return resp.EventID, nil
}
func (client *Client) Sync() {