diff --git a/app/services/slack/loads_slack_channels.rb b/app/services/slack/loads_slack_channels.rb index fb4f674..8506c1b 100644 --- a/app/services/slack/loads_slack_channels.rb +++ b/app/services/slack/loads_slack_channels.rb @@ -11,10 +11,10 @@ class LoadsSlackChannels def call(types:) response = retry_when_rate_limited do - ClientWrapper.client.conversations_list(types: approved_types(types), limit: 1000) + ClientWrapper.client.conversations_list(types: approved_types(types), limit: 1000, exclude_archived: true) end - (response&.channels || []).reject { |ch| ch.is_archived } + get_paged_channels(cursor: response&.response_metadata&.next_cursor, channels: response&.channels || [], types: types) end private @@ -22,5 +22,15 @@ def call(types:) def approved_types(types) types.split(",").select { |t| SLACK_CHANNEL_TYPES.include?(t) }.join(",") end + + def get_paged_channels(cursor:, channels:, types:) + while cursor.present? + response = ClientWrapper.client.conversations_list(cursor: cursor, types: approved_types(types), limit: 1000, exclude_archived: true) + channels.append(response.channels) + cursor = response.response_metadata&.next_cursor + end + + channels.flatten + end end end diff --git a/spec/lib/slack/loads_slack_channels_spec.rb b/spec/lib/slack/loads_slack_channels_spec.rb index 806238b..7075b50 100644 --- a/spec/lib/slack/loads_slack_channels_spec.rb +++ b/spec/lib/slack/loads_slack_channels_spec.rb @@ -18,17 +18,16 @@ is_mpim: true, user: Slack::Messages::Message.new(id: "USER_ID") ) - archived_slack_public_channel = Slack::Messages::Message.new( + Slack::Messages::Message.new( id: "PUBLIC_CHANNEL_ID_2", is_channel: true, is_archived: true ) - expect(@slack_client).to receive(:conversations_list).with(types: "public_channel,mpim", limit: 1000) { + expect(@slack_client).to receive(:conversations_list).with(types: "public_channel,mpim", limit: 1000, exclude_archived: true) { Slack::Messages::Message.new(ok: true, channels: [ slack_public_channel, - slack_mpim_channel, - archived_slack_public_channel + slack_mpim_channel ]) } @@ -37,13 +36,34 @@ expect(channels).to eq([slack_public_channel, slack_mpim_channel]) end + it "loads all active channels when requiring pagination" do + slack_public_channels = (0..1010).map do |ch| + Slack::Messages::Message.new( + id: "PUBLIC_CHANNEL_ID_#{ch}", + is_channel: true + ) + end + + response_metadata = Slack::Messages::Message.new(next_cursor: "cursor") + expect(@slack_client).to receive(:conversations_list).with(types: "public_channel", limit: 1000, exclude_archived: true) { + Slack::Messages::Message.new(ok: true, response_metadata: response_metadata, channels: slack_public_channels[0..999]) + } + expect(@slack_client).to receive(:conversations_list).with(types: "public_channel", limit: 1000, exclude_archived: true, cursor: response_metadata.next_cursor) { + Slack::Messages::Message.new(ok: true, channels: slack_public_channels[1000..]) + } + + channels = subject.call(types: "public_channel") + + expect(channels).to eq(slack_public_channels) + end + it "ignores unrecognized channel types" do slack_public_channel = Slack::Messages::Message.new( id: "PUBLIC_CHANNEL_ID_1", is_channel: true ) - expect(@slack_client).to receive(:conversations_list).with(types: "public_channel", limit: 1000) { + expect(@slack_client).to receive(:conversations_list).with(types: "public_channel", limit: 1000, exclude_archived: true) { Slack::Messages::Message.new(ok: true, channels: [slack_public_channel]) }