diff --git a/handlers/event.go b/handlers/event.go index 8a047c9..49fd69d 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -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 @@ -457,6 +464,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) diff --git a/service/slack.go b/service/slack.go index d2c4c7b..63dd223 100644 --- a/service/slack.go +++ b/service/slack.go @@ -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 { // Check if user is deleted, we do this by checking the user id, // 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.Type = components.ChannelTypeIM + chanItem.Presence = "away" if chn.UnreadCount > 0 { chanItem.Notification = true @@ -209,19 +212,6 @@ func (s *SlackService) GetChannels() ([]components.ChannelItem, error) { 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) } }