diff --git a/app/matrix.go b/app/matrix.go index 8a9e631..8951be5 100644 --- a/app/matrix.go +++ b/app/matrix.go @@ -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() } diff --git a/matrix.go b/matrix.go index 2d9a12b..e97801f 100644 --- a/matrix.go +++ b/matrix.go @@ -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"` } diff --git a/matrix/event.go b/matrix/event.go index f418bee..1fa39af 100644 --- a/matrix/event.go +++ b/matrix/event.go @@ -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( diff --git a/matrix/matrix.go b/matrix/matrix.go index a276d37..a8b796c 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -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 { diff --git a/matrix/replyutil.go b/matrix/replyutil.go index 6009dfc..34ce0d2 100644 --- a/matrix/replyutil.go +++ b/matrix/replyutil.go @@ -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) {