Small changes to Twitter-Node

This commit is contained in:
Jan Oberhauser 2020-06-03 15:06:13 +02:00
parent d17d18ceb6
commit e3a6064196
5 changed files with 30 additions and 77 deletions

View file

@ -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'));
});

View file

@ -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) {

View file

@ -1,6 +1,6 @@
import {
OptionsWithUrl,
} from 'request';
} from 'request';
import {
IHookFunctions,

View file

@ -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',

View file

@ -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);
}
}