FIX: loading user.list longer than 1000 (#24)
This commit is contained in:
parent
790f1bb8b5
commit
1a08d64785
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue