-
Notifications
You must be signed in to change notification settings - Fork 681
feat(web-api): include a blocks argument for file uploads #2261
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ export async function getFileUploadJob( | |
| const fileUploadJob: Record<string, unknown> = { | ||
| // supplied by user | ||
| alt_text: options.alt_text, | ||
| blocks: options.blocks, | ||
| channel_id: options.channels ?? options.channel_id, | ||
| filename: options.filename ?? fileName, | ||
| initial_comment: options.initial_comment, | ||
|
|
@@ -96,8 +97,8 @@ export async function getMultipleFileUploadJobs( | |
| // ensure no omitted properties included in files_upload entry | ||
| // these properties are valid only at the top-level, not | ||
| // inside file_uploads. | ||
| const { channel_id, channels, initial_comment, thread_ts } = upload as FileUploadV2; | ||
| if (channel_id || channels || initial_comment || thread_ts) { | ||
| const { blocks, channel_id, channels, initial_comment, thread_ts } = upload as FileUploadV2; | ||
| if (blocks || channel_id || channels || initial_comment || thread_ts) { | ||
| throw errorWithCode( | ||
| new Error(buildInvalidFilesUploadParamError()), | ||
| ErrorCode.FileUploadInvalidArgumentsError, | ||
|
|
@@ -107,6 +108,7 @@ export async function getMultipleFileUploadJobs( | |
| // supplied at the top level. | ||
| const uploadJobArgs: Record<string, unknown> = { | ||
| ...upload, | ||
| blocks: options.blocks, | ||
| channels: options.channels, | ||
| channel_id: options.channel_id, | ||
| initial_comment: options.initial_comment, | ||
|
|
@@ -220,7 +222,7 @@ export async function getFileDataAsStream(readable: Readable): Promise<Buffer> { | |
|
|
||
| /** | ||
| * Filters through all fileUploads and groups them into jobs for completion | ||
| * based on combination of channel_id, thread_ts, initial_comment. | ||
| * based on combination of channel_id, thread_ts, initial_comment, blocks. | ||
| * {@link https://api.slack.com/methods/files.completeUploadExternal files.completeUploadExternal} allows for multiple | ||
| * files to be uploaded with a message (`initial_comment`), and as a threaded message (`thread_ts`) | ||
| * In order to be grouped together, file uploads must have like properties. | ||
|
|
@@ -232,13 +234,14 @@ export function getAllFileUploadsToComplete( | |
| ): Record<string, FilesCompleteUploadExternalArguments> { | ||
| const toComplete: Record<string, FilesCompleteUploadExternalArguments> = {}; | ||
| for (const upload of fileUploads) { | ||
| const { channel_id, thread_ts, initial_comment, file_id, title } = upload; | ||
| const { blocks, channel_id, thread_ts, initial_comment, file_id, title } = upload; | ||
| if (file_id) { | ||
| const compareString = `:::${channel_id}:::${thread_ts}:::${initial_comment}`; | ||
| const compareString = `:::${channel_id}:::${thread_ts}:::${initial_comment}:::${JSON.stringify(blocks)}`; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch ๐
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! ๐ I'm sometimes thankful to use manual testing during development, but I also thinking improving tests according to an above comment could show this change too ๐ |
||
| if (!Object.prototype.hasOwnProperty.call(toComplete, compareString)) { | ||
| toComplete[compareString] = { | ||
| files: [{ id: file_id, title }], | ||
| channel_id, | ||
| blocks, | ||
| initial_comment, | ||
| }; | ||
| if (thread_ts && channel_id) { | ||
|
|
@@ -425,7 +428,7 @@ export function buildMultipleChannelsErrorMsg(): string { | |
|
|
||
| export function buildInvalidFilesUploadParamError(): string { | ||
| return ( | ||
| 'You may supply file_uploads only for a single channel, comment, thread respectively. ' + | ||
| 'Therefore, please supply any channel_id, initial_comment, thread_ts in the top-layer.' | ||
| 'You may supply file_uploads only for a single channel, message, or thread respectively. ' + | ||
| 'Therefore, please supply any channel_id, initial_comment or blocks, or thread_ts in the top-layer.' | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import type { Stream } from 'node:stream'; | ||
| import type { Block, KnownBlock } from '@slack/types'; | ||
| import type { ExcludeFromUnion } from '../helpers'; | ||
| import type { FilesGetUploadURLExternalResponse } from '../response/index'; | ||
| import type { | ||
|
|
@@ -64,10 +65,19 @@ type FileDestinationArgumentChannels = FileChannelDestinationArgumentChannels | | |
| // https://api.slack.com/methods/files.completeUploadExternal | ||
| export type FilesCompleteUploadExternalArguments = FileDestinationArgument & | ||
| TokenOverridable & { | ||
| /** @description Array of file IDs and their corresponding (optional) titles. */ | ||
| /** | ||
| * @description Array of file IDs and their corresponding (optional) titles. | ||
| * @example [{"id":"F044GKUHN9Z", "title":"slack-test"}] | ||
| **/ | ||
| files: [FileUploadComplete, ...FileUploadComplete[]]; | ||
| /** @description The message text introducing the file in the specified channel. */ | ||
| initial_comment?: string; | ||
| /** | ||
| * @description An array of structured rich text blocks. If the `initial_comment` field is provided, the `blocks` field is ignored. | ||
| * @example [{"type": "section", "text": {"type": "plain_text", "text": "Hello world"}}] | ||
| * @see {@link https://api.slack.com/reference/block-kit/blocks} | ||
| */ | ||
| blocks?: (KnownBlock | Block)[]; | ||
| }; | ||
|
|
||
| // https://api.slack.com/methods/files.delete | ||
|
|
@@ -148,6 +158,12 @@ export type FilesUploadArguments = FileUpload & TokenOverridable; | |
| export type FileUploadV2 = FileUpload & { | ||
| /** @description Description of image for screen-reader. */ | ||
| alt_text?: string; | ||
| /** | ||
| * @description An array of structured rich text blocks. If the `initial_comment` field is provided, the `blocks` field is ignored. | ||
| * @example [{"type": "section", "text": {"type": "plain_text", "text": "Hello world"}}] | ||
| * @see {@link https://api.slack.com/reference/block-kit/blocks} | ||
| */ | ||
| blocks?: (KnownBlock | Block)[]; | ||
| /** @description Channel ID where the file will be shared. If not specified the file will be private. */ | ||
| channel_id?: string; | ||
| /** @deprecated use channel_id instead */ | ||
|
|
@@ -157,7 +173,10 @@ export type FileUploadV2 = FileUpload & { | |
| }; | ||
|
|
||
| export interface FilesUploadV2ArgumentsMultipleFiles { | ||
| file_uploads: ExcludeFromUnion<FileUploadV2, 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts'>[]; | ||
| file_uploads: ExcludeFromUnion< | ||
| FileUploadV2, | ||
| 'blocks' | 'channel_id' | 'channels' | 'initial_comment' | 'thread_ts' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alphabetical order ๐ฎ ๐ |
||
| >[]; | ||
| } | ||
|
|
||
| // https://tools.slack.dev/node-slack-sdk/web-api#upload-a-file | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice ๐ฏ