From edcbf2acbf7539f152fb49f3f00b046f82088f7a Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Tue, 8 Aug 2023 12:54:32 +0200 Subject: [PATCH] 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. --- src/lib/addons/slack-app.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/lib/addons/slack-app.ts b/src/lib/addons/slack-app.ts index 641682eca8..1e7198241d 100644 --- a/src/lib/addons/slack-app.ts +++ b/src/lib/addons/slack-app.ts @@ -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