Skip to content

Commit 2094cf2

Browse files
authored
Add channels param to files.upload v2 method (#1427)
1 parent c8d803a commit 2094cf2

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

slack-api-client/src/main/java/com/slack/api/methods/RequestFormBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,7 +2086,11 @@ public static FormBody.Builder toForm(FilesCompleteUploadExternalRequest req) {
20862086
if (req.getFiles() != null) {
20872087
setIfNotNull("files", GSON.toJson(req.getFiles()), form);
20882088
}
2089-
setIfNotNull("channel_id", req.getChannelId(), form);
2089+
if (req.getChannels() != null) {
2090+
setIfNotNull("channels", req.getChannels().stream().collect(joining(",")), form);
2091+
} else {
2092+
setIfNotNull("channel_id", req.getChannelId(), form);
2093+
}
20902094
setIfNotNull("initial_comment", req.getInitialComment(), form);
20912095
setIfNotNull("thread_ts", req.getThreadTs(), form);
20922096
return form;

slack-api-client/src/main/java/com/slack/api/methods/impl/FilesUploadV2Helper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public FilesUploadV2Response completeUploads(
9494
.token(v2Request.getToken())
9595
.files(files)
9696
.channelId(v2Request.getChannel())
97+
.channels(v2Request.getChannels())
9798
.initialComment(v2Request.getInitialComment())
9899
.threadTs(v2Request.getThreadTs())
99100
);

slack-api-client/src/main/java/com/slack/api/methods/request/files/FilesCompleteUploadExternalRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class FilesCompleteUploadExternalRequest implements SlackApiRequest {
3030
*/
3131
private String channelId;
3232

33+
/**
34+
* Comma-separated string of channel IDs where the file will be shared.
35+
*/
36+
private List<String> channels;
37+
3338
/**
3439
* The message text introducing the file in specified channels.
3540
*/

slack-api-client/src/main/java/com/slack/api/methods/request/files/FilesUploadV2Request.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ public static class UploadFile {
130130
*/
131131
private String channel;
132132

133+
/**
134+
* Comma-separated string of channel IDs where the file will be shared.
135+
*/
136+
private List<String> channels;
137+
133138
/**
134139
* Provide another message's ts value to upload this file as a reply.
135140
* Never use a reply's ts value; use its parent instead.

slack-api-client/src/test/java/test_with_remote_apis/methods/files_Test.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
@Slf4j
4747
public class files_Test {
4848

49+
private String generalChannelId = null;
4950
private String randomChannelId = null;
5051

5152
void loadRandomChannelId() throws IOException, SlackApiException {
@@ -62,6 +63,20 @@ void loadRandomChannelId() throws IOException, SlackApiException {
6263
}
6364
}
6465

66+
void loadGeneralChannelId() throws IOException, SlackApiException {
67+
if (generalChannelId == null) {
68+
ConversationsListResponse channelsListResponse =
69+
slack.methods().conversationsList(r -> r.token(botToken).excludeArchived(true).limit(100));
70+
assertThat(channelsListResponse.getError(), is(nullValue()));
71+
for (Conversation channel : channelsListResponse.getChannels()) {
72+
if (channel.getName().equals("general")) {
73+
generalChannelId = channel.getId();
74+
break;
75+
}
76+
}
77+
}
78+
}
79+
6580
static SlackTestConfig testConfig = SlackTestConfig.getInstance();
6681
static Slack slack = Slack.getInstance(testConfig.getConfig());
6782

@@ -1254,4 +1269,52 @@ public void issue1345_filesUploadV2_multiple_no_filename_no_title() throws IOExc
12541269
// title defaults to filename, which is (as this is defaulted on the client side) "Uploaded file"
12551270
assertThat(response.getFiles().get(0).getTitle(), is("Uploaded file"));
12561271
}
1272+
1273+
@Test
1274+
public void filesUploadV2_channels() throws Exception {
1275+
loadRandomChannelId();
1276+
loadGeneralChannelId();
1277+
MethodsClient client = slack.methods(botToken);
1278+
1279+
File file1 = new File("src/test/resources/sample.txt");
1280+
FilesUploadV2Response response = client.filesUploadV2(r -> r
1281+
.file(file1)
1282+
.title("sample.txt")
1283+
.filename("sample.txt")
1284+
.snippetType("text")
1285+
.channels(Arrays.asList(generalChannelId, randomChannelId))
1286+
.initialComment("Here you are :wave:")
1287+
);
1288+
assertThat(response.getError(), is(nullValue()));
1289+
1290+
List<String> expectedFileIds = response.getFiles().stream()
1291+
.map(a -> a.getId()).collect(Collectors.toList());
1292+
1293+
int count = 0;
1294+
ConversationsHistoryResponse history = null;
1295+
List<String> actualFileIds = null;
1296+
while (count < 10) {
1297+
count++;
1298+
history = client.conversationsHistory(r -> r
1299+
.channel(randomChannelId)
1300+
.limit(1)
1301+
);
1302+
if (history.getMessages().get(0).getFiles() != null) {
1303+
actualFileIds = history.getMessages().get(0).getFiles().stream()
1304+
.map(a -> a.getId()).sorted().collect(Collectors.toList());
1305+
if (actualFileIds.stream().collect(Collectors.joining(","))
1306+
.equals(expectedFileIds.stream().collect(Collectors.joining(",")))) {
1307+
break;
1308+
}
1309+
}
1310+
Thread.sleep(3000L);
1311+
}
1312+
assertThat(history.getError(), is(nullValue()));
1313+
assertThat(history.getMessages().get(0).getFiles(), is(notNullValue()));
1314+
assertThat(actualFileIds, is(expectedFileIds));
1315+
1316+
FilesInfoResponse file1info = client.filesInfo(r -> r.file(response.getFile().getId()));
1317+
assertThat(file1info.getFile().getShares().getPublicChannels().get(randomChannelId), is(notNullValue()));
1318+
}
1319+
12571320
}

0 commit comments

Comments
 (0)