From e3a6064196e7d9c7b87f8f6384e740ad03e1e943 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Wed, 3 Jun 2020 15:06:13 +0200 Subject: [PATCH] :zap: Small changes to Twitter-Node --- packages/cli/src/Server.ts | 4 +- .../src/components/CredentialsInput.vue | 7 +-- .../nodes/Twitter/GenericFunctions.ts | 2 +- .../nodes/Twitter/TweetDescription.ts | 50 +++---------------- .../nodes-base/nodes/Twitter/Twitter.node.ts | 44 +++++++--------- 5 files changed, 30 insertions(+), 77 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 88e60d4c52..1d7f1506de 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -988,7 +988,7 @@ class App { throw new Error('Insufficient parameters for OAuth1 callback'); } - const result = await Db.collections.Credentials!.findOne(cid as any); + const result = await Db.collections.Credentials!.findOne(cid as any); // tslint:disable-line:no-any if (result === undefined) { const errorResponse = new ResponseHelper.ResponseError('The credential is not known.', undefined, 404); return ResponseHelper.sendErrorResponse(res, errorResponse); @@ -1041,7 +1041,7 @@ class App { // Add special database related data newCredentialsData.updatedAt = this.getCurrentDate(); // Save the credentials in DB - await Db.collections.Credentials!.update(cid as any, newCredentialsData); + await Db.collections.Credentials!.update(cid as any, newCredentialsData); // tslint:disable-line:no-any res.sendFile(pathResolve(__dirname, '../../templates/oauth-callback.html')); }); diff --git a/packages/editor-ui/src/components/CredentialsInput.vue b/packages/editor-ui/src/components/CredentialsInput.vue index ba8121ddbe..3d8edec3ec 100644 --- a/packages/editor-ui/src/components/CredentialsInput.vue +++ b/packages/editor-ui/src/components/CredentialsInput.vue @@ -219,14 +219,11 @@ export default mixins( return this.credentialDataTemp; }, isOAuthType (): boolean { - if (this.credentialTypeData.name === 'oAuth2Api') { - return true; - } - if (this.credentialTypeData.name === 'oAuth1Api') { + if (['oAuth1Api', 'oAuth2Api'].includes(this.credentialTypeData.name)) { return true; } const types = this.parentTypes(this.credentialTypeData.name); - return types.includes('oAuth2Api') || types.includes('oAuth1Api'); + return types.includes('oAuth1Api') || types.includes('oAuth2Api'); }, isOAuthConnected (): boolean { if (this.isOAuthType === false) { diff --git a/packages/nodes-base/nodes/Twitter/GenericFunctions.ts b/packages/nodes-base/nodes/Twitter/GenericFunctions.ts index c47f63cb17..d13ea1c548 100644 --- a/packages/nodes-base/nodes/Twitter/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Twitter/GenericFunctions.ts @@ -1,6 +1,6 @@ import { OptionsWithUrl, - } from 'request'; +} from 'request'; import { IHookFunctions, diff --git a/packages/nodes-base/nodes/Twitter/TweetDescription.ts b/packages/nodes-base/nodes/Twitter/TweetDescription.ts index 5b97406b40..46e1760e6f 100644 --- a/packages/nodes-base/nodes/Twitter/TweetDescription.ts +++ b/packages/nodes-base/nodes/Twitter/TweetDescription.ts @@ -1,6 +1,6 @@ import { INodeProperties, - } from 'n8n-workflow'; +} from 'n8n-workflow'; export const tweetOperations = [ { @@ -78,53 +78,15 @@ export const tweetFields = [ }, options: [ { - name: 'attachmentsValues', - displayName: 'Attachments Values', + name: 'attachment', + displayName: 'Attachment', values: [ { - displayName: 'Data', - name: 'data', + displayName: 'Binary Property', + name: 'binaryPropertyName', type: 'string', default: '', - description: 'The base64-encoded file content being uploaded.', - }, - { - displayName: 'Category', - name: 'category', - type: 'options', - options: [ - { - name: 'Amplify Video', - value: 'amplifyVideo', - }, - { - name: 'Gif', - value: 'tweetGif', - }, - { - name: 'Image', - value: 'tweetImage', - }, - { - name: 'Video', - value: 'tweetVideo', - }, - ], - default: '', - description: 'The category that represents how the media will be used', - }, - ], - }, - { - name: 'attachmentsBinary', - displayName: 'Attachments Binary', - values: [ - { - displayName: 'Property', - name: 'property', - type: 'string', - default: '', - description: 'Name of the binary properties which contain data which should be added to email as attachment', + description: 'Name of the binary properties which contain data which should be added to tweet as attachment', }, { displayName: 'Category', diff --git a/packages/nodes-base/nodes/Twitter/Twitter.node.ts b/packages/nodes-base/nodes/Twitter/Twitter.node.ts index 6e17373674..09b1dcde93 100644 --- a/packages/nodes-base/nodes/Twitter/Twitter.node.ts +++ b/packages/nodes-base/nodes/Twitter/Twitter.node.ts @@ -1,7 +1,7 @@ import { IExecuteFunctions, - } from 'n8n-core'; +} from 'n8n-core'; import { IBinaryKeyData, @@ -22,11 +22,11 @@ import { import { ITweet, - } from './TweetInterface'; +} from './TweetInterface'; - import { - snakeCase, - } from 'change-case'; +import { + snakeCase, +} from 'change-case'; export class Twitter implements INodeType { description: INodeTypeDescription = { @@ -73,62 +73,55 @@ export class Twitter implements INodeType { const items = this.getInputData(); const returnData: IDataObject[] = []; const length = items.length as unknown as number; - const qs: IDataObject = {}; let responseData; const resource = this.getNodeParameter('resource', 0) as string; const operation = this.getNodeParameter('operation', 0) as string; for (let i = 0; i < length; i++) { if (resource === 'tweet') { - //https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update + // https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update if (operation === 'create') { const text = this.getNodeParameter('text', i) as string; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const body: ITweet = { status: text, }; + if (additionalFields.attachmentsUi) { const mediaIds = []; const attachmentsUi = additionalFields.attachmentsUi as IDataObject; const uploadUri = 'https://upload.twitter.com/1.1/media/upload.json'; - if (attachmentsUi.attachmentsValues) { - const attachtments = attachmentsUi.attachmentsValues as IDataObject[]; - for (const attachment of attachtments) { - const body = { - media_data: attachment.data, - media_category: snakeCase(attachment.category as string).toUpperCase(), - }; - const response = await twitterApiRequest.call(this, 'POST', '', body, {}, uploadUri); - mediaIds.push(response.media_id); - } - } - if (attachmentsUi.attachmentsBinary) { - const attachtments = attachmentsUi.attachmentsBinary as IDataObject[]; + + if (attachmentsUi.attachment) { + const attachtments = attachmentsUi.attachment as IDataObject[]; for (const attachment of attachtments) { const binaryData = items[i].binary as IBinaryKeyData; - const propertyName = attachment.property as string; + const binaryPropertyName = attachment.binaryPropertyName as string; if (binaryData === undefined) { throw new Error('No binary data set. So file can not be written!'); } - if (!binaryData[propertyName]) { + if (!binaryData[binaryPropertyName]) { continue; } - const body = { - media_data: binaryData[propertyName].data, + const attachmentBody = { + media_data: binaryData[binaryPropertyName].data, media_category: snakeCase(attachment.category as string).toUpperCase(), }; - const response = await twitterApiRequest.call(this, 'POST', '', body, {}, uploadUri); + const response = await twitterApiRequest.call(this, 'POST', '', attachmentBody, {}, uploadUri); mediaIds.push(response.media_id_string); } } + body.media_ids = mediaIds.join(','); } + if (additionalFields.possiblySensitive) { body.possibly_sensitive = additionalFields.possibly_sensitive as boolean; } + if (additionalFields.locationFieldsUi) { const locationUi = additionalFields.locationFieldsUi as IDataObject; if (locationUi.locationFieldsValues) { @@ -137,6 +130,7 @@ export class Twitter implements INodeType { body.long = parseFloat(values.lalatitude as string); } } + responseData = await twitterApiRequest.call(this, 'POST', '/statuses/update.json', body); } }