Skip to content

Commit 087b640

Browse files
authored
Merge pull request #540 from dblock/support_channel_id_s
Added support for channel and channel_id, do not require channel.
2 parents 308a3aa + bf2f795 commit 087b640

12 files changed

+1229
-35
lines changed

.rubocop_todo.yml

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-02-08 14:50:37 UTC using RuboCop version 1.26.1.
3+
# on 2025-02-08 15:52:13 UTC using RuboCop version 1.26.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -47,27 +47,39 @@ Lint/RedundantCopDisableDirective:
4747
# Offense count: 13
4848
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
4949
Metrics/AbcSize:
50-
Max: 42
50+
Max: 58
5151

52-
# Offense count: 5
52+
# Offense count: 6
5353
# Configuration parameters: IgnoredMethods.
5454
Metrics/CyclomaticComplexity:
55-
Max: 9
55+
Max: 11
5656

5757
# Offense count: 15
5858
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
5959
Metrics/MethodLength:
60-
Max: 26
60+
Max: 35
6161

6262
# Offense count: 1
6363
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
6464
Metrics/ParameterLists:
6565
Max: 6
6666

67-
# Offense count: 2
67+
# Offense count: 3
6868
# Configuration parameters: IgnoredMethods.
6969
Metrics/PerceivedComplexity:
70-
Max: 9
70+
Max: 12
71+
72+
# Offense count: 1
73+
# Configuration parameters: MinSize.
74+
Performance/CollectionLiteralInLoop:
75+
Exclude:
76+
- 'spec/slack/web/api/endpoints/custom_specs/files_spec.rb'
77+
78+
# Offense count: 1
79+
# This cop supports safe auto-correction (--auto-correct).
80+
Performance/MapCompact:
81+
Exclude:
82+
- 'lib/slack/web/api/helpers/files.rb'
7183

7284
# Offense count: 2
7385
# This cop supports safe auto-correction (--auto-correct).
@@ -94,7 +106,7 @@ RSpec/ContextMethod:
94106
RSpec/ContextWording:
95107
Enabled: false
96108

97-
# Offense count: 67
109+
# Offense count: 70
98110
# Configuration parameters: CountAsOne.
99111
RSpec/ExampleLength:
100112
Max: 18
@@ -105,13 +117,13 @@ RSpec/ExampleLength:
105117
RSpec/FilePath:
106118
Enabled: false
107119

108-
# Offense count: 69
120+
# Offense count: 70
109121
# Configuration parameters: .
110122
# SupportedStyles: have_received, receive
111123
RSpec/MessageSpies:
112124
EnforcedStyle: receive
113125

114-
# Offense count: 94
126+
# Offense count: 95
115127
RSpec/MultipleExpectations:
116128
Max: 5
117129

@@ -188,6 +200,12 @@ Style/GlobalStdStream:
188200
- 'spec/integration/integration_spec.rb'
189201
- 'spec/slack/web/client_spec.rb'
190202

