From 1a08d647857b356bdd39810bde76bc90de978337 Mon Sep 17 00:00:00 2001 From: Mateusz Bugowski <23140767+mbugowski@users.noreply.github.com> Date: Fri, 14 Dec 2018 02:03:49 +0100 Subject: [PATCH] FIX: loading user.list longer than 1000 (#24) --- .../provider/slack/slack_transcript.rb | 20 +++++++++++++------ .../slack/slack_command_controller_spec.rb | 2 +- .../provider/slack/slack_transcript_spec.rb | 6 +++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/discourse_chat/provider/slack/slack_transcript.rb b/lib/discourse_chat/provider/slack/slack_transcript.rb index fe6d1e4..a89c922 100644 --- a/lib/discourse_chat/provider/slack/slack_transcript.rb +++ b/lib/discourse_chat/provider/slack/slack_transcript.rb @@ -192,13 +192,21 @@ module DiscourseChat::Provider::SlackProvider http = Net::HTTP.new("slack.com", 443) http.use_ssl = true + cursor = nil req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list')) - req.set_form_data(token: SiteSetting.chat_integration_slack_access_token) - response = http.request(req) - return false unless response.kind_of? Net::HTTPSuccess - json = JSON.parse(response.body) - return false unless json['ok'] - @users = json['members'] + + @users = [] + loop do + break if cursor == "" + req.set_form_data(token: SiteSetting.chat_integration_slack_access_token, limit: 200, cursor: cursor) + response = http.request(req) + return false unless response.kind_of? Net::HTTPSuccess + json = JSON.parse(response.body) + return false unless json['ok'] + cursor = json['response_metadata']['next_cursor'] + @users << json['members'] + end + @users&.flatten! end def load_chat_history(count: 500) diff --git a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb index 9b74604..25d9523 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_command_controller_spec.rb @@ -195,7 +195,7 @@ describe 'Slack Command Controller', type: :request do context "with valid slack responses" do before do - stub_request(:post, "https://slack.com/api/users.list").to_return(body: '{"ok":true,"members":[{"id":"U5Z773QLS","name":"david","profile":{"icon_24":"https://example.com/avatar"}}]}') + stub_request(:post, "https://slack.com/api/users.list").to_return(body: '{"ok":true,"members":[{"id":"U5Z773QLS","name":"david","profile":{"icon_24":"https://example.com/avatar"}}],"response_metadata":{"next_cursor":""}}') stub_request(:post, "https://slack.com/api/conversations.history").to_return(body: { ok: true, messages: messages_fixture }.to_json) end diff --git a/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb index a764c70..731c648 100644 --- a/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb +++ b/spec/lib/discourse_chat/provider/slack/slack_transcript_spec.rb @@ -75,8 +75,8 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do describe 'loading users' do it 'loads users correctly' do stub_request(:post, "https://slack.com/api/users.list") - .with(body: { token: "abcde" }) - .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }] }.to_json) + .with(body: { token: "abcde", "cursor": nil, "limit": "200" }) + .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }], response_metadata: { next_cursor: "" } }.to_json) expect(transcript.load_user_data).to be_truthy end @@ -99,7 +99,7 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do context 'with loaded users' do before do stub_request(:post, "https://slack.com/api/users.list") - .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }] }.to_json) + .to_return(status: 200, body: { ok: true, members: [{ id: "U5Z773QLS", name: "awesomeguy", profile: { image_24: "https://example.com/avatar" } }], response_metadata: { next_cursor: "" } }.to_json) transcript.load_user_data end