Implement rate limited user presence resolver

Reference #167
This commit is contained in:
erroneousboat 2018-12-25 14:00:39 +01:00
parent 8475d8d63d
commit 9b9c8e993c
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) { func RegisterEventHandlers(ctx *context.AppContext) {
// Keyboard events
eventHandler(ctx) eventHandler(ctx)
// RTM incoming events
messageHandler(ctx) messageHandler(ctx)
// User presence
go actionSetPresenceAll(ctx)
} }
// eventHandler will handle events created by the user // eventHandler will handle events created by the user
@ -457,6 +464,24 @@ func actionSetPresence(ctx *context.AppContext, channelID string, presence strin
termui.Render(ctx.View.Channels) 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) { func actionScrollUpChat(ctx *context.AppContext) {
ctx.View.Chat.ScrollUp() ctx.View.Chat.ScrollUp()
termui.Render(ctx.View.Chat) termui.Render(ctx.View.Chat)

View File

@ -190,6 +190,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 { if chn.IsIM {
// Check if user is deleted, we do this by checking the user id, // Check if user is deleted, we do this by checking the user id,
// and see if we have the user in the UserCache // and see if we have the user in the UserCache
@ -200,6 +202,7 @@ func (s *SlackService) GetChannels() ([]components.ChannelItem, error) {
chanItem.Name = name chanItem.Name = name
chanItem.Type = components.ChannelTypeIM chanItem.Type = components.ChannelTypeIM
chanItem.Presence = "away"
if chn.UnreadCount > 0 { if chn.UnreadCount > 0 {
chanItem.Notification = true chanItem.Notification = true
@ -209,19 +212,6 @@ func (s *SlackService) GetChannels() ([]components.ChannelItem, error) {
channelItem: chanItem, channelItem: chanItem,
slackChannel: chn, 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)
} }
} }