203+
# Offense count: 1
204+
# This cop supports unsafe auto-correction (--auto-correct-all).
205+
Style/MapToHash:
206+
Exclude:
207+
- 'spec/slack/web/api/endpoints/custom_specs/files_spec.rb'
208+
191209
# Offense count: 1
192210
Style/MultilineBlockChain:
193211
Exclude:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* [#530](https://github.com/slack-ruby/slack-ruby-client/pull/530): Specify dependency on `logger` - [@rwstauner](https://github.com/rwstauner).
77
* [#533](https://github.com/slack-ruby/slack-ruby-client/pull/533), [#536](https://github.com/slack-ruby/slack-ruby-client/pull/536): Added `files_upload_v2` - [@gregsaab](https://github.com/gregsaab).
88
* [#538](https://github.com/slack-ruby/slack-ruby-client/pull/538): Translate channel names and support an array of channels in `files_upload_v2` - [@dblock](https://github.com/dblock).
9+
* [#540](https://github.com/slack-ruby/slack-ruby-client/pull/540): Added support for `channel` and `channel_id`, do not require channel in `files_upload_v2` - [@dblock](https://github.com/dblock).
910
* Your contribution here.
1011

1112
### 2.4.0 (2024/07/14)

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,19 @@ This library provides a helper method `files_upload_v2` that wraps the three sep
178178
```ruby
179179
client.files_upload_v2(
180180
# required options
181-
channels: 'C000000,C000001', # comma delimited channel ids, only one channel is required
182181
filename: 'results.pdf', # this is used for the file title, unless a :title option is provided
183182
contents: File.read('/users/me/results.pdf'), # the string contents of the file
184183

185184
# optional options
185+
channels: ['C000000', 'C000001'], # channel IDs to share the file in (:channel_id, :channel, or :channels are all supported)
186186
initial_comment: 'Sharing the Q1 results :tada:', # the message that is included with the file share thread
187187
snippet_type: 'text', # the type of snippet
188188
title: 'Q1 Results', # sets the title of the file, overriding the filename
189189
thread_ts: '1738331487.481469' # specifies a thread to add this file to
190190
)
191191
```
192192

193-
You can use all of channel ID, an array of channel IDs, or a channel name (prefixed with `#`) in `files_upload_v2`. Lookup by name is not supported by the Slack API and this method called invokes `conversations_list` in order to locate the channel ID. This invocation can have a cost if you have many Slack channels and is only recommended when you intend to list channels anyway.
193+
You can use a channel ID passed as `channel_id`, a single channel as `channel`, an array of channel IDs as `channels`, or a channel name or names (prefixed with `#`) in `files_upload_v2`. Lookup by name is not supported by the Slack API and this method called invokes `conversations_list` in order to locate the channel ID. This invocation can have a cost if you have many Slack channels and is only recommended when you intend to list channels anyway.
194194

195195
Note: This library includes a `files_upload` method that uses a deprecated endpoint `files.upload` that will [no longer be supported on 3/11/2025](https://api.slack.com/methods/files.upload#markdown).
196196

lib/slack/web/api/helpers/files.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ module Files
1919
# Syntax type of the snippet being uploaded.
2020
# @option params [string] :title
2121
# Title of file.
22+
# @option params [string] :channel_id
23+
# Channel ID where the file will be shared. If not specified the file will be private.
24+
# @option params [string] :channel
25+
# Channel where the file will be shared, alias of channel_id. If not specified the file will be private.
2226
# @option params [string] :channels
2327
# Comma-separated string of channel IDs where the file will be shared. If not specified the file will be private.
2428
# @option params [string] :initial_comment
@@ -28,13 +32,26 @@ module Files
2832
# Never use a reply's ts value; use its parent instead.
2933
# Also make sure to provide only one channel when using 'thread_ts'.
3034
def files_upload_v2(params = {})
31-
%i[filename content channels].each do |param|
35+
%i[filename content].each do |param|
3236
raise ArgumentError, "Required argument :#{param} missing" if params[param].nil?
3337
end
3438

35-
channels = Array(params[:channels]).map do |channel|
36-
conversations_id(channel: channel)['channel']['id']
37-
end.uniq.join(',')
39+
channel_params = %i[channel channels channel_id].map { |param| params[param] }.compact
40+
raise ArgumentError, 'Only one of :channel, :channels, or :channel_id is required' if channel_params.size > 1
41+
42+
complete_upload_request_params = params.slice(:initial_comment, :thread_ts)
43+
44+
if params[:channels]
45+
complete_upload_request_params[:channels] = Array(params[:channels]).map do |channel|
46+
conversations_id(channel: channel)['channel']['id']
47+
end.uniq.join(',')
48+
elsif params[:channel]
49+
complete_upload_request_params[:channel] = conversations_id(
50+
channel: params[:channel]
51+
)['channel']['id']
52+
elsif params[:channel_id]
53+
complete_upload_request_params[:channel_id] = params[:channel_id]
54+
end
3855

3956
content = params[:content]
4057
title = params[:title] || params[:filename]
@@ -59,8 +76,6 @@ def files_upload_v2(params = {})
5976
end
6077

6178
# Complete the upload.
62-
complete_upload_request_params = params.slice(:initial_comment, :thread_ts)
63-
complete_upload_request_params[:channels] = channels
6479
complete_upload_request_params[:files] = [{ id: file_id, title: title }].to_json
6580

6681
files_completeUploadExternal(complete_upload_request_params)

spec/fixtures/slack/web/files_upload_v2.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)