From b0644c3a17dfd1a55f4ed5f4f91fcc84e74359ed Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 21 Feb 2020 19:28:00 +0100 Subject: Basic backlogging --- connector/mattermost/mattermost.go | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'connector/mattermost') 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) -- cgit v1.2.3