mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ Improve changes to IMAP Node
This commit is contained in:
parent
534b852fc2
commit
0ea3411f5d
|
@ -30,8 +30,7 @@ export class EmailReadImap implements INodeType {
|
||||||
color: '#44AA22',
|
color: '#44AA22',
|
||||||
},
|
},
|
||||||
inputs: [],
|
inputs: [],
|
||||||
outputs: ['main', 'main'],
|
outputs: ['main'],
|
||||||
outputNames: ['data', 'error'],
|
|
||||||
credentials: [
|
credentials: [
|
||||||
{
|
{
|
||||||
name: 'imap',
|
name: 'imap',
|
||||||
|
@ -131,27 +130,6 @@ export class EmailReadImap implements INodeType {
|
||||||
},
|
},
|
||||||
description: 'Prefix for name of the binary property to which to<br />write the attachments. An index starting with 0 will be added.<br />So if name is "attachment_" the first attachment is saved to "attachment_0"',
|
description: 'Prefix for name of the binary property to which to<br />write the attachments. An index starting with 0 will be added.<br />So if name is "attachment_" the first attachment is saved to "attachment_0"',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
displayName: 'Use custom email config',
|
|
||||||
name: 'useCustomEmailConfig',
|
|
||||||
type: 'boolean',
|
|
||||||
default: false,
|
|
||||||
description: 'If custom email rules should be used.',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
displayName: 'Custom email rules',
|
|
||||||
name: 'customEmailConfig',
|
|
||||||
type: 'string',
|
|
||||||
default: "['UNSEEN']",
|
|
||||||
displayOptions: {
|
|
||||||
show: {
|
|
||||||
useCustomEmailConfig: [
|
|
||||||
true
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
description: 'Custom email fetching rules. See <a href="https://github.com/mscdex/node-imap">node-imap</a>\'s search function for more details'
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
displayName: 'Options',
|
displayName: 'Options',
|
||||||
name: 'options',
|
name: 'options',
|
||||||
|
@ -159,6 +137,13 @@ export class EmailReadImap implements INodeType {
|
||||||
placeholder: 'Add Option',
|
placeholder: 'Add Option',
|
||||||
default: {},
|
default: {},
|
||||||
options: [
|
options: [
|
||||||
|
{
|
||||||
|
displayName: 'Custom email rules',
|
||||||
|
name: 'customEmailConfig',
|
||||||
|
type: 'string',
|
||||||
|
default: '["UNSEEN"]',
|
||||||
|
description: 'Custom email fetching rules. See <a href="https://github.com/mscdex/node-imap">node-imap</a>\'s search function for more details'
|
||||||
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Ignore SSL Issues',
|
displayName: 'Ignore SSL Issues',
|
||||||
name: 'allowUnauthorizedCerts',
|
name: 'allowUnauthorizedCerts',
|
||||||
|
@ -184,6 +169,16 @@ export class EmailReadImap implements INodeType {
|
||||||
const postProcessAction = this.getNodeParameter('postProcessAction') as string;
|
const postProcessAction = this.getNodeParameter('postProcessAction') as string;
|
||||||
const options = this.getNodeParameter('options', {}) as IDataObject;
|
const options = this.getNodeParameter('options', {}) as IDataObject;
|
||||||
|
|
||||||
|
let searchCriteria = [
|
||||||
|
'UNSEEN'
|
||||||
|
];
|
||||||
|
if (options.customEmailConfig !== undefined) {
|
||||||
|
try {
|
||||||
|
searchCriteria = JSON.parse(options.customEmailConfig as string);
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error(`Custom email config is not valid JSON.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns the email text
|
// Returns the email text
|
||||||
const getText = async (parts: any[], message: Message, subtype: string) => { // tslint:disable-line:no-any
|
const getText = async (parts: any[], message: Message, subtype: string) => { // tslint:disable-line:no-any
|
||||||
|
@ -235,22 +230,9 @@ export class EmailReadImap implements INodeType {
|
||||||
|
|
||||||
|
|
||||||
// Returns all the new unseen messages
|
// Returns all the new unseen messages
|
||||||
const getNewEmails = async (connection: ImapSimple): Promise<INodeExecutionData[]> => {
|
const getNewEmails = async (connection: ImapSimple, searchCriteria: string[]): Promise<INodeExecutionData[]> => {
|
||||||
const format = this.getNodeParameter('format', 0) as string;
|
const format = this.getNodeParameter('format', 0) as string;
|
||||||
|
|
||||||
let searchCriteria = [
|
|
||||||
'UNSEEN'
|
|
||||||
];
|
|
||||||
const useCustomEmailConfig = this.getNodeParameter('useCustomEmailConfig') as boolean;
|
|
||||||
if (useCustomEmailConfig) {
|
|
||||||
const customEmailConfig = this.getNodeParameter('customEmailConfig') as string;
|
|
||||||
try {
|
|
||||||
searchCriteria = eval(customEmailConfig);
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Parsing of ${customEmailConfig}\nfailed with error ${err}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let fetchOptions = {};
|
let fetchOptions = {};
|
||||||
|
|
||||||
if (format === 'simple' || format === 'raw') {
|
if (format === 'simple' || format === 'raw') {
|
||||||
|
@ -274,8 +256,6 @@ export class EmailReadImap implements INodeType {
|
||||||
let attachments: IBinaryData[];
|
let attachments: IBinaryData[];
|
||||||
let propertyName: string;
|
let propertyName: string;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// All properties get by default moved to metadata except the ones
|
// All properties get by default moved to metadata except the ones
|
||||||
// which are defined here which get set on the top level.
|
// which are defined here which get set on the top level.
|
||||||
const topLevelProperties = [
|
const topLevelProperties = [
|
||||||
|
@ -367,22 +347,7 @@ export class EmailReadImap implements INodeType {
|
||||||
return newEmails;
|
return newEmails;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let connection: ImapSimple;
|
let connection: ImapSimple;
|
||||||
let empty: INodeExecutionData[] = [];
|
|
||||||
let errToJson = (err: Error) => {
|
|
||||||
return {
|
|
||||||
json:
|
|
||||||
{
|
|
||||||
message: err.message,
|
|
||||||
stack: err.stack
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
let emitError = (err: Error) => {
|
|
||||||
this.emit([empty, [errToJson(err)]]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const config: ImapSimpleOptions = {
|
const config: ImapSimpleOptions = {
|
||||||
imap: {
|
imap: {
|
||||||
|
@ -394,14 +359,10 @@ export class EmailReadImap implements INodeType {
|
||||||
authTimeout: 3000
|
authTimeout: 3000
|
||||||
},
|
},
|
||||||
onmail: async () => {
|
onmail: async () => {
|
||||||
try{
|
const returnData = await getNewEmails(connection, searchCriteria);
|
||||||
const returnData = await getNewEmails(connection);
|
|
||||||
|
|
||||||
if (returnData.length) {
|
if (returnData.length) {
|
||||||
this.emit([returnData, empty]);
|
this.emit([returnData]);
|
||||||
}
|
|
||||||
}catch(e) {
|
|
||||||
emitError(e);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -414,12 +375,8 @@ export class EmailReadImap implements INodeType {
|
||||||
|
|
||||||
// Connect to the IMAP server and open the mailbox
|
// Connect to the IMAP server and open the mailbox
|
||||||
// that we get informed whenever a new email arrives
|
// that we get informed whenever a new email arrives
|
||||||
try {
|
|
||||||
connection = await imapConnect(config);
|
connection = await imapConnect(config);
|
||||||
await connection.openBox(mailbox);
|
await connection.openBox(mailbox);
|
||||||
} catch (e) {
|
|
||||||
emitError(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// When workflow and so node gets set to inactive close the connectoin
|
// When workflow and so node gets set to inactive close the connectoin
|
||||||
async function closeFunction() {
|
async function closeFunction() {
|
||||||
|
|
Loading…
Reference in a new issue