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 users #51

Open
wants to merge 1 commit 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: 13 additions & 2 deletions app/lib/slack/loads_slack_channel_members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,20 @@ def call(channel:)
private

def eligible_user_ids
response = ClientWrapper.client.users_list
response = ClientWrapper.client.users_list(limit: 200)
members = get_paged_members(cursor: response&.response_metadata&.next_cursor, members: response&.members || [])

(response&.members || []).reject { |u| u.is_bot }.map(&:id)
(members || []).reject { |u| u.is_bot }.map(&:id)
end

def get_paged_members(cursor:, members:)
while cursor.present?
response = ClientWrapper.client.users_list(cursor: cursor, limit: 200)
members.append(response.members)
cursor = response.response_metadata&.next_cursor
end

members.flatten
end
end
end
27 changes: 27 additions & 0 deletions spec/lib/slack/loads_slack_channel_members_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@
expect(members).to eq(slack_members)
end

it "loads members found in a channel when pagination is required" do
all_slack_users = (0..205).map do |id|
Slack::Messages::Message.new(id: "USER_ID_#{id}")
end

slack_members = [
"USER_ID_1",
"USER_ID_3",
"USER_ID_205"
]

response_metadata = Slack::Messages::Message.new(next_cursor: "cursor")
expect(@slack_client).to receive(:users_list).with(limit: 200) {
Slack::Messages::Message.new(ok: true, members: all_slack_users[0..199], response_metadata: response_metadata)
}
expect(@slack_client).to receive(:users_list).with(cursor: response_metadata.next_cursor, limit: 200) {
Slack::Messages::Message.new(ok: true, members: all_slack_users[200..])
}
expect(@slack_client).to receive(:conversations_members).with(channel: "CHANNEL_ID", limit: 1000) {
Slack::Messages::Message.new(ok: true, members: slack_members)
}

members = subject.call(channel: "CHANNEL_ID")

expect(members).to eq(slack_members)
end

it "excludes bots from the returned users" do
slack_user = Slack::Messages::Message.new(id: "USER_ID", is_bot: false)
slack_app = Slack::Messages::Message.new(id: "APP_ID", is_bot: true)
Expand Down