2022-09-08 05:44:34 -07:00
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IExecuteFunctions,
|
2022-09-08 05:44:34 -07:00
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeBaseDescription,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IEmail } from '../GenericFunctions';
|
2022-09-08 05:44:34 -07:00
|
|
|
import {
|
|
|
|
encodeEmail,
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
parseRawEmail,
|
|
|
|
prepareEmailAttachments,
|
|
|
|
prepareEmailBody,
|
|
|
|
prepareEmailsInput,
|
|
|
|
prepareQuery,
|
2023-06-16 01:44:37 -07:00
|
|
|
replyToEmail,
|
2022-09-08 05:44:34 -07:00
|
|
|
simplifyOutput,
|
|
|
|
unescapeSnippets,
|
|
|
|
} from '../GenericFunctions';
|
|
|
|
|
|
|
|
import { messageFields, messageOperations } from './MessageDescription';
|
|
|
|
|
|
|
|
import { labelFields, labelOperations } from './LabelDescription';
|
|
|
|
|
|
|
|
import { draftFields, draftOperations } from './DraftDescription';
|
|
|
|
|
|
|
|
import { threadFields, threadOperations } from './ThreadDescription';
|
|
|
|
|
|
|
|
const versionDescription: INodeTypeDescription = {
|
|
|
|
displayName: 'Gmail',
|
|
|
|
name: 'gmail',
|
|
|
|
icon: 'file:gmail.svg',
|
|
|
|
group: ['transform'],
|
2023-10-03 01:18:59 -07:00
|
|
|
version: [2, 2.1],
|
2022-09-08 05:44:34 -07:00
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Gmail API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Gmail',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: ['serviceAccount'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'gmailOAuth2',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: ['oAuth2'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
|
|
|
name: 'OAuth2 (recommended)',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Service Account',
|
|
|
|
value: 'serviceAccount',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'oAuth2',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
noDataExpression: true,
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Label',
|
|
|
|
value: 'label',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Draft',
|
|
|
|
value: 'draft',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Thread',
|
|
|
|
value: 'thread',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'message',
|
|
|
|
},
|
|
|
|
//-------------------------------
|
|
|
|
// Draft Operations
|
|
|
|
//-------------------------------
|
|
|
|
...draftOperations,
|
|
|
|
...draftFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Label Operations
|
|
|
|
//-------------------------------
|
|
|
|
...labelOperations,
|
|
|
|
...labelFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Message Operations
|
|
|
|
//-------------------------------
|
|
|
|
...messageOperations,
|
|
|
|
...messageFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Thread Operations
|
|
|
|
//-------------------------------
|
|
|
|
...threadOperations,
|
|
|
|
...threadFields,
|
|
|
|
//-------------------------------
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
export class GmailV2 implements INodeType {
|
|
|
|
description: INodeTypeDescription;
|
|
|
|
|
|
|
|
constructor(baseDescription: INodeTypeBaseDescription) {
|
|
|
|
this.description = {
|
|
|
|
...baseDescription,
|
|
|
|
...versionDescription,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
2023-04-19 07:00:49 -07:00
|
|
|
// Get all the labels to display them to user so that they can
|
2022-09-08 05:44:34 -07:00
|
|
|
// select them easily
|
|
|
|
async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
|
|
|
|
const labels = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'labels',
|
|
|
|
'GET',
|
|
|
|
'/gmail/v1/users/me/labels',
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const label of labels) {
|
|
|
|
returnData.push({
|
|
|
|
name: label.name,
|
|
|
|
value: label.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData.sort((a, b) => {
|
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async getThreadMessages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData: INodePropertyOptions[] = [];
|
|
|
|
|
|
|
|
const id = this.getNodeParameter('threadId', 0) as string;
|
|
|
|
const { messages } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
`/gmail/v1/users/me/threads/${id}`,
|
|
|
|
{},
|
|
|
|
{ format: 'minimal' },
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const message of messages || []) {
|
|
|
|
returnData.push({
|
|
|
|
name: message.snippet,
|
|
|
|
value: message.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2023-10-03 01:18:59 -07:00
|
|
|
const nodeVersion = this.getNode().typeVersion;
|
|
|
|
const instanceId = await this.getInstanceId();
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
// labels //
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
if (resource === 'label') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/labels/create
|
|
|
|
const labelName = this.getNodeParameter('name', i) as string;
|
|
|
|
const labelListVisibility = this.getNodeParameter(
|
|
|
|
'options.labelListVisibility',
|
|
|
|
i,
|
|
|
|
'labelShow',
|
|
|
|
) as string;
|
|
|
|
const messageListVisibility = this.getNodeParameter(
|
|
|
|
'options.messageListVisibility',
|
|
|
|
i,
|
|
|
|
'show',
|
|
|
|
) as string;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
labelListVisibility,
|
|
|
|
messageListVisibility,
|
|
|
|
name: labelName,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/gmail/v1/users/me/labels',
|
|
|
|
body,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/labels/delete
|
|
|
|
const labelId = this.getNodeParameter('labelId', i) as string[];
|
|
|
|
const endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/labels/get
|
|
|
|
const labelId = this.getNodeParameter('labelId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'GET', endpoint);
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-12-29 03:20:43 -08:00
|
|
|
responseData = await googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/labels');
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = this.helpers.returnJsonArray(responseData.labels as IDataObject[]);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (!returnAll) {
|
2022-11-18 06:26:22 -08:00
|
|
|
const limit = this.getNodeParameter('limit', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
responseData = responseData.splice(0, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
// messages //
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
if (resource === 'message') {
|
|
|
|
if (operation === 'send') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/send
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
const sendTo = this.getNodeParameter('sendTo', i) as string;
|
|
|
|
let qs: IDataObject = {};
|
|
|
|
|
|
|
|
const to = prepareEmailsInput.call(this, sendTo, 'To', i);
|
|
|
|
let cc = '';
|
|
|
|
let bcc = '';
|
2023-06-16 01:44:37 -07:00
|
|
|
let replyTo = '';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (options.ccList) {
|
|
|
|
cc = prepareEmailsInput.call(this, options.ccList as string, 'CC', i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.bccList) {
|
|
|
|
bcc = prepareEmailsInput.call(this, options.bccList as string, 'BCC', i);
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:44:37 -07:00
|
|
|
if (options.replyTo) {
|
|
|
|
replyTo = prepareEmailsInput.call(this, options.replyTo as string, 'ReplyTo', i);
|
|
|
|
}
|
|
|
|
|
2022-09-08 05:44:34 -07:00
|
|
|
let attachments: IDataObject[] = [];
|
|
|
|
|
|
|
|
if (options.attachmentsUi) {
|
|
|
|
attachments = await prepareEmailAttachments.call(
|
|
|
|
this,
|
|
|
|
options.attachmentsUi as IDataObject,
|
|
|
|
items,
|
|
|
|
i,
|
|
|
|
);
|
|
|
|
if (attachments.length) {
|
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let from = '';
|
|
|
|
if (options.senderName) {
|
|
|
|
const { emailAddress } = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
|
|
|
'/gmail/v1/users/me/profile',
|
|
|
|
);
|
|
|
|
from = `${options.senderName as string} <${emailAddress}>`;
|
|
|
|
}
|
|
|
|
|
2023-10-03 01:18:59 -07:00
|
|
|
let appendAttribution = options.appendAttribution;
|
|
|
|
if (appendAttribution === undefined) {
|
|
|
|
appendAttribution = nodeVersion >= 2.1;
|
|
|
|
}
|
|
|
|
|
2022-09-08 05:44:34 -07:00
|
|
|
const email: IEmail = {
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
cc,
|
|
|
|
bcc,
|
2023-06-16 01:44:37 -07:00
|
|
|
replyTo,
|
2022-09-08 05:44:34 -07:00
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
2023-10-03 01:18:59 -07:00
|
|
|
...prepareEmailBody.call(this, i, appendAttribution as boolean, instanceId),
|
2022-09-08 05:44:34 -07:00
|
|
|
attachments,
|
|
|
|
};
|
|
|
|
|
|
|
|
const endpoint = '/gmail/v1/users/me/messages/send';
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'reply') {
|
|
|
|
const messageIdGmail = this.getNodeParameter('messageId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2023-06-16 01:44:37 -07:00
|
|
|
responseData = await replyToEmail.call(this, items, messageIdGmail, options, i);
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/messages/get
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}`;
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', i, {});
|
2022-09-08 05:44:34 -07:00
|
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
qs.format = 'metadata';
|
|
|
|
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
|
|
|
|
} else {
|
|
|
|
qs.format = 'raw';
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
|
|
|
|
let nodeExecutionData: INodeExecutionData;
|
|
|
|
if (!simple) {
|
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(options.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
|
|
|
|
|
|
|
nodeExecutionData = await parseRawEmail.call(
|
|
|
|
this,
|
|
|
|
responseData,
|
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
|
|
|
} else {
|
2023-02-27 19:39:43 -08:00
|
|
|
const [json, _] = await simplifyOutput.call(this, [responseData as IDataObject]);
|
2022-09-08 05:44:34 -07:00
|
|
|
nodeExecutionData = { json };
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = [nodeExecutionData];
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-12-02 12:54:28 -08:00
|
|
|
const options = this.getNodeParameter('options', i, {});
|
|
|
|
const filters = this.getNodeParameter('filters', i, {});
|
2022-09-08 05:44:34 -07:00
|
|
|
const qs: IDataObject = {};
|
2023-04-03 02:48:11 -07:00
|
|
|
Object.assign(qs, prepareQuery.call(this, filters, i), options, i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'messages',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/messages',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/messages',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
qs.format = 'metadata';
|
|
|
|
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
|
|
|
|
} else {
|
|
|
|
qs.format = 'raw';
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < responseData.length; index++) {
|
|
|
|
responseData[index] = await googleApiRequest.call(
|
2022-09-08 05:44:34 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-02 12:54:28 -08:00
|
|
|
`/gmail/v1/users/me/messages/${responseData[index].id}`,
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!simple) {
|
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(options.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index] = await parseRawEmail.call(
|
2022-09-08 05:44:34 -07:00
|
|
|
this,
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index],
|
2022-09-08 05:44:34 -07:00
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
responseData = this.helpers.returnJsonArray(
|
2023-02-27 19:39:43 -08:00
|
|
|
await simplifyOutput.call(this, responseData as IDataObject[]),
|
2022-09-08 05:44:34 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/delete
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'markAsRead') {
|
|
|
|
// https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
removeLabelIds: ['UNREAD'],
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'markAsUnread') {
|
|
|
|
// https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
addLabelIds: ['UNREAD'],
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (operation === 'addLabels') {
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
addLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
if (operation === 'removeLabels') {
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
removeLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
// drafts //
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
if (resource === 'draft') {
|
|
|
|
if (operation === 'create') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/create
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
let qs: IDataObject = {};
|
|
|
|
|
|
|
|
let to = '';
|
|
|
|
let cc = '';
|
|
|
|
let bcc = '';
|
2023-06-16 01:44:37 -07:00
|
|
|
let replyTo = '';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (options.sendTo) {
|
|
|
|
to += prepareEmailsInput.call(this, options.sendTo as string, 'To', i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.ccList) {
|
|
|
|
cc = prepareEmailsInput.call(this, options.ccList as string, 'CC', i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.bccList) {
|
|
|
|
bcc = prepareEmailsInput.call(this, options.bccList as string, 'BCC', i);
|
|
|
|
}
|
|
|
|
|
2023-06-16 01:44:37 -07:00
|
|
|
if (options.replyTo) {
|
|
|
|
replyTo = prepareEmailsInput.call(this, options.replyTo as string, 'ReplyTo', i);
|
|
|
|
}
|
|
|
|
|
2022-09-08 05:44:34 -07:00
|
|
|
let attachments: IDataObject[] = [];
|
|
|
|
if (options.attachmentsUi) {
|
|
|
|
attachments = await prepareEmailAttachments.call(
|
|
|
|
this,
|
|
|
|
options.attachmentsUi as IDataObject,
|
|
|
|
items,
|
|
|
|
i,
|
|
|
|
);
|
|
|
|
if (attachments.length) {
|
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const email: IEmail = {
|
|
|
|
to,
|
|
|
|
cc,
|
|
|
|
bcc,
|
2023-06-16 01:44:37 -07:00
|
|
|
replyTo,
|
2022-09-08 05:44:34 -07:00
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
...prepareEmailBody.call(this, i),
|
|
|
|
attachments,
|
|
|
|
};
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
message: {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'POST',
|
|
|
|
'/gmail/v1/users/me/drafts',
|
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/get
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
qs.format = 'raw';
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
|
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(options.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const nodeExecutionData = await parseRawEmail.call(
|
2022-09-08 05:44:34 -07:00
|
|
|
this,
|
|
|
|
responseData.message,
|
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add the draft-id
|
|
|
|
nodeExecutionData.json.messageId = nodeExecutionData.json.id;
|
|
|
|
nodeExecutionData.json.id = responseData.id;
|
|
|
|
|
|
|
|
responseData = [nodeExecutionData];
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/delete
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
Object.assign(qs, options);
|
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'drafts',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/drafts',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/drafts',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.drafts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
qs.format = 'raw';
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
for (let index = 0; index < responseData.length; index++) {
|
|
|
|
responseData[index] = await googleApiRequest.call(
|
2022-09-08 05:44:34 -07:00
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-02 12:54:28 -08:00
|
|
|
`/gmail/v1/users/me/drafts/${responseData[index].id}`,
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(options.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
2022-12-02 12:54:28 -08:00
|
|
|
const id = responseData[index].id;
|
|
|
|
responseData[index] = await parseRawEmail.call(
|
2022-09-08 05:44:34 -07:00
|
|
|
this,
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index].message,
|
2022-09-08 05:44:34 -07:00
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Add the draft-id
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index].json.messageId = responseData[index].json.id;
|
|
|
|
responseData[index].json.id = id;
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
// threads //
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
if (resource === 'thread') {
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/delete
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'DELETE', endpoint);
|
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/get
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}`;
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2023-01-19 04:37:19 -08:00
|
|
|
const onlyMessages = options.returnOnlyMessages || false;
|
2022-09-08 05:44:34 -07:00
|
|
|
const qs: IDataObject = {};
|
|
|
|
|
|
|
|
const simple = this.getNodeParameter('simple', i) as boolean;
|
|
|
|
|
|
|
|
if (simple) {
|
|
|
|
qs.format = 'metadata';
|
|
|
|
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
|
|
|
|
} else {
|
|
|
|
qs.format = 'full';
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'GET', endpoint, {}, qs);
|
|
|
|
|
|
|
|
if (onlyMessages) {
|
|
|
|
responseData = this.helpers.returnJsonArray(
|
2023-02-27 19:39:43 -08:00
|
|
|
await simplifyOutput.call(this, responseData.messages as IDataObject[]),
|
2022-09-08 05:44:34 -07:00
|
|
|
);
|
|
|
|
} else {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData.messages = await simplifyOutput.call(
|
|
|
|
this,
|
|
|
|
responseData.messages as IDataObject[],
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
responseData = [{ json: responseData }];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (operation === 'getAll') {
|
|
|
|
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/list
|
2022-11-18 05:31:38 -08:00
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
2022-11-18 07:29:44 -08:00
|
|
|
const filters = this.getNodeParameter('filters', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
const qs: IDataObject = {};
|
2023-04-03 02:48:11 -07:00
|
|
|
Object.assign(qs, prepareQuery.call(this, filters, i));
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (returnAll) {
|
|
|
|
responseData = await googleApiRequestAllItems.call(
|
|
|
|
this,
|
|
|
|
'threads',
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/threads',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
} else {
|
2022-11-18 06:26:22 -08:00
|
|
|
qs.maxResults = this.getNodeParameter('limit', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/threads',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
responseData = responseData.threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (responseData === undefined) {
|
|
|
|
responseData = [];
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = this.helpers.returnJsonArray(responseData as IDataObject[]);
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
if (operation === 'reply') {
|
|
|
|
const messageIdGmail = this.getNodeParameter('messageId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const options = this.getNodeParameter('options', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2023-06-16 01:44:37 -07:00
|
|
|
responseData = await replyToEmail.call(this, items, messageIdGmail, options, i);
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
if (operation === 'trash') {
|
|
|
|
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/trash
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}/trash`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint);
|
|
|
|
}
|
|
|
|
if (operation === 'untrash') {
|
|
|
|
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/untrash
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}/untrash`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint);
|
|
|
|
}
|
|
|
|
if (operation === 'addLabels') {
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
addLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
if (operation === 'removeLabels') {
|
|
|
|
const id = this.getNodeParameter('threadId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
const endpoint = `/gmail/v1/users/me/threads/${id}/modify`;
|
|
|
|
|
|
|
|
const body = {
|
|
|
|
removeLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
responseData = await googleApiRequest.call(this, 'POST', endpoint, body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------//
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-11-08 06:28:21 -08:00
|
|
|
{
|
|
|
|
itemData: { item: i },
|
|
|
|
},
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
returnData.push(...executionData);
|
|
|
|
} catch (error) {
|
|
|
|
error.message = `${error.message} (item ${i})`;
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ json: { error: error.message }, pairedItem: { item: i } });
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
throw new NodeOperationError(this.getNode(), error as Error, {
|
2022-09-08 05:44:34 -07:00
|
|
|
description: error.description,
|
|
|
|
itemIndex: i,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
['draft', 'message', 'thread'].includes(resource) &&
|
|
|
|
['get', 'getAll'].includes(operation)
|
|
|
|
) {
|
2023-09-05 03:59:02 -07:00
|
|
|
return [unescapeSnippets(returnData)];
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|