feat(Email Trigger (IMAP) Node): Option to disable last message id tracking (#17964)

This commit is contained in:
Michael Kret
2025-08-07 14:59:34 +03:00
committed by GitHub
parent c2fe529d2b
commit 25379fe522
2 changed files with 26 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
import { mock } from 'jest-mock-extended';
import { type INodeTypeBaseDescription, type ITriggerFunctions } from 'n8n-workflow';
import type { INode, INodeTypeBaseDescription, ITriggerFunctions } from 'n8n-workflow';
import { type ICredentialsDataImap } from '@credentials/Imap.credentials';
@@ -42,6 +42,7 @@ describe('Test IMap V2', () => {
};
triggerFunctions.getCredentials.calledWith('imap').mockResolvedValue(credentials);
triggerFunctions.getNode.mockReturnValue(mock<INode>({ typeVersion: 2.1 }));
triggerFunctions.logger.debug = jest.fn();
triggerFunctions.getNodeParameter.calledWith('options').mockReturnValue({
name: 'Mark as Read',

View File

@@ -177,6 +177,19 @@ const versionDescription: INodeTypeDescription = {
default: 60,
description: 'Sets an interval (in minutes) to force a reconnection',
},
{
displayName: 'Fetch Only New Emails',
name: 'trackLastMessageId',
type: 'boolean',
default: true,
description:
'Whether to fetch only new emails since the last run, or all emails that match the "Custom Email Rules" (["UNSEEN"] by default)',
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 2.1 } }],
},
},
},
],
},
],
@@ -247,6 +260,7 @@ export class EmailReadImapV2 implements INodeType {
};
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
const node = this.getNode();
const credentialsObject = await this.getCredentials('imap');
const credentials = isCredentialsDataImap(credentialsObject) ? credentialsObject : undefined;
if (!credentials) {
@@ -258,6 +272,15 @@ export class EmailReadImapV2 implements INodeType {
const activatedAt = DateTime.now();
const staticData = this.getWorkflowStaticData('node');
if (node.typeVersion <= 2) {
// before v 2.1 staticData.lastMessageUid was never set, preserve that behavior
staticData.lastMessageUid = undefined;
}
if (options.trackLastMessageId === false) {
staticData.lastMessageUid = undefined;
}
this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData });
let connection: ImapSimple;
@@ -387,7 +410,7 @@ export class EmailReadImapV2 implements INodeType {
* by checking UIDValidity.
*/
searchCriteria.push(['UID', `${staticData.lastMessageUid as number}:*`]);
} else {
} else if (node.typeVersion > 2 && options.trackLastMessageId !== false) {
searchCriteria.push(['SINCE', activatedAt.toFormat('dd-LLL-yyyy')]);
}