Skip to content

Commit cf9dea4

Browse files
committed
Added support for channel_id.
1 parent 308a3aa commit cf9dea4

File tree

10 files changed

+982
-36
lines changed

10 files changed

+982
-36
lines changed

.rubocop_todo.yml

Lines changed: 18 additions & 7 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:30:56 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,33 @@ Lint/RedundantCopDisableDirective:
4747
# Offense count: 13
4848
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
4949
Metrics/AbcSize:
50-
Max: 42
50+
Max: 62
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: 39
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+
# This cop supports safe auto-correction (--auto-correct).
74+
Performance/MapCompact:
75+
Exclude:
76+
- 'lib/slack/web/api/helpers/files.rb'
7177

7278
# Offense count: 2
7379
# This cop supports safe auto-correction (--auto-correct).
@@ -130,6 +136,11 @@ RSpec/NamedSubject:
130136
RSpec/NestedGroups:
131137
Max: 6
132138

139+
# Offense count: 2
140+
RSpec/RepeatedExample:
141+
Exclude:
142+
- 'spec/slack/web/api/endpoints/custom_specs/files_spec.rb'
143+
133144
# Offense count: 5
134145
RSpec/StubbedMock:
135146
Exclude:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ 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
181+
channels: ['C000000', 'C000001'], # channel IDs, one is required (:channel_id, :channel, or :channels are all supported)
182182
filename: 'results.pdf', # this is used for the file title, unless a :title option is provided
183183
contents: File.read('/users/me/results.pdf'), # the string contents of the file
184184

@@ -190,7 +190,7 @@ client.files_upload_v2(
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: 25 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,30 @@ 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_id]
49+
complete_upload_request_params[:channel_id] = conversations_id(
50+
channel: params[:channel_id]
51+
)['channel']['id']
52+
elsif params[:channel]
53+
complete_upload_request_params[:channel] = conversations_id(
54+
channel: params[:channel]
55+
)['channel']['id']
56+
else
57+
raise ArgumentError, 'At least one of :channel, :channels, or :channel_id is required'
58+
end
3859

3960
content = params[:content]
4061
title = params[:title] || params[:filename]
@@ -59,8 +80,6 @@ def files_upload_v2(params = {})
5980
end
6081

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

6685
files_completeUploadExternal(complete_upload_request_params)

spec/fixtures/slack/web/files_upload_v2_channel.yml

Lines changed: 224 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)