fix(Gmail Node): Set References and In-Reply-To only when user provides threadId (#16838)

This commit is contained in:
Dana
2025-07-11 10:08:15 +02:00
committed by GitHub
parent 810f5daa16
commit 7657cce5a4
5 changed files with 40 additions and 22 deletions

View File

@@ -175,7 +175,7 @@ describe('Test Gmail Node v2', () => {
mail mail
.replace(/boundary=".*"/g, 'boundary="--test-boundary"') .replace(/boundary=".*"/g, 'boundary="--test-boundary"')
.replace(/----.*/g, '----test-boundary') .replace(/----.*/g, '----test-boundary')
.replace(/Message-ID:.*/g, 'Message-ID: test-message-id'), .replace(/Message-ID:.*/g, 'Message-ID: <test-message-id@mail.com>'),
'utf-8', 'utf-8',
).toString('base64'); ).toString('base64');
@@ -184,11 +184,10 @@ describe('Test Gmail Node v2', () => {
return body; return body;
} }
}) })
.post('/v1/users/me/drafts', { .post('/v1/users/me/drafts', (body) => {
message: { return (
raw: 'Q29udGVudC1UeXBlOiBtdWx0aXBhcnQvbWl4ZWQ7IGJvdW5kYXJ5PSItLXRlc3QtYm91bmRhcnkiDQpGcm9tOiB0ZXN0LWFsaWFzQG44bi5pbw0KVG86IHRlc3QtdG9AbjhuLmlvDQpDYzogdGVzdC1jY0BuOG4uaW8NCkJjYzogdGVzdC1iY2NAbjhuLmlvDQpSZXBseS1UbzogdGVzdC1yZXBseUBuOG4uaW8NClN1YmplY3Q6IFRlc3QgRHJhZnQgU3ViamVjdA0KTWVzc2FnZS1JRDogdGVzdC1tZXNzYWdlLWlkDQpEYXRlOiBNb24sIDE2IERlYyAyMDI0IDEyOjM0OjU2ICswMDAwDQpNSU1FLVZlcnNpb246IDEuMA0KDQotLS0tdGVzdC1ib3VuZGFyeQ0KQ29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PXV0Zi04DQpDb250ZW50LVRyYW5zZmVyLUVuY29kaW5nOiA3Yml0DQoNClRlc3QgRHJhZnQgTWVzc2FnZQ0KLS0tLXRlc3QtYm91bmRhcnkNCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vanNvbjsgbmFtZT1maWxlLmpzb24NCkNvbnRlbnQtVHJhbnNmZXItRW5jb2Rpbmc6IGJhc2U2NA0KQ29udGVudC1EaXNwb3NpdGlvbjogYXR0YWNobWVudDsgZmlsZW5hbWU9ZmlsZS5qc29uDQoNClczc2lZbWx1WVhKNUlqcDBjblZsZlYwPQ0KLS0tLXRlc3QtYm91bmRhcnkNCg==', typeof body.message?.raw === 'string' && body.message.threadId === 'test-thread-id'
threadId: 'test-thread-id', );
},
}) })
.query({ userId: 'me', uploadType: 'media' }) .query({ userId: 'me', uploadType: 'media' })
.reply(200, messages[0]); .reply(200, messages[0]);
@@ -200,7 +199,9 @@ describe('Test Gmail Node v2', () => {
metadataHeaders: 'Message-ID', metadataHeaders: 'Message-ID',
}) })
.reply(200, { .reply(200, {
messages: [{ payload: { headers: ['jjkjkjkf@reply.com'] } }], messages: [
{ payload: { headers: [{ name: 'Message-ID', value: '<test-message-id@mail.com>' }] } },
],
}); });
gmailNock gmailNock
.get('/v1/users/me/drafts/test-draft-id') .get('/v1/users/me/drafts/test-draft-id')

View File

