diff options
Diffstat (limited to 'connector/mattermost')
-rw-r--r-- | connector/mattermost/mattermost.go | 67 |
1 files changed, 50 insertions, 17 deletions
diff --git a/connector/mattermost/mattermost.go b/connector/mattermost/mattermost.go index 51ad636..0b863fb 100644 --- a/connector/mattermost/mattermost.go +++ b/connector/mattermost/mattermost.go @@ -27,6 +27,9 @@ type Mattermost struct { username string teams map[string]bool + initial_members int // How many room members (maximum) to load when first joining a channel + initial_backlog int // How many previous messages (maximum) to load when first joining a channel + conn *matterclient.MMClient handlerStopChan chan bool @@ -66,6 +69,16 @@ func (mm *Mattermost) Configure(c Configuration) error { return err } + mm.initial_members, err = c.GetInt("initial_members", 1000) + if err != nil { + return err + } + + mm.initial_backlog, err = c.GetInt("initial_backlog", 1000) + if err != nil { + return err + } + teams, err := c.GetString("teams") if err != nil { return err @@ -385,7 +398,7 @@ func (mm *Mattermost) initSyncChannel(ch *model.Channel) { // Update member list // TODO (when this will be slow, i.e. hundreds of members): do only a diff - members, resp := mm.conn.Client.GetChannelMembers(ch.Id, 0, 1000, "") + members, resp := mm.conn.Client.GetChannelMembers(ch.Id, 0, mm.initial_members, "") if resp.Error == nil { for _, mem := range *members { if mem.UserId == mm.conn.User.Id { @@ -405,26 +418,45 @@ func (mm *Mattermost) initSyncChannel(ch *model.Channel) { } // Read backlog - var backlog *model.PostList - var resp *model.Response last_seen_post := mm.handler.CacheGet(fmt.Sprintf("last_seen_%s", ch.Id)) if last_seen_post != "" { - backlog, resp = mm.conn.Client.GetPostsAfter(ch.Id, last_seen_post, 0, 1000, "") - // TODO: if there are more than 1000, loop around - } else { - 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) + const NUM_PER_PAGE = 100 + page := 0 + backlogs := []*model.PostList{} + for { + backlog, resp := mm.conn.Client.GetPostsAfter(ch.Id, last_seen_post, page, NUM_PER_PAGE, "") + if resp.Error == nil { + backlogs = append(backlogs, backlog) + if len(backlog.Order) == NUM_PER_PAGE { + page += 1 + } else { + break + } + } else { + break + } + } + for i := 0; i < len(backlogs); i++ { + mm.processBacklog(ch, backlogs[i]) } } else { - log.Warnf("Could not get channel backlog: %s", resp.Error) + backlog, resp := mm.conn.Client.GetPostsForChannel(ch.Id, 0, mm.initial_backlog, "") + if resp.Error == nil { + mm.processBacklog(ch, backlog) + } else { + log.Warnf("Could not get channel backlog: %s", resp.Error) + } + } +} + +func (mm *Mattermost) processBacklog(ch *model.Channel, backlog *model.PostList) { + 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) } } @@ -569,6 +601,7 @@ func (mm *Mattermost) handlePost(channel_name string, post *model.Post, only_mes } mm.handler.Event(msg_ev) + mm.handler.CachePut(fmt.Sprintf("last_seen_%s", post.ChannelId), post.Id) } else { interested, roomId := mm.reverseRoomId(post.ChannelId) if !interested { |