Compare commits

...

1 Commits

Author SHA1 Message Date
erroneousboat
825e88b407 Implement rate limited user presence resolver
Reference #167
2018-12-25 14:00:39 +01:00
2 changed files with 28 additions and 13 deletions

View File

@ -50,8 +50,15 @@ var actionMap = map[string]func(*context.AppContext){
}
func RegisterEventHandlers(ctx *context.AppContext) {
// Keyboard events
eventHandler(ctx)
// RTM incoming events
messageHandler(ctx)
// User presence
go actionSetPresenceAll(ctx)
}
// eventHandler will handle events created by the user
@ -444,6 +451,24 @@ func actionSetPresence(ctx *context.AppContext, channelID string, presence strin
termui.Render(ctx.View.Channels)
}
// actionPresenceAll will set the presence of the user list. Because the
// requests to the endpoint are rate limited we implement a timeout here.
func actionSetPresenceAll(ctx *context.AppContext) {
for _, chn := range ctx.Service.Conversations {
if chn.IsIM {
presence, err := ctx.Service.GetUserPresence(chn.User)
if err != nil {
presence = "away"
}
ctx.View.Channels.SetPresence(chn.ID, presence)
termui.Render(ctx.View.Channels)
time.Sleep(1200 * time.Millisecond)
}
}
}
func actionScrollUpChat(ctx *context.AppContext) {
ctx.View.Chat.ScrollUp()
termui.Render(ctx.View.Chat)

View File

@ -173,6 +173,8 @@ func (s *SlackService) GetChannels() ([]components.ChannelItem, error) {
}
}
// NOTE: user presence is set in the event handler by the function
// `actionSetPresenceAll`, that is why we set the presence to away
if chn.IsIM {
// Check if user is deleted, we do this by checking the user id,
// and see if we have the user in the UserCache
@ -183,24 +185,12 @@ func (s *SlackService) GetChannels() ([]components.ChannelItem, error) {
chanItem.Name = name
chanItem.Type = components.ChannelTypeIM
chanItem.Presence = "away"
buckets[3][chn.User] = &tempChan{
channelItem: chanItem,
slackChannel: chn,
}
wg.Add(1)
go func(user string, buckets map[int]map[string]*tempChan) {
defer wg.Done()
presence, err := s.GetUserPresence(user)
if err != nil {
buckets[3][user].channelItem.Presence = "away"
return
}
buckets[3][user].channelItem.Presence = presence
}(chn.User, buckets)
}
}