diff --git a/lib/slack-profile-cache.js b/lib/slack-profile-cache.js index 37acf6e..883bb40 100644 --- a/lib/slack-profile-cache.js +++ b/lib/slack-profile-cache.js @@ -13,6 +13,7 @@ const MemCache = require('mem-cache') const { WebClient } = require('@slack/client') const token = process.env.SLACK_USER_TOKEN || '' +const slackWebClient = new WebClient(token) const cacheMemcachedKey = 'slack-profile-cache-json' const slackIdCacheKeyPrefix = 'Slack-' const slackUsernameCacheKeyPrefix = 'SlackUN-' @@ -113,8 +114,8 @@ async function internalBuild (robot, cache) { try { const slackWeb = new WebClient(token) // We need to use a different token because users.profile API is not available to bot users - const usersList = await slackWeb.users.list() // TODO: This call should be paginated to avoid hitting limits (memory, API): https://api.slack.com/docs/pagination#cursors - const activeUsersList = usersList.members.filter(u => !u.deleted && !u.is_bot && u.id !== 'USLACKBOT') + const usersList = await getUsersList() + const activeUsersList = usersList.filter(u => !u.deleted && !u.is_bot && u.id !== 'USLACKBOT') let gitHubFieldId = null let pubKeyFieldId = null @@ -251,3 +252,14 @@ function timeout (ms) { async function sleep (timeoutMs) { await timeout(timeoutMs) } + +async function getUsersList (users = [], cursor = '') { + const res = await slackWebClient.users.list({limit: 200, cursor: cursor}) + users = users.concat(res.members) + + if (!res.response_metadata.next_cursor) { + return users + } + + return getUsersList(users, res.response_metadata.next_cursor) +}