FIX: loading user.list longer than 1000 (#24)

This commit is contained in:
Mateusz Bugowski 2018-12-14 02:03:49 +01:00 committed by David Taylor
parent 790f1bb8b5
commit 1a08d64785
3 changed files with 18 additions and 10 deletions

View File

@ -192,13 +192,21 @@ module DiscourseChat::Provider::SlackProvider
http = Net::HTTP.new("slack.com", 443) http = Net::HTTP.new("slack.com", 443)
http.use_ssl = true http.use_ssl = true
cursor = nil
req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list')) 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) @users = []
return false unless response.kind_of? Net::HTTPSuccess loop do
json = JSON.parse(response.body) break if cursor == ""
return false unless json['ok'] req.set_form_data(token: SiteSetting.chat_integration_slack_access_token, limit: 200, cursor: cursor)
@users = json['members'] 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 end
def load_chat_history(count: 500) def load_chat_history(count: 500)

View File

@ -195,7 +195,7 @@ describe 'Slack Command Controller', type: :request do
context "with valid slack responses" do context "with valid slack responses" do
before 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) stub_request(:post, "https://slack.com/api/conversations.history").to_return(body: { ok: true, messages: messages_fixture }.to_json)
end end

View File

@ -75,8 +75,8 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do
describe 'loading users' do describe 'loading users' do
it 'loads users correctly' do it 'loads users correctly' do
stub_request(:post, "https://slack.com/api/users.list") stub_request(:post, "https://slack.com/api/users.list")
.with(body: { token: "abcde" }) .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" } }] }.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)
expect(transcript.load_user_data).to be_truthy expect(transcript.load_user_data).to be_truthy
end end
@ -99,7 +99,7 @@ RSpec.describe DiscourseChat::Provider::SlackProvider::SlackTranscript do
context 'with loaded users' do context 'with loaded users' do
before do before do
stub_request(:post, "https://slack.com/api/users.list") 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 transcript.load_user_data
end end