@@ -143,8 +143,8 @@ describe('addThreadHeadersToEmail', () => {
const mockMessageId = '<message-id@example.com>'; const mockMessageId = '<message-id@example.com>';
const mockThread = { const mockThread = {
messages: [ messages: [
{ payload: { headers: [{ value: '<old-id@example.com>' }] } }, { payload: { headers: [{ name: 'Message-ID', value: '<old-id@example.com>' }] } },
{ payload: { headers: [{ value: mockMessageId }] } }, { payload: { headers: [{ name: 'Message-ID', value: mockMessageId }] } },
], ],
}; };
@@ -159,14 +159,14 @@ describe('addThreadHeadersToEmail', () => {
await addThreadHeadersToEmail.call(thisArg, email, mockThreadId); await addThreadHeadersToEmail.call(thisArg, email, mockThreadId);
expect(email.inReplyTo).toBe(mockMessageId); expect(email.inReplyTo).toBe(mockMessageId);
expect(email.reference).toBe(mockMessageId); expect(email.references).toBe(mockMessageId);
}); });
it('should set inReplyTo and reference on the email object even if the message has only one item', async () => { it('should set inReplyTo and reference on the email object even if the message has only one item', async () => {
const mockThreadId = 'thread123'; const mockThreadId = 'thread123';
const mockMessageId = '<message-id@example.com>'; const mockMessageId = '<message-id@example.com>';
const mockThread = { const mockThread = {
messages: [{ payload: { headers: [{ value: mockMessageId }] } }], messages: [{ payload: { headers: [{ name: 'Message-ID', value: mockMessageId }] } }],
}; };
jest.spyOn(GenericFunctions, 'googleApiRequest').mockImplementation(async function () { jest.spyOn(GenericFunctions, 'googleApiRequest').mockImplementation(async function () {
@@ -180,7 +180,7 @@ describe('addThreadHeadersToEmail', () => {
await addThreadHeadersToEmail.call(thisArg, email, mockThreadId); await addThreadHeadersToEmail.call(thisArg, email, mockThreadId);
expect(email.inReplyTo).toBe(mockMessageId); expect(email.inReplyTo).toBe(mockMessageId);
expect(email.reference).toBe(mockMessageId); expect(email.references).toBe(mockMessageId);
}); });
it('should not do anything if the thread has no messages', async () => { it('should not do anything if the thread has no messages', async () => {
@@ -199,6 +199,6 @@ describe('addThreadHeadersToEmail', () => {
// We are using mock<IEmail>({}) which means the value of these will be a mock function // We are using mock<IEmail>({}) which means the value of these will be a mock function
expect(typeof email.inReplyTo).toBe('function'); expect(typeof email.inReplyTo).toBe('function');
expect(typeof email.reference).toBe('function'); expect(typeof email.references).toBe('function');
}); });
}); });

View File

@@ -561,10 +561,10 @@ export class GmailV2 implements INodeType {
attachments, attachments,
}; };
if (threadId && options.replyTo) { if (threadId) {
// If a threadId is set, we need to add the Message-ID of the last message in the thread // If a threadId is set, we need to add the Message-ID of the last message in the thread
// to the email so that Gmail can correctly associate the draft with the thread // to the email so that Gmail can correctly associate the draft with the thread
await addThreadHeadersToEmail.call(this, email, threadId); await addThreadHeadersToEmail.call(this, email, threadId as string);
} }
const body = { const body = {

View File

@@ -4,6 +4,28 @@ import type { IEmail } from '@utils/sendAndWait/interfaces';
import { googleApiRequest } from '../../GenericFunctions'; import { googleApiRequest } from '../../GenericFunctions';
function setEmailReplyHeaders(email: IEmail, messageId: string | undefined): void {
if (messageId) {
email.inReplyTo = messageId;
email.references = messageId;
}
}
function setThreadHeaders(
email: IEmail,
thread: { messages: Array<{ payload: { headers: Array<{ name: string; value: string }> } }> },
): void {
if (thread?.messages) {
const lastMessage = thread.messages.length - 1;
const messageId = thread.messages[lastMessage].payload.headers.find(
(header: { name: string; value: string }) =>
header.name.toLowerCase().includes('message') && header.name.toLowerCase().includes('id'),
)?.value;
setEmailReplyHeaders(email, messageId);
}
}
/** /**
* Adds inReplyTo and reference headers to the email if threadId is provided. * Adds inReplyTo and reference headers to the email if threadId is provided.
*/ */
@@ -20,11 +42,5 @@ export async function addThreadHeadersToEmail(
{ format: 'metadata', metadataHeaders: ['Message-ID'] }, { format: 'metadata', metadataHeaders: ['Message-ID'] },
); );
if (thread?.messages) { setThreadHeaders(email, thread);
const lastMessage = thread.messages.length - 1;
const messageId: string = thread.messages[lastMessage].payload.headers[0].value;
email.inReplyTo = messageId;
email.reference = messageId;
}
} }

View File

@@ -8,6 +8,7 @@ export interface IEmail {
replyTo?: string; replyTo?: string;
inReplyTo?: string; inReplyTo?: string;
reference?: string; reference?: string;
references?: string;
subject: string; subject: string;
body: string; body: string;
htmlBody?: string; htmlBody?: string;