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() {
log.Debugln("Starting Matrix syncer")
for _, client := range bot.Clients {
go func() {
client.SetAvatarURL(client.DB.AvatarURL)
client.SetDisplayName(client.DB.DisplayName)
}()
if client.DB.Sync {
client.Sync()
}

View File

@ -57,9 +57,11 @@ type EventHandler func(*Event) bool
type MatrixClient interface {
AddEventHandler(EventType, EventHandler)
GetEvent(string, string) *Event
}
type EventFuncs interface {
MarkRead() error
Reply(string) (string, error)
ReplyContent(Content) (string, error)
SendMessage(string) (string, error)
@ -70,22 +72,22 @@ type EventFuncs interface {
type Event struct {
EventFuncs
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
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
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)
Content Content `json:"content"`
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.
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
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
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)
Content Content `json:"content"`
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.
}
type Unsigned struct {
PrevContent Content `json:"prev_content,omitempty"`
PrevSender string `json:"prev_sender,omitempty"`
ReplacesState string `json:"replaces_state,omitempty"`
Age int64 `json:"age"`
PrevContent Content `json:"prev_content,omitempty"`
PrevSender string `json:"prev_sender,omitempty"`
ReplacesState string `json:"replaces_state,omitempty"`
Age int64 `json:"age"`
}
type Content struct {
@ -93,32 +95,32 @@ type Content struct {
MsgType MessageType `json:"msgtype"`
Body string `json:"body"`
Format string `json:"format"`
FormattedBody string `json:"formatted_body"`
Format string `json:"format,omitempty"`
FormattedBody string `json:"formatted_body,omitempty"`
Info FileInfo `json:"info"`
URL string `json:"url"`
Info FileInfo `json:"info,omitempty"`
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 {
MimeType string `json:"mimetype"`
ThumbnailInfo *FileInfo `json:"thumbnail_info"`
ThumbnailURL string `json:"thumbnail_url"`
Height int `json:"h"`
Width int `json:"w"`
Size int `json:"size"`
MimeType string `json:"mimetype,omitempty"`
ThumbnailInfo *FileInfo `json:"thumbnail_info,omitempty"`
ThumbnailURL string `json:"thumbnail_url,omitempty"`
Height int `json:"h,omitempty"`
Width int `json:"w,omitempty"`
Size int `json:"size,omitempty"`
}
type RelatesTo struct {
InReplyTo InReplyTo `json:"m.in_reply_to"`
InReplyTo InReplyTo `json:"m.in_reply_to,omitempty"`
}
type InReplyTo struct {
EventID string `json:"event_id"`
// 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
}
func (evt *Event) MarkRead() error {
return evt.Client.MarkRead(evt.RoomID, evt.ID)
}
func (evt *Event) Reply(text string) (string, error) {
return evt.SendRawEvent(maubot.EventMessage,
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) {
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 {

View File

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