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

Update files.upload.v2 internals due to server-side improvements #1668

Merged
merged 2 commits into from Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/web-api/src/WebClient.spec.js
Expand Up @@ -1540,7 +1540,8 @@ describe('WebClient', function () {
}]
});

it('is called when request_file_info is true or undefined', async () => {
// since v7, the behavior has been changed
it('is not called when request_file_info is true or undefined', async () => {
client.getFileInfo = sinon.spy();
// set initial files upload arguments with request_file_info true
const withRequestFileInfoTrue = {
Expand All @@ -1549,14 +1550,16 @@ describe('WebClient', function () {
request_file_info: true,
};
await client.filesUploadV2(withRequestFileInfoTrue);
assert.equal(client.getFileInfo.called, true);
// since v7, the behavior has been changed
assert.equal(client.getFileInfo.called, false);

const withRequestFileInfoOmitted = {
file: Buffer.from('test'),
filename: 'test.txt',
}
await client.filesUploadV2(withRequestFileInfoOmitted);
assert.equal(client.getFileInfo.calledTwice, true);
// since v7, the behavior has been changed
assert.equal(client.getFileInfo.calledTwice, false);
});
it('is not called when request_file_info is set as false', async () => {
client.getFileInfo = sinon.spy();
Expand Down
23 changes: 1 addition & 22 deletions packages/web-api/src/WebClient.ts
Expand Up @@ -408,8 +408,6 @@ export class WebClient extends Methods {
*
* **#3**: Complete uploads {@link https://api.slack.com/methods/files.completeUploadExternal files.completeUploadExternal}
*
* **#4**: Unless `request_file_info` set to false, call {@link https://api.slack.com/methods/files.info files.info} for
* each file uploaded and returns that data. Requires that your app have `files:read` scope.
* @param options
*/
public async filesUploadV2(options: FilesUploadV2Arguments): Promise<WebAPICallResult> {
Expand All @@ -429,13 +427,7 @@ export class WebClient extends Methods {
// 3
const completion = await this.completeFileUploads(fileUploads);

// 4
let res = completion;
if (options.request_file_info ?? true) {
res = await this.getFileInfo(fileUploads);
}

return { ok: true, files: res };
return { ok: true, files: completion };
}

/**
Expand Down Expand Up @@ -472,19 +464,6 @@ export class WebClient extends Methods {
);
}

/**
* Call {@link https://api.slack.com/methods/files.info files.info} for
* each file uploaded and returns relevant data. Requires that your app have `files:read` scope, to
* turn off, set `request_file_info` set to false.
* @param fileUploads
* @returns
*/
private async getFileInfo(fileUploads: FileUploadV2Job[]):
Promise<Array<WebAPICallResult>> {
/* eslint-disable @typescript-eslint/no-non-null-assertion */
return Promise.all(fileUploads.map((job) => this.files.info({ file: job.file_id! })));
}

/**
* for each returned file upload URL, upload corresponding file
* @param fileUploads
Expand Down
3 changes: 3 additions & 0 deletions packages/web-api/src/methods.ts
Expand Up @@ -1884,6 +1884,9 @@ interface FileUpload {

export interface FilesUploadV2Arguments extends FileUploadV2, WebAPICallOptions, TokenOverridable {
file_uploads?: Omit<FileUploadV2, 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts'>[];
/**
* @deprecated Since v7, this flag is no longer used. You can safely remove it from your code.
*/
request_file_info?: boolean;
}

Expand Down
Expand Up @@ -18,6 +18,57 @@ export type FilesCompleteUploadExternalResponse = WebAPICallResult & {
};

export interface File {
id?: string;
title?: string;
channels?: string[];
comments_count?: number;
created?: number;
display_as_bot?: boolean;
edit_link?: string;
editable?: boolean;
external_type?: string;
file_access?: string;
filetype?: string;
groups?: string[];
has_more_shares?: boolean;
has_rich_preview?: boolean;
id?: string;
ims?: string[];
is_external?: boolean;
is_public?: boolean;
is_starred?: boolean;
lines?: number;
lines_more?: number;
media_display_type?: string;
mimetype?: string;
mode?: string;
name?: string;
permalink?: string;
permalink_public?: string;
pretty_type?: string;
preview?: string;
preview_highlight?: string;
preview_is_truncated?: boolean;
public_url_shared?: boolean;
shares?: Shares;
size?: number;
timestamp?: number;
title?: string;
url_private?: string;
url_private_download?: string;
user?: string;
user_team?: string;
username?: string;
}

export interface Shares {
public?: { [key: string]: Public[] };
}

export interface Public {
channel_name?: string;
reply_count?: number;
reply_users?: string[];
reply_users_count?: number;
share_user_id?: string;
team_id?: string;
ts?: string;
}