1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: Added cursor pagination to slackapp conversations query (#4442)

We've been struggling with not getting hold of all channels. Reading the
documentation I found the support for next_cursor in the
response_metadata, as long as the response has a next_cursor, there are
responses left, so this PR adds a loop for querying until the next
cursor or the channels list is undefined or `''`. This should solve the
issue Wayfair had as well.
This commit is contained in:
Christopher Kolstad 2023-08-08 12:54:32 +02:00 committed by GitHub
parent 19119bd1f0
commit edcbf2acbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,8 +87,39 @@ export default class SlackAppAddon extends Addon {
const slackConversationsList =
await this.slackClient.conversations.list({
types: 'public_channel,private_channel',
exclude_archived: true,
limit: 200,
});
this.slackChannels = slackConversationsList.channels || [];
let nextCursor =
slackConversationsList.response_metadata?.next_cursor;
while (nextCursor !== undefined && nextCursor !== '') {
this.logger.debug('Fetching next page of channels');
const moreChannels =
await this.slackClient.conversations.list({
cursor: nextCursor,
types: 'public_channel,private_channel',
exclude_archived: true,
limit: 200,
});
const channels = moreChannels.channels;
if (channels === undefined) {
this.logger.debug(
'Channels list was empty, breaking pagination',
);
nextCursor = undefined;
break;
}
nextCursor = moreChannels.response_metadata?.next_cursor;
this.logger.debug(
`This page had ${channels.length} channels`,
);
channels.forEach((channel) =>
this.slackChannels?.push(channel),
);
}
this.logger.debug(
`Fetched ${
this.slackChannels.length