Fixes #12: Added users list pagination

This commit is contained in:
RealPhi 2018-06-26 13:11:39 +04:30 committed by Pedro Pombeiro
parent e12b50ba2d
commit 59f7019b29
1 changed files with 14 additions and 2 deletions

View File

@ -13,6 +13,7 @@ const MemCache = require('mem-cache')
const { WebClient } = require('@slack/client') const { WebClient } = require('@slack/client')
const token = process.env.SLACK_USER_TOKEN || '' const token = process.env.SLACK_USER_TOKEN || ''
const slackWebClient = new WebClient(token)
const cacheMemcachedKey = 'slack-profile-cache-json' const cacheMemcachedKey = 'slack-profile-cache-json'
const slackIdCacheKeyPrefix = 'Slack-' const slackIdCacheKeyPrefix = 'Slack-'
const slackUsernameCacheKeyPrefix = 'SlackUN-' const slackUsernameCacheKeyPrefix = 'SlackUN-'
@ -113,8 +114,8 @@ async function internalBuild (robot, cache) {
try { try {
const slackWeb = new WebClient(token) // We need to use a different token because users.profile API is not available to bot users 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 usersList = await getUsersList()
const activeUsersList = usersList.members.filter(u => !u.deleted && !u.is_bot && u.id !== 'USLACKBOT') const activeUsersList = usersList.filter(u => !u.deleted && !u.is_bot && u.id !== 'USLACKBOT')
let gitHubFieldId = null let gitHubFieldId = null
let pubKeyFieldId = null let pubKeyFieldId = null
@ -251,3 +252,14 @@ function timeout (ms) {
async function sleep (timeoutMs) { async function sleep (timeoutMs) {
await timeout(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)
}