fixed tabs issue

This commit is contained in:
Ricardo Espinoza 2019-11-11 00:00:24 -05:00
parent e9613944db
commit 11826d6a4e
2 changed files with 347 additions and 351 deletions

View file

@ -1,5 +1,4 @@
import { OptionsWithUri } from 'request'; import { OptionsWithUri } from 'request';
import { import {
IExecuteFunctions, IExecuteFunctions,
IHookFunctions, IHookFunctions,
@ -8,17 +7,16 @@ import {
} from 'n8n-core'; } from 'n8n-core';
export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any export async function rocketchatApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, resource: string, method: string, operation: string, body: any = {}, headers?: object): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('rocketchatApi'); const credentials = this.getCredentials('rocketchatApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
const headerWithAuthentication = Object.assign({}, headers, const headerWithAuthentication = Object.assign({}, headers,
{ 'X-Auth-Token': credentials.authKey, 'X-User-Id': credentials.userId }); { 'X-Auth-Token': credentials.authKey, 'X-User-Id': credentials.userId });
const endpoint = 'rocket.chat/api/v1'; const endpoint = 'rocket.chat/api/v1';
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: headerWithAuthentication, headers: headerWithAuthentication,

View file

@ -7,47 +7,46 @@ import {
INodeExecutionData, INodeExecutionData,
INodeType INodeType
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
rocketchatApiRequest, rocketchatApiRequest,
validateJSON validateJSON
} from './GenericFunctions'; } from './GenericFunctions';
interface IField { interface IField {
short?: boolean; short?: boolean;
title?: string; title?: string;
value?: string; value?: string;
} }
interface IAttachment { interface IAttachment {
color?: string; color?: string;
text?: string; text?: string;
ts?: string; ts?: string;
title?: string; title?: string;
thumb_url?: string; thumb_url?: string;
message_link?: string; message_link?: string;
collapsed?: boolean; collapsed?: boolean;
author_name?: string; author_name?: string;
author_link?: string; author_link?: string;
author_icon?: string; author_icon?: string;
title_link?: string; title_link?: string;
title_link_download?: boolean; title_link_download?: boolean;
image_url?: string; image_url?: string;
audio_url?: string; audio_url?: string;
video_url?: string; video_url?: string;
fields?: IField[]; fields?: IField[];
} }
interface IPostMessageBody { interface IPostMessageBody {
channel: string; channel: string;
text?: string; text?: string;
alias?: string; alias?: string;
emoji?: string; emoji?: string;
avatar?: string; avatar?: string;
attachments?: IAttachment[]; attachments?: IAttachment[];
} }
export class Rocketchat implements INodeType { export class Rocketchat implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Rocketchat', displayName: 'Rocketchat',
name: 'Rocketchat', name: 'Rocketchat',
@ -67,9 +66,9 @@ export class Rocketchat implements INodeType {
name: 'rocketchatApi', name: 'rocketchatApi',
required: true, required: true,
} }
], ],
properties: [ properties: [
{ {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
type: 'options', type: 'options',
@ -102,26 +101,26 @@ export class Rocketchat implements INodeType {
], ],
default: '', default: '',
description: 'The operation to perform.', description: 'The operation to perform.',
}, },
{ {
displayName: 'Channel', displayName: 'Channel',
name: 'channel', name: 'channel',
type: 'string', type: 'string',
required: true, required: true,
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage' 'postMessage'
] ]
}, },
}, },
default: '', default: '',
description: 'TThe channel name with the prefix in front of it.', description: 'TThe channel name with the prefix in front of it.',
}, },
{ {
displayName: 'Alias', displayName: 'Alias',
name: 'alias', name: 'alias',
type: 'string', type: 'string',
@ -129,16 +128,16 @@ export class Rocketchat implements INodeType {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage' 'postMessage'
] ]
}, },
}, },
default: '', default: '',
description: 'This will cause the messages name to appear as the given alias, but your username will still display.', description: 'This will cause the messages name to appear as the given alias, but your username will still display.',
}, },
{ {
displayName: 'Avatar', displayName: 'Avatar',
name: 'avatar', name: 'avatar',
type: 'string', type: 'string',
@ -146,16 +145,16 @@ export class Rocketchat implements INodeType {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage' 'postMessage'
] ]
}, },
}, },
default: '', default: '',
description: 'If provided, this will make the avatar use the provided image url.', description: 'If provided, this will make the avatar use the provided image url.',
}, },
{ {
displayName: 'Text', displayName: 'Text',
name: 'text', name: 'text',
type: 'string', type: 'string',
@ -163,16 +162,16 @@ export class Rocketchat implements INodeType {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage' 'postMessage'
] ]
}, },
}, },
default: '', default: '',
description: 'The text of the message to send, is optional because of attachments.', description: 'The text of the message to send, is optional because of attachments.',
}, },
{ {
displayName: 'JSON Parameters', displayName: 'JSON Parameters',
name: 'jsonParameters', name: 'jsonParameters',
type: 'boolean', type: 'boolean',
@ -189,7 +188,7 @@ export class Rocketchat implements INodeType {
}, },
}, },
}, },
{ {
displayName: 'Options', displayName: 'Options',
name: 'options', name: 'options',
type: 'collection', type: 'collection',
@ -197,328 +196,327 @@ export class Rocketchat implements INodeType {
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage', 'postMessage',
], ],
}, },
}, },
options: [ options: [
{ {
displayName: 'Emoji', displayName: 'Emoji',
name: 'emoji', name: 'emoji',
type: 'string', type: 'string',
default: '', default: '',
description: 'This will cause the messages name to appear as the given alias, but your username will still display.', description: 'This will cause the messages name to appear as the given alias, but your username will still display.',
} }
] ]
}, },
{ {
displayName: 'Attachments', displayName: 'Attachments',
name: 'attachments', name: 'attachments',
type: 'collection', type: 'collection',
default: {}, default: {},
placeholder: 'Add Attachment Item', placeholder: 'Add Attachment Item',
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
multipleValueButtonText: 'Add Attachment', multipleValueButtonText: 'Add Attachment',
}, },
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'chat', 'chat',
], ],
operation: [ operation: [
'postMessage', 'postMessage',
], ],
jsonParameters: [ jsonParameters: [
false false
], ],
}, },
}, },
options: [ options: [
{ {
displayName: 'Color', displayName: 'Color',
name: 'color', name: 'color',
type: 'string', type: 'string',
default: '', default: '',
description: 'The color you want the order on the left side to be, any value background-css supports.', description: 'The color you want the order on the left side to be, any value background-css supports.',
}, },
{ {
displayName: 'Text', displayName: 'Text',
name: 'text', name: 'text',
type: 'string', type: 'string',
default: '', default: '',
description: 'The text to display for this attachment, it is different than the messages text.', description: 'The text to display for this attachment, it is different than the messages text.',
}, },
{ {
displayName: 'Timestamp', displayName: 'Timestamp',
name: 'ts', name: 'ts',
type: 'dateTime', type: 'dateTime',
default: '', default: '',
description: 'Displays the time next to the text portion.', description: 'Displays the time next to the text portion.',
}, },
{ {
displayName: 'Thumb URL', displayName: 'Thumb URL',
name: 'thumbUrl', name: 'thumbUrl',
type: 'string', type: 'string',
default: '', default: '',
description: 'An image that displays to the left of the text, looks better when this is relatively small.', description: 'An image that displays to the left of the text, looks better when this is relatively small.',
}, },
{ {
displayName: 'Message Link', displayName: 'Message Link',
name: 'messageLink', name: 'messageLink',
type: 'string', type: 'string',
default: '', default: '',
description: 'Only applicable if the timestamp is provided, as it makes the time clickable to this link.', description: 'Only applicable if the timestamp is provided, as it makes the time clickable to this link.',
}, },
{ {
displayName: 'Collapsed', displayName: 'Collapsed',
name: 'collapsed', name: 'collapsed',
type: 'boolean', type: 'boolean',
default: false, default: false,
description: 'Causes the image, audio, and video sections to be hiding when collapsed is true.', description: 'Causes the image, audio, and video sections to be hiding when collapsed is true.',
}, },
{ {
displayName: 'Author Name', displayName: 'Author Name',
name: 'authorName', name: 'authorName',
type: 'string', type: 'string',
default: '', default: '',
description: 'Name of the author.', description: 'Name of the author.',
}, },
{ {
displayName: 'Author Link', displayName: 'Author Link',
name: 'authorLink', name: 'authorLink',
type: 'string', type: 'string',
default: '', default: '',
description: 'Providing this makes the author name clickable and points to this link.', description: 'Providing this makes the author name clickable and points to this link.',
}, },
{ {
displayName: 'Author Icon', displayName: 'Author Icon',
name: 'authorIcon', name: 'authorIcon',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'https://site.com/img.png', placeholder: 'https://site.com/img.png',
description: 'Displays a tiny icon to the left of the Authors name.', description: 'Displays a tiny icon to the left of the Authors name.',
}, },
{ {
displayName: 'Title', displayName: 'Title',
name: 'title', name: 'title',
type: 'string', type: 'string',
default: '', default: '',
description: 'Title to display for this attachment, displays under the author.', description: 'Title to display for this attachment, displays under the author.',
}, },
{ {
displayName: 'Title Link', displayName: 'Title Link',
name: 'titleLink', name: 'titleLink',
type: 'string', type: 'string',
default: '', default: '',
description: 'Providing this makes the title clickable, pointing to this link.', description: 'Providing this makes the title clickable, pointing to this link.',
}, },
{ {
displayName: 'Title Link Download', displayName: 'Title Link Download',
name: 'titleLinkDownload', name: 'titleLinkDownload',
type: 'boolean', type: 'boolean',
default: false, default: false,
description: 'When this is true, a download icon appears and clicking this saves the link to file.', description: 'When this is true, a download icon appears and clicking this saves the link to file.',
}, },
{ {
displayName: 'Image URL', displayName: 'Image URL',
name: 'imageUrl', name: 'imageUrl',
type: 'string', type: 'string',
default: '', default: '',
description: 'The image to display, will be “big” and easy to see.', description: 'The image to display, will be “big” and easy to see.',
}, },
{ {
displayName: 'Audio URL', displayName: 'Audio URL',
name: 'audioUrl', name: 'audioUrl',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'https://site.com/aud.mp3', placeholder: 'https://site.com/aud.mp3',
description: 'Audio file to play, only supports what html audio does.', description: 'Audio file to play, only supports what html audio does.',
}, },
{ {
displayName: 'video URL', displayName: 'video URL',
name: 'videoUrl', name: 'videoUrl',
type: 'string', type: 'string',
default: '', default: '',
placeholder: 'https://site.com/vid.mp4', placeholder: 'https://site.com/vid.mp4',
description: 'Video file to play, only supports what html video does.', description: 'Video file to play, only supports what html video does.',
}, },
{ {
displayName: 'Fields', displayName: 'Fields',
name: 'fields', name: 'fields',
type: 'fixedCollection', type: 'fixedCollection',
placeholder: 'Add Field Item', placeholder: 'Add Field Item',
typeOptions: { typeOptions: {
multipleValues: true, multipleValues: true,
}, },
default: '', default: '',
options: [ options: [
{ {
name: 'fieldsValues', name: 'fieldsValues',
displayName: 'Fields', displayName: 'Fields',
values: [ values: [
{ {
displayName: 'Short', displayName: 'Short',
name: 'short', name: 'short',
type: 'boolean', type: 'boolean',
default: false, default: false,
description: 'Whether this field should be a short field.' description: 'Whether this field should be a short field.'
}, },
{ {
displayName: 'Title', displayName: 'Title',
name: 'title', name: 'title',
type: 'string', type: 'string',
default: '', default: '',
description: 'The title of this field.' description: 'The title of this field.'
}, },
{ {
displayName: 'Value', displayName: 'Value',
name: 'value', name: 'value',
type: 'string', type: 'string',
default: '', default: '',
description: 'The value of this field, displayed underneath the title value.' description: 'The value of this field, displayed underneath the title value.'
}, },
], ],
}, },
], ],
}, },
] ]
}, },
{ {
displayName: 'Attachments', displayName: 'Attachments',
name: 'attachmentsJson', name: 'attachmentsJson',
type: 'json', type: 'json',
typeOptions: { typeOptions: {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'chat' 'chat'
], ],
operation: [ operation: [
'postMessage' 'postMessage'
], ],
jsonParameters: [ jsonParameters: [
true true
], ],
}, },
}, },
default: '', default: '',
required: false, required: false,
description: '', description: '',
} }
] ]
}; };
async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> { async executeSingle(this: IExecuteSingleFunctions): Promise<INodeExecutionData> {
const resource = this.getNodeParameter('resource') as string;
const resource = this.getNodeParameter('resource') as string;
const opeation = this.getNodeParameter('operation') as string; const opeation = this.getNodeParameter('operation') as string;
let response; let response;
if (resource === 'chat') { if (resource === 'chat') {
//https://rocket.chat/docs/developer-guides/rest-api/chat/postmessage //https://rocket.chat/docs/developer-guides/rest-api/chat/postmessage
if (opeation === 'postMessage') { if (opeation === 'postMessage') {
const channel = this.getNodeParameter('channel') as string; const channel = this.getNodeParameter('channel') as string;
const alias = this.getNodeParameter('alias') as string; const alias = this.getNodeParameter('alias') as string;
const avatar = this.getNodeParameter('avatar') as string; const avatar = this.getNodeParameter('avatar') as string;
const text = this.getNodeParameter('text') as string; const text = this.getNodeParameter('text') as string;
const options = this.getNodeParameter('options') as IDataObject; const options = this.getNodeParameter('options') as IDataObject;
const jsonActive = this.getNodeParameter('jsonParameters') as boolean; const jsonActive = this.getNodeParameter('jsonParameters') as boolean;
const body: IPostMessageBody = { const body: IPostMessageBody = {
channel, channel,
alias, alias,
avatar, avatar,
text, text,
}; };
if (options.emoji) { if (options.emoji) {
body.emoji = options.emoji as string; body.emoji = options.emoji as string;
} }
if (!jsonActive) { if (!jsonActive) {
const optionsAttachments = this.getNodeParameter('attachments') as IDataObject[]; const optionsAttachments = this.getNodeParameter('attachments') as IDataObject[];
if (optionsAttachments.length > 0) { if (optionsAttachments.length > 0) {
const attachments: IAttachment[] = []; const attachments: IAttachment[] = [];
for (let i = 0; i < optionsAttachments.length; i++) { for (let i = 0; i < optionsAttachments.length; i++) {
const attachment: IAttachment = {}; const attachment: IAttachment = {};
for (const option of Object.keys(optionsAttachments[i])) { for (const option of Object.keys(optionsAttachments[i])) {
if (option === 'color') { if (option === 'color') {
attachment.color = optionsAttachments[i][option] as string; attachment.color = optionsAttachments[i][option] as string;
} else if (option === 'text') { } else if (option === 'text') {
attachment.text = optionsAttachments[i][option] as string; attachment.text = optionsAttachments[i][option] as string;
} else if (option === 'ts') { } else if (option === 'ts') {
attachment.ts = optionsAttachments[i][option] as string; attachment.ts = optionsAttachments[i][option] as string;
} else if (option === 'messageLinks') { } else if (option === 'messageLinks') {
attachment.message_link = optionsAttachments[i][option] as string; attachment.message_link = optionsAttachments[i][option] as string;
} else if (option === 'thumbUrl') { } else if (option === 'thumbUrl') {
attachment.thumb_url = optionsAttachments[i][option] as string; attachment.thumb_url = optionsAttachments[i][option] as string;
} else if (option === 'collapsed') { } else if (option === 'collapsed') {
attachment.collapsed = optionsAttachments[i][option] as boolean; attachment.collapsed = optionsAttachments[i][option] as boolean;
} else if (option === 'authorName') { } else if (option === 'authorName') {
attachment.author_name = optionsAttachments[i][option] as string; attachment.author_name = optionsAttachments[i][option] as string;
} else if (option === 'authorLink') { } else if (option === 'authorLink') {
attachment.author_link = optionsAttachments[i][option] as string; attachment.author_link = optionsAttachments[i][option] as string;
} else if (option === 'authorIcon') { } else if (option === 'authorIcon') {
attachment.author_icon = optionsAttachments[i][option] as string; attachment.author_icon = optionsAttachments[i][option] as string;
} else if (option === 'title') { } else if (option === 'title') {
attachment.title = optionsAttachments[i][option] as string; attachment.title = optionsAttachments[i][option] as string;
} else if (option === 'titleLink') { } else if (option === 'titleLink') {
attachment.title_link = optionsAttachments[i][option] as string; attachment.title_link = optionsAttachments[i][option] as string;
} else if (option === 'titleLinkDownload') { } else if (option === 'titleLinkDownload') {
attachment.title_link_download = optionsAttachments[i][option] as boolean; attachment.title_link_download = optionsAttachments[i][option] as boolean;
} else if (option === 'imageUrl') { } else if (option === 'imageUrl') {
attachment.image_url = optionsAttachments[i][option] as string; attachment.image_url = optionsAttachments[i][option] as string;
} else if (option === 'audioUrl') { } else if (option === 'audioUrl') {
attachment.audio_url = optionsAttachments[i][option] as string; attachment.audio_url = optionsAttachments[i][option] as string;
} else if (option === 'videoUrl') { } else if (option === 'videoUrl') {
attachment.video_url = optionsAttachments[i][option] as string; attachment.video_url = optionsAttachments[i][option] as string;
} else if (option === 'fields') { } else if (option === 'fields') {
const fieldsValues = (optionsAttachments[i][option] as IDataObject).fieldsValues as IDataObject[]; const fieldsValues = (optionsAttachments[i][option] as IDataObject).fieldsValues as IDataObject[];
if (fieldsValues.length > 0) { if (fieldsValues.length > 0) {
const fields: IField[] = []; const fields: IField[] = [];
for (let i = 0; i < fieldsValues.length; i++) { for (let i = 0; i < fieldsValues.length; i++) {
const field: IField = {}; const field: IField = {};
for (const key of Object.keys(fieldsValues[i])) { for (const key of Object.keys(fieldsValues[i])) {
if (key === 'short') { if (key === 'short') {
field.short = fieldsValues[i][key] as boolean; field.short = fieldsValues[i][key] as boolean;
} else if (key === 'title') { } else if (key === 'title') {
field.title = fieldsValues[i][key] as string; field.title = fieldsValues[i][key] as string;
} else if (key === 'value') { } else if (key === 'value') {
field.value = fieldsValues[i][key] as string; field.value = fieldsValues[i][key] as string;
} }
} }
fields.push(field); fields.push(field);
attachment.fields = fields; attachment.fields = fields;
} }
} }
} }
} }
attachments.push(attachment); attachments.push(attachment);
} }
body.attachments = attachments; body.attachments = attachments;
} }
} else { } else {
body.attachments = validateJSON(this.getNodeParameter('attachmentsJson') as string); body.attachments = validateJSON(this.getNodeParameter('attachmentsJson') as string);
} }
try { try {
response = await rocketchatApiRequest.call(this, '/chat', 'POST', 'postMessage', body); response = await rocketchatApiRequest.call(this, '/chat', 'POST', 'postMessage', body);
} catch (err) { } catch (err) {
throw new Error(`Rocketchat Error: ${err}`); throw new Error(`Rocketchat Error: ${err}`);
} }
} }
} }
return { return {
json: response json: response
}; };
} }
} }