Backlog several pages of messages if not initial backlog
This commit is contained in:
parent
e6fa715b81
commit
495a6303dc
1 changed files with 50 additions and 17 deletions
|
@ -27,6 +27,9 @@ type Mattermost struct {
|
||||||
username string
|
username string
|
||||||
teams map[string]bool
|
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
|
conn *matterclient.MMClient
|
||||||
handlerStopChan chan bool
|
handlerStopChan chan bool
|
||||||
|
|
||||||
|
@ -66,6 +69,16 @@ func (mm *Mattermost) Configure(c Configuration) error {
|
||||||
return err
|
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")
|
teams, err := c.GetString("teams")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -385,7 +398,7 @@ func (mm *Mattermost) initSyncChannel(ch *model.Channel) {
|
||||||
|
|
||||||
// Update member list
|
// Update member list
|
||||||
// TODO (when this will be slow, i.e. hundreds of members): do only a diff
|
// 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 {
|
if resp.Error == nil {
|
||||||
for _, mem := range *members {
|
for _, mem := range *members {
|
||||||
if mem.UserId == mm.conn.User.Id {
|
if mem.UserId == mm.conn.User.Id {
|
||||||
|
@ -405,26 +418,45 @@ func (mm *Mattermost) initSyncChannel(ch *model.Channel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read backlog
|
// Read backlog
|
||||||
var backlog *model.PostList
|
|
||||||
var resp *model.Response
|
|
||||||
last_seen_post := mm.handler.CacheGet(fmt.Sprintf("last_seen_%s", ch.Id))
|
last_seen_post := mm.handler.CacheGet(fmt.Sprintf("last_seen_%s", ch.Id))
|
||||||
if last_seen_post != "" {
|
if last_seen_post != "" {
|
||||||
backlog, resp = mm.conn.Client.GetPostsAfter(ch.Id, last_seen_post, 0, 1000, "")
|
const NUM_PER_PAGE = 100
|
||||||
// TODO: if there are more than 1000, loop around
|
page := 0
|
||||||
} else {
|
backlogs := []*model.PostList{}
|
||||||
backlog, resp = mm.conn.Client.GetPostsForChannel(ch.Id, 0, 1000, "")
|
for {
|
||||||
}
|
backlog, resp := mm.conn.Client.GetPostsAfter(ch.Id, last_seen_post, page, NUM_PER_PAGE, "")
|
||||||
if resp.Error == nil {
|
if resp.Error == nil {
|
||||||
for i := 0; i < len(backlog.Order); i++ {
|
backlogs = append(backlogs, backlog)
|
||||||
post_id := backlog.Order[len(backlog.Order)-i-1]
|
if len(backlog.Order) == NUM_PER_PAGE {
|
||||||
post := backlog.Posts[post_id]
|
page += 1
|
||||||
post_time := time.Unix(post.CreateAt/1000, 0)
|
} else {
|
||||||
post.Message = fmt.Sprintf("[%s] %s",
|
break
|
||||||
post_time.Format("2006-01-02 15:04:05 MST"), post.Message)
|
}
|
||||||
mm.handlePost(ch.Name, post, true)
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := 0; i < len(backlogs); i++ {
|
||||||
|
mm.processBacklog(ch, backlogs[i])
|
||||||
}
|
}
|
||||||
} else {
|
} 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.Event(msg_ev)
|
||||||
|
mm.handler.CachePut(fmt.Sprintf("last_seen_%s", post.ChannelId), post.Id)
|
||||||
} else {
|
} else {
|
||||||
interested, roomId := mm.reverseRoomId(post.ChannelId)
|
interested, roomId := mm.reverseRoomId(post.ChannelId)
|
||||||
if !interested {
|
if !interested {
|
||||||
|
|
Loading…
Reference in a new issue