diff options
author | Alex Auvolat <alex@adnab.me> | 2020-02-21 19:28:00 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-02-21 19:28:00 +0100 |
commit | b0644c3a17dfd1a55f4ed5f4f91fcc84e74359ed (patch) | |
tree | 9a0e45bde1ad4b30e116ab58c6db1241f5e1c9df /connector | |
parent | e1b838d30493effbcd8a23fe43e2b131c745b722 (diff) | |
download | easybridge-b0644c3a17dfd1a55f4ed5f4f91fcc84e74359ed.tar.gz easybridge-b0644c3a17dfd1a55f4ed5f4f91fcc84e74359ed.zip |
Basic backlogging
Diffstat (limited to 'connector')
-rw-r--r-- | connector/connector.go | 11 | ||||
-rw-r--r-- | connector/mattermost/mattermost.go | 31 |
2 files changed, 38 insertions, 4 deletions
diff --git a/connector/connector.go b/connector/connector.go index 2235318..30dfea4 100644 --- a/connector/connector.go +++ b/connector/connector.go @@ -98,6 +98,11 @@ const ( type Event struct { Type EventType + // If non-empty, the event Id is used to deduplicate events in a channel + // This is usefull for backends that provide a backlog of channel messages + // when (re-)joining a room + Id string + // UserID of the user that sent the event // If this is a direct message event, this event can only have been authored // by the user we are talking to (and not by ourself) @@ -120,12 +125,18 @@ type Event struct { type UserInfo struct { DisplayName string + + // If non-empty, the Filename of the avatar object will be used by Easybridge + // to deduplicate the update events and prevent needless reuploads. + // Example strategy that works for the mattermost backend: use the update timestamp as fictious file name Avatar MediaObject } type RoomInfo struct { Name string Topic string + + // Same deduplication comment as for UserInfo.Avatar Picture MediaObject } diff --git a/connector/mattermost/mattermost.go b/connector/mattermost/mattermost.go index 6282831..117286b 100644 --- a/connector/mattermost/mattermost.go +++ b/connector/mattermost/mattermost.go @@ -354,6 +354,22 @@ func (mm *Mattermost) handleConnected() { } else { log.Warnf("Could not get channel members: %s", resp.Error) } + + // Read backlog + // TODO: remember what was the last seen post in this channel + backlog, resp := mm.conn.Client.GetPostsForChannel(ch.Id, 0, 1000, "") + if resp.Error == nil { + for i := 0; i < len(backlog.Order); i++ { + post_id := backlog.Order[len(backlog.Order)-i-1] + post := backlog.Posts[post_id] + post_time := time.Unix(post.CreateAt/1000, 0) + post.Message = fmt.Sprintf("[%s] %s", + post_time.Format("2006-01-02 15:04:05 MST"), post.Message) + mm.handlePost(ch.Name, post, true) + } + } else { + log.Warnf("Could not get channel backlog: %s", resp.Error) + } } } @@ -425,6 +441,10 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error { return err } + return mm.handlePost(channel_name, &post, false) +} + +func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_messages bool) error { // Skip self messages if post.UserId == mm.conn.User.Id { return nil @@ -440,6 +460,7 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error { // Build message event msg_ev := &Event{ + Id: post.Id, Author: userId, Text: post.Message, Type: EVENT_MESSAGE, @@ -489,10 +510,12 @@ func (mm *Mattermost) handlePosted(msg *model.WebSocketEvent) error { mm.ensureJoined(user, roomId) if post.Type == "system_header_change" { - new_header := post.Props["new_header"].(string) - mm.handler.RoomInfoUpdated(roomId, userId, &RoomInfo{ - Topic: new_header, - }) + if !only_messages { + new_header := post.Props["new_header"].(string) + mm.handler.RoomInfoUpdated(roomId, userId, &RoomInfo{ + Topic: new_header, + }) + } } else if post.Type == "" || post.Type == "me" { msg_ev.Room = roomId mm.handler.Event(msg_ev) |