Small fixes

This commit is contained in:
Tulir Asokan 2018-06-14 13:36:53 +03:00
parent 3a27831112
commit feaf75b327
5 changed files with 53 additions and 29 deletions

View file

@ -40,6 +40,10 @@ func (bot *Bot) initClients() {
func (bot *Bot) startClients() { func (bot *Bot) startClients() {
log.Debugln("Starting Matrix syncer") log.Debugln("Starting Matrix syncer")
for _, client := range bot.Clients { for _, client := range bot.Clients {
go func() {
client.SetAvatarURL(client.DB.AvatarURL)
client.SetDisplayName(client.DB.DisplayName)
}()
if client.DB.Sync { if client.DB.Sync {
client.Sync() client.Sync()
} }

View file

@ -57,9 +57,11 @@ type EventHandler func(*Event) bool
type MatrixClient interface { type MatrixClient interface {
AddEventHandler(EventType, EventHandler) AddEventHandler(EventType, EventHandler)
GetEvent(string, string) *Event
} }
type EventFuncs interface { type EventFuncs interface {
MarkRead() error
Reply(string) (string, error) Reply(string) (string, error)
ReplyContent(Content) (string, error) ReplyContent(Content) (string, error)
SendMessage(string) (string, error) SendMessage(string) (string, error)
@ -70,22 +72,22 @@ type EventFuncs interface {
type Event struct { type Event struct {
EventFuncs EventFuncs
StateKey string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events. StateKey string `json:"state_key,omitempty"` // The state key for the event. Only present on State Events.
Sender string `json:"sender"` // The user ID of the sender of the event Sender string `json:"sender"` // The user ID of the sender of the event
Type EventType `json:"type"` // The event type Type EventType `json:"type"` // The event type
Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server Timestamp int64 `json:"origin_server_ts"` // The unix timestamp when this message was sent by the origin server
ID string `json:"event_id"` // The unique ID of this event ID string `json:"event_id"` // The unique ID of this event
RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence) RoomID string `json:"room_id"` // The room the event was sent to. May be nil (e.g. for presence)
Content Content `json:"content"` Content Content `json:"content"`
Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event Redacts string `json:"redacts,omitempty"` // The event ID that was redacted if a m.room.redaction event
Unsigned Unsigned `json:"unsigned,omitempty"` // Unsigned content set by own homeserver. Unsigned Unsigned `json:"unsigned,omitempty"` // Unsigned content set by own homeserver.
} }
type Unsigned struct { type Unsigned struct {
PrevContent Content `json:"prev_content,omitempty"` PrevContent Content `json:"prev_content,omitempty"`
PrevSender string `json:"prev_sender,omitempty"` PrevSender string `json:"prev_sender,omitempty"`
ReplacesState string `json:"replaces_state,omitempty"` ReplacesState string `json:"replaces_state,omitempty"`
Age int64 `json:"age"` Age int64 `json:"age"`
} }
type Content struct { type Content struct {
@ -93,32 +95,32 @@ type Content struct {
MsgType MessageType `json:"msgtype"` MsgType MessageType `json:"msgtype"`
Body string `json:"body"` Body string `json:"body"`
Format string `json:"format"` Format string `json:"format,omitempty"`
FormattedBody string `json:"formatted_body"` FormattedBody string `json:"formatted_body,omitempty"`
Info FileInfo `json:"info"` Info FileInfo `json:"info,omitempty"`
URL string `json:"url"` URL string `json:"url,omitempty"`
Membership string `json:"membership"` Membership string `json:"membership,omitempty"`
RelatesTo RelatesTo `json:"m.relates_to"` RelatesTo RelatesTo `json:"m.relates_to,omitempty"`
} }
type FileInfo struct { type FileInfo struct {
MimeType string `json:"mimetype"` MimeType string `json:"mimetype,omitempty"`
ThumbnailInfo *FileInfo `json:"thumbnail_info"` ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"`
ThumbnailURL string `json:"thumbnail_url"` ThumbnailURL string `json:"thumbnail_url,omitempty"`
Height int `json:"h"` Height int `json:"h,omitempty"`
Width int `json:"w"` Width int `json:"w,omitempty"`
Size int `json:"size"` Size int `json:"size,omitempty"`
} }
type RelatesTo struct { type RelatesTo struct {
InReplyTo InReplyTo `json:"m.in_reply_to"` InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"`
} }
type InReplyTo struct { type InReplyTo struct {
EventID string `json:"event_id"` EventID string `json:"event_id"`
// Not required, just for future-proofing // Not required, just for future-proofing
RoomID string `json:"room_id"` RoomID string `json:"room_id,omitempty"`
} }

View file

@ -65,6 +65,10 @@ func (evt *Event) Interface() *maubot.Event {
return mbEvent return mbEvent
} }
func (evt *Event) MarkRead() error {
return evt.Client.MarkRead(evt.RoomID, evt.ID)
}
func (evt *Event) Reply(text string) (string, error) { func (evt *Event) Reply(text string) (string, error) {
return evt.SendRawEvent(maubot.EventMessage, return evt.SendRawEvent(maubot.EventMessage,
SetReply( SetReply(

View file

@ -57,7 +57,21 @@ func (client *Client) ParseEvent(evt *gomatrix.Event) *Event {
} }
func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) { func (client *Client) AddEventHandler(evt maubot.EventType, handler maubot.EventHandler) {
client.syncer.OnEventType(evt, handler) client.syncer.OnEventType(evt, func(evt *maubot.Event) bool {
if evt.Sender == client.UserID {
return false
}
return handler(evt)
})
}
func (client *Client) GetEvent(roomID, eventID string) *maubot.Event {
evt, err := client.Client.GetEvent(roomID, eventID)
if err != nil {
log.Warnf("Failed to get event %s @ %s: %v", eventID, roomID, err)
return nil
}
return client.ParseEvent(evt).Interface()
} }
func (client *Client) onJoin(evt *maubot.Event) bool { func (client *Client) onJoin(evt *maubot.Event) bool {

View file

@ -41,7 +41,7 @@ func TrimReplyFallbackText(text string) string {
for len(lines) > 0 && strings.HasPrefix(lines[0], "> ") { for len(lines) > 0 && strings.HasPrefix(lines[0], "> ") {
lines = lines[1:] lines = lines[1:]
} }
return strings.Join(lines, "\n") return strings.TrimSpace(strings.Join(lines, "\n"))
} }
func RemoveReplyFallback(evt *maubot.Event) { func RemoveReplyFallback(evt *maubot.Event) {