Fixes #12: Added users list pagination
This commit is contained in:
parent
e12b50ba2d
commit
59f7019b29
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue