fix(Gmail Trigger Node): Prevent error for empty emails, improve type safety (#13171)

This commit is contained in:
Elias Meire 2025-02-11 12:45:55 +01:00 committed by GitHub
parent 4c19baea3d
commit 115a367cae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 267 additions and 73 deletions

View file

@ -1,12 +1,12 @@
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import type { import type {
IPollFunctions,
IDataObject, IDataObject,
ILoadOptionsFunctions, ILoadOptionsFunctions,
INodeExecutionData, INodeExecutionData,
INodePropertyOptions, INodePropertyOptions,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
IPollFunctions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { NodeConnectionType } from 'n8n-workflow'; import { NodeConnectionType } from 'n8n-workflow';
@ -17,6 +17,15 @@ import {
prepareQuery, prepareQuery,
simplifyOutput, simplifyOutput,
} from './GenericFunctions'; } from './GenericFunctions';
import type {
GmailTriggerFilters,
GmailTriggerOptions,
GmailWorkflowStaticData,
GmailWorkflowStaticDataDictionary,
Label,
Message,
MessageListResponse,
} from './types';
export class GmailTrigger implements INodeType { export class GmailTrigger implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -206,12 +215,12 @@ export class GmailTrigger implements INodeType {
async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> { async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
const labels = await googleApiRequestAllItems.call( const labels = (await googleApiRequestAllItems.call(
this, this,
'labels', 'labels',
'GET', 'GET',
'/gmail/v1/users/me/labels', '/gmail/v1/users/me/labels',
); )) as Label[];
for (const label of labels) { for (const label of labels) {
returnData.push({ returnData.push({
@ -234,50 +243,53 @@ export class GmailTrigger implements INodeType {
}; };
async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> { async poll(this: IPollFunctions): Promise<INodeExecutionData[][] | null> {
const workflowStaticData = this.getWorkflowStaticData('node'); const workflowStaticData = this.getWorkflowStaticData('node') as
| GmailWorkflowStaticData
| GmailWorkflowStaticDataDictionary;
const node = this.getNode(); const node = this.getNode();
let nodeStaticData = workflowStaticData; let nodeStaticData = (workflowStaticData ?? {}) as GmailWorkflowStaticData;
if (node.typeVersion > 1) { if (node.typeVersion > 1) {
const nodeName = node.name; const nodeName = node.name;
if (workflowStaticData[nodeName] === undefined) { const dictionary = workflowStaticData as GmailWorkflowStaticDataDictionary;
workflowStaticData[nodeName] = {} as IDataObject; if (!(nodeName in workflowStaticData)) {
nodeStaticData = workflowStaticData[nodeName] as IDataObject; dictionary[nodeName] = {};
} else {
nodeStaticData = workflowStaticData[nodeName] as IDataObject;
} }
nodeStaticData = dictionary[nodeName];
} }
let responseData;
const now = Math.floor(DateTime.now().toSeconds()).toString(); const now = Math.floor(DateTime.now().toSeconds()).toString();
const startDate = (nodeStaticData.lastTimeChecked as string) || +now; const startDate = nodeStaticData.lastTimeChecked ?? +now;
const endDate = +now; const endDate = +now;
const options = this.getNodeParameter('options', {}) as IDataObject; const options = this.getNodeParameter('options', {}) as GmailTriggerOptions;
const filters = this.getNodeParameter('filters', {}) as IDataObject; const filters = this.getNodeParameter('filters', {}) as GmailTriggerFilters;
let responseData: INodeExecutionData[] = [];
try { try {
const qs: IDataObject = {}; const qs: IDataObject = {};
filters.receivedAfter = startDate; const allFilters: GmailTriggerFilters = { ...filters, receivedAfter: startDate };
if (this.getMode() === 'manual') { if (this.getMode() === 'manual') {
qs.maxResults = 1; qs.maxResults = 1;
delete filters.receivedAfter; delete allFilters.receivedAfter;
} }
Object.assign(qs, prepareQuery.call(this, filters, 0), options); Object.assign(qs, prepareQuery.call(this, allFilters, 0), options);
responseData = await googleApiRequest.call( const messagesResponse: MessageListResponse = await googleApiRequest.call(
this, this,
'GET', 'GET',
'/gmail/v1/users/me/messages', '/gmail/v1/users/me/messages',
{}, {},
qs, qs,
); );
responseData = responseData.messages;
if (!responseData?.length) { const messages = messagesResponse.messages ?? [];
if (!messages.length) {
nodeStaticData.lastTimeChecked = endDate; nodeStaticData.lastTimeChecked = endDate;
return null; return null;
} }
@ -291,48 +303,47 @@ export class GmailTrigger implements INodeType {
qs.format = 'raw'; qs.format = 'raw';
} }
let includeDrafts; let includeDrafts = false;
if (node.typeVersion > 1.1) { if (node.typeVersion > 1.1) {
includeDrafts = (qs.includeDrafts as boolean) ?? false; includeDrafts = filters.includeDrafts ?? false;
} else { } else {
includeDrafts = (qs.includeDrafts as boolean) ?? true; includeDrafts = filters.includeDrafts ?? true;
} }
delete qs.includeDrafts;
const withoutDrafts = [];
for (let i = 0; i < responseData.length; i++) { delete qs.includeDrafts;
responseData[i] = await googleApiRequest.call(
for (const message of messages) {
const fullMessage = (await googleApiRequest.call(
this, this,
'GET', 'GET',
`/gmail/v1/users/me/messages/${responseData[i].id}`, `/gmail/v1/users/me/messages/${message.id}`,
{}, {},
qs, qs,
); )) as Message;
if (!includeDrafts) { if (!includeDrafts) {
if (responseData[i].labelIds.includes('DRAFT')) { if (fullMessage.labelIds?.includes('DRAFT')) {
continue; continue;
} }
} }
if (!simple && responseData?.length) {
if (!simple) {
const dataPropertyNameDownload = const dataPropertyNameDownload =
(options.dataPropertyAttachmentsPrefixName as string) || 'attachment_'; options.dataPropertyAttachmentsPrefixName || 'attachment_';
responseData[i] = await parseRawEmail.call( const parsed = await parseRawEmail.call(this, fullMessage, dataPropertyNameDownload);
this, responseData.push(parsed);
responseData[i], } else {
dataPropertyNameDownload, responseData.push({ json: fullMessage });
);
} }
withoutDrafts.push(responseData[i]);
} }
if (!includeDrafts) { if (simple) {
responseData = withoutDrafts;
}
if (simple && responseData?.length) {
responseData = this.helpers.returnJsonArray( responseData = this.helpers.returnJsonArray(
await simplifyOutput.call(this, responseData as IDataObject[]), await simplifyOutput.call(
this,
responseData.map((item) => item.json),
),
); );
} }
} catch (error) { } catch (error) {
@ -349,52 +360,49 @@ export class GmailTrigger implements INodeType {
}, },
); );
} }
if (!responseData?.length) { if (!responseData.length) {
nodeStaticData.lastTimeChecked = endDate; nodeStaticData.lastTimeChecked = endDate;
return null; return null;
} }
const emailsWithInvalidDate = new Set<string>(); const emailsWithInvalidDate = new Set<string>();
const getEmailDateAsSeconds = (email: IDataObject): number => {
const getEmailDateAsSeconds = (email: Message): number => {
let date; let date;
if (email.internalDate) { if (email.internalDate) {
date = +(email.internalDate as string) / 1000; date = +email.internalDate / 1000;
} else if (email.date) { } else if (email.date) {
date = +DateTime.fromJSDate(new Date(email.date as string)).toSeconds(); date = +DateTime.fromJSDate(new Date(email.date)).toSeconds();
} else { } else if (email.headers?.date) {
date = +DateTime.fromJSDate( date = +DateTime.fromJSDate(new Date(email.headers.date)).toSeconds();
new Date((email?.headers as IDataObject)?.date as string),
).toSeconds();
} }
if (!date || isNaN(date)) { if (!date || isNaN(date)) {
emailsWithInvalidDate.add(email.id as string); emailsWithInvalidDate.add(email.id);
return +startDate; return +startDate;
} }
return date; return date;
}; };
const lastEmailDate = (responseData as IDataObject[]).reduce((lastDate, { json }) => { const lastEmailDate = responseData.reduce((lastDate, { json }) => {
const emailDate = getEmailDateAsSeconds(json as IDataObject); const emailDate = getEmailDateAsSeconds(json as Message);
return emailDate > lastDate ? emailDate : lastDate; return emailDate > lastDate ? emailDate : lastDate;
}, 0); }, 0);
const nextPollPossibleDuplicates = (responseData as IDataObject[]).reduce( const nextPollPossibleDuplicates = responseData
(duplicates, { json }) => { .filter((item) => item.json)
const emailDate = getEmailDateAsSeconds(json as IDataObject); .reduce((duplicates, { json }) => {
return emailDate <= lastEmailDate const emailDate = getEmailDateAsSeconds(json as Message);
? duplicates.concat((json as IDataObject).id as string) return emailDate <= lastEmailDate ? duplicates.concat((json as Message).id) : duplicates;
: duplicates; }, Array.from(emailsWithInvalidDate));
},
Array.from(emailsWithInvalidDate),
);
const possibleDuplicates = (nodeStaticData.possibleDuplicates as string[]) || []; const possibleDuplicates = new Set(nodeStaticData.possibleDuplicates ?? []);
if (possibleDuplicates.length) { if (possibleDuplicates.size > 0) {
responseData = (responseData as IDataObject[]).filter(({ json }) => { responseData = responseData.filter(({ json }) => {
const { id } = json as IDataObject; if (!json || typeof json.id !== 'string') return false;
return !possibleDuplicates.includes(id as string); return !possibleDuplicates.has(json.id);
}); });
} }
@ -402,7 +410,7 @@ export class GmailTrigger implements INodeType {
nodeStaticData.lastTimeChecked = lastEmailDate || endDate; nodeStaticData.lastTimeChecked = lastEmailDate || endDate;
if (Array.isArray(responseData) && responseData.length) { if (Array.isArray(responseData) && responseData.length) {
return [responseData as INodeExecutionData[]]; return [responseData];
} }
return null; return null;

View file

@ -212,4 +212,63 @@ describe('GmailTrigger', () => {
expect(response).toEqual(null); expect(response).toEqual(null);
}); });
it('should handle duplicates and different date fields', async () => {
const messageListResponse: MessageListResponse = {
messages: [
createListMessage({ id: '1' }),
createListMessage({ id: '2' }),
createListMessage({ id: '3' }),
createListMessage({ id: '4' }),
createListMessage({ id: '5' }),
],
resultSizeEstimate: 123,
};
nock(baseUrl)
.get('/gmail/v1/users/me/labels')
.reply(200, { labels: [{ id: 'testLabelId', name: 'Test Label Name' }] });
nock(baseUrl).get(new RegExp('/gmail/v1/users/me/messages?.*')).reply(200, messageListResponse);
nock(baseUrl)
.get(new RegExp('/gmail/v1/users/me/messages/1?.*'))
.reply(200, createMessage({ id: '1', internalDate: '1727777957863', date: undefined }));
nock(baseUrl)
.get(new RegExp('/gmail/v1/users/me/messages/2?.*'))
.reply(200, createMessage({ id: '2', internalDate: undefined, date: '1727777957863' }));
nock(baseUrl)
.get(new RegExp('/gmail/v1/users/me/messages/3?.*'))
.reply(
200,
createMessage({
id: '3',
internalDate: undefined,
date: undefined,
headers: { date: 'Thu, 5 Dec 2024 08:30:00 -0800' },
}),
);
nock(baseUrl)
.get(new RegExp('/gmail/v1/users/me/messages/4?.*'))
.reply(
200,
createMessage({
id: '4',
internalDate: undefined,
date: undefined,
headers: undefined,
}),
);
nock(baseUrl).get(new RegExp('/gmail/v1/users/me/messages/5?.*')).reply(200, {});
const { response } = await testPollingTriggerNode(GmailTrigger, {
node: { parameters: { simple: true } },
workflowStaticData: {
'Gmail Trigger': {
lastTimeChecked: new Date('2024-10-31').getTime() / 1000,
possibleDuplicates: ['1'],
},
},
});
expect(response).toMatchSnapshot();
});
}); });

View file

@ -0,0 +1,96 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`GmailTrigger should handle duplicates and different date fields 1`] = `
[
[
{
"json": {
"date": "1727777957863",
"historyId": "testHistoryId",
"id": "2",
"labels": [
{
"id": "testLabelId",
"name": "Test Label Name",
},
],
"payload": {
"body": {
"attachmentId": "testAttachmentId",
"data": "dGVzdA==",
"size": 4,
},
"filename": "foo.txt",
"mimeType": "text/plain",
"partId": "testPartId",
"parts": [],
},
"raw": "dGVzdA==",
"sizeEstimate": 4,
"snippet": "test",
"testHeader": "testHeaderValue",
"threadId": "testThreadId",
},
},
{
"json": {
"headers": {
"date": "Thu, 5 Dec 2024 08:30:00 -0800",
},
"historyId": "testHistoryId",
"id": "3",
"labels": [
{
"id": "testLabelId",
"name": "Test Label Name",
},
],
"payload": {
"body": {
"attachmentId": "testAttachmentId",
"data": "dGVzdA==",
"size": 4,
},
"filename": "foo.txt",
"mimeType": "text/plain",
"partId": "testPartId",
"parts": [],
},
"raw": "dGVzdA==",
"sizeEstimate": 4,
"snippet": "test",
"testHeader": "testHeaderValue",
"threadId": "testThreadId",
},
},
{
"json": {
"historyId": "testHistoryId",
"id": "4",
"labels": [
{
"id": "testLabelId",
"name": "Test Label Name",
},
],
"payload": {
"body": {
"attachmentId": "testAttachmentId",
"data": "dGVzdA==",
"size": 4,
},
"filename": "foo.txt",
"mimeType": "text/plain",
"partId": "testPartId",
"parts": [],
},
"raw": "dGVzdA==",
"sizeEstimate": 4,
"snippet": "test",
"testHeader": "testHeaderValue",
"threadId": "testThreadId",
},
},
],
]
`;

View file

@ -4,7 +4,9 @@ export type Message = {
labelIds: string[]; labelIds: string[];
snippet: string; snippet: string;
historyId: string; historyId: string;
internalDate: string; date?: string;
headers?: Record<string, string>;
internalDate?: string;
sizeEstimate: number; sizeEstimate: number;
raw: string; raw: string;
payload: MessagePart; payload: MessagePart;
@ -13,7 +15,7 @@ export type Message = {
export type ListMessage = Pick<Message, 'id' | 'threadId'>; export type ListMessage = Pick<Message, 'id' | 'threadId'>;
export type MessageListResponse = { export type MessageListResponse = {
messages: ListMessage[]; messages?: ListMessage[];
nextPageToken?: string; nextPageToken?: string;
resultSizeEstimate: number; resultSizeEstimate: number;
}; };
@ -37,3 +39,32 @@ type MessagePartBody = {
size: number; size: number;
data: string; data: string;
}; };
export type Label = {
id: string;
name: string;
messageListVisibility?: 'hide';
labelListVisibility?: 'labelHide';
type?: 'system';
};
export type GmailWorkflowStaticData = {
lastTimeChecked?: number;
possibleDuplicates?: string[];
};
export type GmailWorkflowStaticDataDictionary = Record<string, GmailWorkflowStaticData>;
export type GmailTriggerOptions = Partial<{
dataPropertyAttachmentsPrefixName: string;
downloadAttachments: boolean;
}>;
export type GmailTriggerFilters = Partial<{
sender: string;
q: string;
includeSpamTrash: boolean;
includeDrafts: boolean;
readStatus: 'read' | 'unread' | 'both';
labelIds: string[];
receivedAfter: number;
}>;