Skip to content

Commit

Permalink
fix: Added cursor pagination to slackapp conversations query (#4442)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Christopher Kolstad committed Aug 8, 2023
1 parent 19119bd commit edcbf2a
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/lib/addons/slack-app.ts
Expand Up @@ -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
Expand Down

0 comments on commit edcbf2a

Please sign in to comment.