Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add channels param to files.upload v2 method (and its underlying files.completeUploadExternal) #1427

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add channels param to files.upload v2 method
seratch committed Jan 27, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit d7cff9d2e9eb5bf596d2ad864e5135e1c1255e1d
Original file line number Diff line number Diff line change
@@ -2086,7 +2086,11 @@
if (req.getFiles() != null) {
setIfNotNull("files", GSON.toJson(req.getFiles()), form);
}
setIfNotNull("channel_id", req.getChannelId(), form);
if (req.getChannels() != null) {
setIfNotNull("channels", req.getChannels().stream().collect(joining(",")), form);

Check warning on line 2090 in slack-api-client/src/main/java/com/slack/api/methods/RequestFormBuilder.java

Codecov / codecov/patch

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

Added line #L2090 was not covered by tests
} else {
setIfNotNull("channel_id", req.getChannelId(), form);
}
setIfNotNull("initial_comment", req.getInitialComment(), form);
setIfNotNull("thread_ts", req.getThreadTs(), form);
return form;
Original file line number Diff line number Diff line change
@@ -94,6 +94,7 @@
.token(v2Request.getToken())
.files(files)
.channelId(v2Request.getChannel())
.channels(v2Request.getChannels())

Check warning on line 97 in slack-api-client/src/main/java/com/slack/api/methods/impl/FilesUploadV2Helper.java

Codecov / codecov/patch

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

Added line #L97 was not covered by tests
.initialComment(v2Request.getInitialComment())
.threadTs(v2Request.getThreadTs())
);
Original file line number Diff line number Diff line change
@@ -30,6 +30,11 @@ public class FilesCompleteUploadExternalRequest implements SlackApiRequest {
*/
private String channelId;

/**
* Comma-separated string of channel IDs where the file will be shared.
*/
private List<String> channels;

/**
* The message text introducing the file in specified channels.
*/
Original file line number Diff line number Diff line change
@@ -130,6 +130,11 @@ public static class UploadFile {
*/
private String channel;

/**
* Comma-separated string of channel IDs where the file will be shared.
*/
private List<String> channels;

/**
* Provide another message's ts value to upload this file as a reply.
* Never use a reply's ts value; use its parent instead.
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@
@Slf4j
public class files_Test {

private String generalChannelId = null;
private String randomChannelId = null;

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

void loadGeneralChannelId() throws IOException, SlackApiException {
if (generalChannelId == null) {
ConversationsListResponse channelsListResponse =
slack.methods().conversationsList(r -> r.token(botToken).excludeArchived(true).limit(100));
assertThat(channelsListResponse.getError(), is(nullValue()));
for (Conversation channel : channelsListResponse.getChannels()) {
if (channel.getName().equals("general")) {
generalChannelId = channel.getId();
break;
}
}
}
}

static SlackTestConfig testConfig = SlackTestConfig.getInstance();
static Slack slack = Slack.getInstance(testConfig.getConfig());

@@ -1254,4 +1269,52 @@ public void issue1345_filesUploadV2_multiple_no_filename_no_title() throws IOExc
// title defaults to filename, which is (as this is defaulted on the client side) "Uploaded file"
assertThat(response.getFiles().get(0).getTitle(), is("Uploaded file"));
}

@Test
public void filesUploadV2_channels() throws Exception {
loadRandomChannelId();
loadGeneralChannelId();
MethodsClient client = slack.methods(botToken);

File file1 = new File("src/test/resources/sample.txt");
FilesUploadV2Response response = client.filesUploadV2(r -> r
.file(file1)
.title("sample.txt")
.filename("sample.txt")
.snippetType("text")
.channels(Arrays.asList(generalChannelId, randomChannelId))
.initialComment("Here you are :wave:")
);
assertThat(response.getError(), is(nullValue()));

List<String> expectedFileIds = response.getFiles().stream()
.map(a -> a.getId()).collect(Collectors.toList());

int count = 0;
ConversationsHistoryResponse history = null;
List<String> actualFileIds = null;
while (count < 10) {
count++;
history = client.conversationsHistory(r -> r
.channel(randomChannelId)
.limit(1)
);
if (history.getMessages().get(0).getFiles() != null) {
actualFileIds = history.getMessages().get(0).getFiles().stream()
.map(a -> a.getId()).sorted().collect(Collectors.toList());
if (actualFileIds.stream().collect(Collectors.joining(","))
.equals(expectedFileIds.stream().collect(Collectors.joining(",")))) {
break;
}
}
Thread.sleep(3000L);
}
assertThat(history.getError(), is(nullValue()));
assertThat(history.getMessages().get(0).getFiles(), is(notNullValue()));
assertThat(actualFileIds, is(expectedFileIds));

FilesInfoResponse file1info = client.filesInfo(r -> r.file(response.getFile().getId()));
assertThat(file1info.getFile().getShares().getPublicChannels().get(randomChannelId), is(notNullValue()));
}

}