Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate through slack channels #38

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
15 changes: 12 additions & 3 deletions app/lib/slack/loads_slack_channels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ class LoadsSlackChannels
].freeze

def call(types:)
response = ClientWrapper.client.conversations_list(types: approved_types(types), limit: 1000)

(response&.channels || []).reject { |ch| ch.is_archived }
response = ClientWrapper.client.conversations_list(types: approved_types(types), limit: 1000, exclude_archived: true)
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