Skip to content

Commit d93052c

Browse files
authored
More efficient name-to-id translation (#555)
1 parent af8828f commit d93052c

File tree

9 files changed

+96
-20
lines changed

9 files changed

+96
-20
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ jobs:
4141
parallel: true
4242
github-token: ${{ secrets.GITHUB_TOKEN }}
4343
flag-name: run-${{ matrix.entry.ruby }}${{ matrix.entry.concurrency && '-'}}${{ matrix.entry.concurrency }}
44+
allow-empty: ${{ matrix.entry.ignore || false }}
4445

4546
finish:
4647
name: coveralls

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [#549](https://github.com/slack-ruby/slack-ruby-client/pull/549): Add group ID formatting support for message mentions - [@n0h0](https://github.com/n0h0).
44
* [#553](https://github.com/slack-ruby/slack-ruby-client/pull/553): Use `secure_compare` during signature verification - [@hieuk09](https://github.com/hieuk09).
5+
* [#555](https://github.com/slack-ruby/slack-ruby-client/pull/555): Make page size in resolving IDs configurable - [@eizengan](https://github.com/eizengan).
56
* Your contribution here.
67

78
### 2.5.2 (2025/02/19)

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ client.files_upload_v2(
179179
# required options
180180
filename: 'results.pdf', # this is used for the file title, unless a :title option is provided
181181
content: File.read('/users/me/results.pdf'), # the string contents of the file
182-
182+
183183
# optional options
184184
channels: ['C000000', 'C000001'], # channel IDs to share the file in (:channel_id, :channel, or :channels are all supported)
185185
initial_comment: 'Sharing the Q1 results :tada:', # the message that is included with the file share thread
@@ -281,20 +281,22 @@ client = Slack::Web::Client.new(user_agent: 'Slack Ruby Client/1.0')
281281

282282
The following settings are supported.
283283

284-
setting | description
285-
--------------------|-------------------------------------------------------------------------------------------------
286-
token | Slack API token.
287-
user_agent | User-agent, defaults to _Slack Ruby Client/version_.
288-
proxy | Optional HTTP proxy.
289-
ca_path | Optional SSL certificates path.
290-
ca_file | Optional SSL certificates file.
291-
endpoint | Slack endpoint, default is _https://slack.com/api_.
292-
logger | Optional `Logger` instance that logs HTTP requests.
293-
timeout | Optional open/read timeout in seconds.
294-
open_timeout | Optional connection open timeout in seconds.
295-
default_page_size | Optional page size for paginated requests, default is _100_.
296-
default_max_retries | Optional number of retries for paginated requests, default is _100_.
297-
adapter | Optional HTTP adapter to use, defaults to `Faraday.default_adapter`.
284+
setting | description
285+
-----------------------------|-------------------------------------------------------------------------------------------------
286+
token | Slack API token.
287+
user_agent | User-agent, defaults to _Slack Ruby Client/version_.
288+
proxy | Optional HTTP proxy.
289+
ca_path | Optional SSL certificates path.
290+
ca_file | Optional SSL certificates file.
291+
endpoint | Slack endpoint, default is _https://slack.com/api_.
292+
logger | Optional `Logger` instance that logs HTTP requests.
293+
timeout | Optional open/read timeout in seconds.
294+
open_timeout | Optional connection open timeout in seconds.
295+
default_page_size | Optional page size for paginated requests, default is _100_.
296+
conversations_id_page_size | Optional page size for conversations_list requests made when calculating conversation id from a conversation name, default is _nil_, which will use the default_page_size.
297+
users_id_page_size | Optional page size for users_list requests made when calculating user id from a user name, default is _nil_, which will use the default_page_size.
298+
default_max_retries | Optional number of retries for paginated requests, default is _100_.
299+
adapter | Optional HTTP adapter to use, defaults to `Faraday.default_adapter`.
298300

299301
You can also pass request options, including `timeout` and `open_timeout` into individual calls.
300302

lib/slack/web/api/mixins/conversations.id.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ module Conversations
1212
#
1313
# @option options [channel] :channel
1414
# Channel to get ID for, prefixed with #.
15+
# @option options [integer] :id_limit
16+
# The page size used for conversations_list calls required to find the channel's ID
1517
def conversations_id(options = {})
1618
name = options[:channel]
19+
limit = options.fetch(:id_limit, Slack::Web.config.conversations_id_page_size)
20+
1721
raise ArgumentError, 'Required arguments :channel missing' if name.nil?
1822

19-
id_for :channel, name, '#', :conversations_list, :channels, 'channel_not_found'
23+
id_for(
24+
key: :channel,
25+
name: name,
26+
prefix: '#',
27+
enum_method: :conversations_list,
28+
list_method: :channels,
29+
options: { limit: limit }.compact
30+
)
2031
end
2132
end
2233
end

lib/slack/web/api/mixins/ids.id.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ module Mixins
66
module Ids
77
private
88

9-
def id_for(key, name, prefix, enum_method, list_method, not_found_error)
9+
def id_for(key:, name:, prefix:, enum_method:, list_method:, options: {})
1010
return { 'ok' => true, key.to_s => { 'id' => name } } unless name[0] == prefix
1111

12-
public_send enum_method do |list|
12+
public_send(enum_method, **options) do |list|
1313
list.public_send(list_method).each do |li|
1414
return Slack::Messages::Message.new('ok' => true, key.to_s => { 'id' => li.id }) if li.name == name[1..-1]
1515
end
1616
end
1717

18-
raise Slack::Web::Api::Errors::SlackError, not_found_error
18+
raise Slack::Web::Api::Errors::SlackError, "#{key}_not_found"
1919
end
2020
end
2121
end

lib/slack/web/api/mixins/users.id.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ module Users
1212
#
1313
# @option options [user] :user
1414
# User to get ID for, prefixed with '@'.
15+
# @option options [integer] :id_limit
16+
# The page size used for users_list calls required to find the user's ID
1517
def users_id(options = {})
1618
name = options[:user]
19+
limit = options.fetch(:id_limit, Slack::Web.config.users_id_page_size)
20+
1721
raise ArgumentError, 'Required arguments :user missing' if name.nil?
1822

19-
id_for :user, name, '@', :users_list, :members, 'user_not_found'
23+
id_for(
24+
key: :user,
25+
name: name,
26+
prefix: '@',
27+
enum_method: :users_list,
28+
list_method: :members,
29+
options: { limit: limit }.compact
30+
)
2031
end
2132
end
2233
end

lib/slack/web/config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Config
1515
timeout
1616
open_timeout
1717
default_page_size
18+
conversations_id_page_size
19+
users_id_page_size
1820
default_max_retries
1921
adapter
2022
].freeze
@@ -32,6 +34,8 @@ def reset
3234
self.timeout = nil
3335
self.open_timeout = nil
3436
self.default_page_size = 100
37+
self.conversations_id_page_size = nil
38+
self.users_id_page_size = nil
3539
self.default_max_retries = 100
3640
self.adapter = ::Faraday.default_adapter
3741
end

spec/slack/web/api/mixins/conversations_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,39 @@
3131
end
3232

3333
it 'translates a channel that starts with a #' do
34+
expect(conversations).to receive(:conversations_list)
3435
expect(conversations.conversations_id(channel: '#general')).to(
3536
eq('ok' => true, 'channel' => { 'id' => 'CDEADBEEF' })
3637
)
3738
end
3839

40+
it 'forwards a provided limit to the underlying conversations_list calls' do
41+
expect(conversations).to receive(:conversations_list).with(limit: 1234)
42+
conversations.conversations_id(channel: '#general', id_limit: 1234)
43+
end
44+
3945
it 'fails with an exception' do
4046
expect { conversations.conversations_id(channel: '#invalid') }.to(
4147
raise_error(Slack::Web::Api::Errors::SlackError, 'channel_not_found')
4248
)
4349
end
50+
51+
context 'when a non-default conversations_id page size has been configured' do
52+
before { Slack::Web.config.conversations_id_page_size = 500 }
53+
54+
after { Slack::Web.config.reset }
55+
56+
it 'translates a channel that starts with a #' do
57+
expect(conversations).to receive(:conversations_list).with(limit: 500)
58+
expect(conversations.conversations_id(channel: '#general')).to(
59+
eq('ok' => true, 'channel' => { 'id' => 'CDEADBEEF' })
60+
)
61+
end
62+
63+
it 'forwards a provided limit to the underlying conversations_list calls' do
64+
expect(conversations).to receive(:conversations_list).with(limit: 1234)
65+
conversations.conversations_id(channel: '#general', id_limit: 1234)
66+
end
67+
end
4468
end
4569
end

spec/slack/web/api/mixins/users_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,36 @@
3030
end
3131

3232
it 'translates a user that starts with a @' do
33+
expect(users).to receive(:users_list)
3334
expect(users.users_id(user: '@aws')).to eq('ok' => true, 'user' => { 'id' => 'UDEADBEEF' })
3435
end
3536

37+
it 'forwards a provided limit to the underlying users_list calls' do
38+
expect(users).to receive(:users_list).with(limit: 1234)
39+
users.users_id(user: '@aws', id_limit: 1234)
40+
end
41+
3642
it 'fails with an exception' do
3743
expect { users.users_id(user: '@foo') }.to(
3844
raise_error(Slack::Web::Api::Errors::SlackError, 'user_not_found')
3945
)
4046
end
47+
48+
context 'when a non-default conversations_id page size has been configured' do
49+
before { Slack::Web.config.users_id_page_size = 500 }
50+
51+
after { Slack::Web.config.reset }
52+
53+
it 'translates a user that starts with a @' do
54+
expect(users).to receive(:users_list).with(limit: 500)
55+
expect(users.users_id(user: '@aws')).to eq('ok' => true, 'user' => { 'id' => 'UDEADBEEF' })
56+
end
57+
58+
it 'forwards a provided limit to the underlying users_list calls' do
59+
expect(users).to receive(:users_list).with(limit: 1234)
60+
users.users_id(user: '@aws', id_limit: 1234)
61+
end
62+
end
4163
end
4264

4365
if defined?(Picky)

0 commit comments

Comments
 (0)