Skip to content

Commit

Permalink
Refactor getNewEmais
Browse files Browse the repository at this point in the history
  • Loading branch information
dana-gill committed Feb 14, 2025
1 parent ba95f97 commit d4625c2
Showing 1 changed file with 10 additions and 166 deletions.
176 changes: 10 additions & 166 deletions packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import rfc2047 from 'rfc2047';

import type { ICredentialsDataImap } from '../../../credentials/Imap.credentials';
import { isCredentialsDataImap } from '../../../credentials/Imap.credentials';
import { getNewEmails } from './utils';

export async function parseRawEmail(
this: ITriggerFunctions,
Expand Down Expand Up @@ -369,171 +370,6 @@ export class EmailReadImapV2 implements INodeType {
return await Promise.all(attachmentPromises);
};

// Returns all the new unseen messages
const getNewEmails = async (
imapConnection: ImapSimple,
searchCriteria: Array<string | string[]>,
): Promise<INodeExecutionData[]> => {
const format = this.getNodeParameter('format', 0) as string;

let fetchOptions = {};

if (format === 'simple' || format === 'raw') {
fetchOptions = {
bodies: ['TEXT', 'HEADER'],
markSeen: false,
struct: true,
};
} else if (format === 'resolved') {
fetchOptions = {
bodies: [''],
markSeen: false,
struct: true,
};
}

const results = await imapConnection.search(searchCriteria, fetchOptions);

const newEmails: INodeExecutionData[] = [];
let newEmail: INodeExecutionData;
let attachments: IBinaryData[];
let propertyName: string;

// All properties get by default moved to metadata except the ones
// which are defined here which get set on the top level.
const topLevelProperties = ['cc', 'date', 'from', 'subject', 'to'];

if (format === 'resolved') {
const dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;

for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const part = find(message.parts, { which: '' });

if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
const parsedEmail = await parseRawEmail.call(
this,
part.body as Buffer,
dataPropertyAttachmentsPrefixName,
);

newEmails.push(parsedEmail);
}
} else if (format === 'simple') {
const downloadAttachments = this.getNodeParameter('downloadAttachments') as boolean;

let dataPropertyAttachmentsPrefixName = '';
if (downloadAttachments) {
dataPropertyAttachmentsPrefixName = this.getNodeParameter(
'dataPropertyAttachmentsPrefixName',
) as string;
}

for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const parts = getParts(message.attributes.struct as IDataObject[]);

newEmail = {
json: {
textHtml: await getText(parts, message, 'html'),
textPlain: await getText(parts, message, 'plain'),
metadata: {} as IDataObject,
},
};

const messageHeader = message.parts.filter((part) => part.which === 'HEADER');

const messageBody = messageHeader[0].body as Record<string, string[]>;
for (propertyName of Object.keys(messageBody)) {
if (messageBody[propertyName].length) {
if (topLevelProperties.includes(propertyName)) {
newEmail.json[propertyName] = messageBody[propertyName][0];
} else {
(newEmail.json.metadata as IDataObject)[propertyName] =
messageBody[propertyName][0];
}
}
}

if (downloadAttachments) {
// Get attachments and add them if any get found
attachments = await getAttachment(imapConnection, parts, message);
if (attachments.length) {
newEmail.binary = {};
for (let i = 0; i < attachments.length; i++) {
newEmail.binary[`${dataPropertyAttachmentsPrefixName}${i}`] = attachments[i];
}
}
}

newEmails.push(newEmail);
}
} else if (format === 'raw') {
for (const message of results) {
if (
staticData.lastMessageUid !== undefined &&
message.attributes.uid <= (staticData.lastMessageUid as number)
) {
continue;
}
if (
staticData.lastMessageUid === undefined ||
(staticData.lastMessageUid as number) < message.attributes.uid
) {
staticData.lastMessageUid = message.attributes.uid;
}
const part = find(message.parts, { which: 'TEXT' });

if (part === undefined) {
throw new NodeOperationError(this.getNode(), 'Email part could not be parsed.');
}
// Return base64 string
newEmail = {
json: {
raw: part.body as string,
},
};

newEmails.push(newEmail);
}
}

// only mark messages as seen once processing has finished
if (postProcessAction === 'read') {
const uidList = results.map((e) => e.attributes.uid);
if (uidList.length > 0) {
await imapConnection.addFlags(uidList, '\\SEEN');
}
}
return newEmails;
};

const returnedPromise = this.helpers.createDeferredPromise();

const establishConnection = async (): Promise<ImapSimple> => {
Expand Down Expand Up @@ -579,7 +415,15 @@ export class EmailReadImapV2 implements INodeType {
}

try {
const returnData = await getNewEmails(connection, searchCriteria);
const returnData = await getNewEmails.call(
this,
connection,
searchCriteria,
staticData,
postProcessAction,
getText,
getAttachment,
);
if (returnData.length) {
this.emit([returnData]);
}
Expand Down

0 comments on commit d4625c2

Please sign in to comment.