Skip to content

Commit

Permalink
Update files.upload.v2 internals due to server-side improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Oct 4, 2023
1 parent 89b109d commit 7857f26
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
9 changes: 6 additions & 3 deletions packages/web-api/src/WebClient.spec.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion packages/web-api/src/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ interface FileUpload {

export interface FilesUploadV2Arguments extends FileUploadV2, WebAPICallOptions, TokenOverridable {
file_uploads?: Omit<FileUploadV2, 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts'>[];
request_file_info?: boolean;
request_file_info?: boolean; // since v7, this flag is no longer used
}

export type FileUploadV2 = FileUpload & {
Expand Down
Original file line number Diff line number Diff line change
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;
}

0 comments on commit 7857f26

Please sign in to comment.