Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions app/services/slack/loads_slack_channels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@ 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

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
30 changes: 25 additions & 5 deletions spec/lib/slack/loads_slack_channels_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
])
}

Expand All @@ -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])
}

Expand Down