n8n-nodes-base
Version:
Base nodes of n8n
601 lines • 33.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GmailV2 = void 0;
const n8n_workflow_1 = require("n8n-workflow");
const DraftDescription_1 = require("./DraftDescription");
const LabelDescription_1 = require("./LabelDescription");
const loadOptions_1 = require("./loadOptions");
const MessageDescription_1 = require("./MessageDescription");
const ThreadDescription_1 = require("./ThreadDescription");
const draft_1 = require("./utils/draft");
const configureWaitTillDate_util_1 = require("../../../../utils/sendAndWait/configureWaitTillDate.util");
const descriptions_1 = require("../../../../utils/sendAndWait/descriptions");
const utils_1 = require("../../../../utils/sendAndWait/utils");
const GenericFunctions_1 = require("../GenericFunctions");
const replyToEmail_1 = require("../utils/replyToEmail");
const versionDescription = {
displayName: 'Gmail',
name: 'gmail',
icon: 'file:gmail.svg',
group: ['transform'],
version: [2, 2.1, 2.2],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the Gmail API',
defaults: {
name: 'Gmail',
},
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
usableAsTool: true,
credentials: [
{
name: 'googleApi',
required: true,
displayOptions: {
show: {
authentication: ['serviceAccount'],
},
},
},
{
name: 'gmailOAuth2',
required: true,
displayOptions: {
show: {
authentication: ['oAuth2'],
},
},
},
],
waitingNodeTooltip: utils_1.SEND_AND_WAIT_WAITING_TOOLTIP,
webhooks: descriptions_1.sendAndWaitWebhooksDescription,
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
//-------------------------------
...DraftDescription_1.draftOperations,
...DraftDescription_1.draftFields,
//-------------------------------
// Label Operations
//-------------------------------
...LabelDescription_1.labelOperations,
...LabelDescription_1.labelFields,
//-------------------------------
// Message Operations
//-------------------------------
...MessageDescription_1.messageOperations,
...MessageDescription_1.messageFields,
...(0, utils_1.getSendAndWaitProperties)([
{
displayName: 'To',
name: 'sendTo',
type: 'string',
default: '',
required: true,
placeholder: 'e.g. info@example.com',
},
]),
//-------------------------------
// Thread Operations
//-------------------------------
...ThreadDescription_1.threadOperations,
...ThreadDescription_1.threadFields,
//-------------------------------
],
};
class GmailV2 {
description;
constructor(baseDescription) {
this.description = {
...baseDescription,
...versionDescription,
};
}
methods = {
loadOptions: {
getLabels: loadOptions_1.getLabels,
getThreadMessages: loadOptions_1.getThreadMessages,
getGmailAliases: loadOptions_1.getGmailAliases,
},
};
webhook = utils_1.sendAndWaitWebhook;
async execute() {
const items = this.getInputData();
const returnData = [];
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const nodeVersion = this.getNode().typeVersion;
const instanceId = this.getInstanceId();
if (resource === 'message' && operation === n8n_workflow_1.SEND_AND_WAIT_OPERATION) {
const email = (0, utils_1.createEmail)(this);
await GenericFunctions_1.googleApiRequest.call(this, 'POST', '/gmail/v1/users/me/messages/send', {
raw: await (0, GenericFunctions_1.encodeEmail)(email),
});
const waitTill = (0, configureWaitTillDate_util_1.configureWaitTillDate)(this);
await this.putExecutionToWait(waitTill);
return [this.getInputData()];
}
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);
const labelListVisibility = this.getNodeParameter('options.labelListVisibility', i, 'labelShow');
const messageListVisibility = this.getNodeParameter('options.messageListVisibility', i, 'show');
const body = {
labelListVisibility,
messageListVisibility,
name: labelName,
};
responseData = await GenericFunctions_1.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);
const endpoint = `/gmail/v1/users/me/labels/${labelId}`;
responseData = await GenericFunctions_1.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 GenericFunctions_1.googleApiRequest.call(this, 'GET', endpoint);
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i);
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/labels');
responseData = this.helpers.returnJsonArray(responseData.labels);
if (!returnAll) {
const limit = this.getNodeParameter('limit', i);
responseData = responseData.splice(0, limit);
}
}
}
//------------------------------------------------------------------//
// messages //
//------------------------------------------------------------------//
if (resource === 'message') {
if (operation === 'send') {
// https://developers.google.com/gmail/api/v1/reference/users/messages/send
const options = this.getNodeParameter('options', i);
const sendTo = this.getNodeParameter('sendTo', i);
let qs = {};
const to = GenericFunctions_1.prepareEmailsInput.call(this, sendTo, 'To', i);
let cc = '';
let bcc = '';
let replyTo = '';
if (options.ccList) {
cc = GenericFunctions_1.prepareEmailsInput.call(this, options.ccList, 'CC', i);
}
if (options.bccList) {
bcc = GenericFunctions_1.prepareEmailsInput.call(this, options.bccList, 'BCC', i);
}
if (options.replyTo) {
replyTo = GenericFunctions_1.prepareEmailsInput.call(this, options.replyTo, 'ReplyTo', i);
}
let attachments = [];
if (options.attachmentsUi) {
attachments = await GenericFunctions_1.prepareEmailAttachments.call(this, options.attachmentsUi, i);
if (attachments.length) {
qs = {
userId: 'me',
uploadType: 'media',
};
}
}
let from = '';
if (options.senderName) {
const { emailAddress } = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/profile');
from = `${options.senderName} <${emailAddress}>`;
}
let appendAttribution = options.appendAttribution;
if (appendAttribution === undefined) {
appendAttribution = nodeVersion >= 2.1;
}
const email = {
from,
to,
cc,
bcc,
replyTo,
subject: this.getNodeParameter('subject', i),
...GenericFunctions_1.prepareEmailBody.call(this, i, appendAttribution, instanceId),
attachments,
};
const endpoint = '/gmail/v1/users/me/messages/send';
const body = {
raw: await (0, GenericFunctions_1.encodeEmail)(email),
};
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body, qs);
}
if (operation === 'reply') {
const messageIdGmail = this.getNodeParameter('messageId', i);
const options = this.getNodeParameter('options', i);
responseData = await replyToEmail_1.replyToEmail.call(this, messageIdGmail, options, i, nodeVersion);
}
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 = {};
const options = this.getNodeParameter('options', i, {});
const simple = this.getNodeParameter('simple', i);
if (simple) {
qs.format = 'metadata';
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
}
else {
qs.format = 'raw';
}
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', endpoint, {}, qs);
let nodeExecutionData;
if (!simple) {
const dataPropertyNameDownload = options.dataPropertyAttachmentsPrefixName || 'attachment_';
nodeExecutionData = await GenericFunctions_1.parseRawEmail.call(this, responseData, dataPropertyNameDownload);
}
else {
const [json, _] = await GenericFunctions_1.simplifyOutput.call(this, [responseData]);
nodeExecutionData = { json };
}
responseData = [nodeExecutionData];
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i);
const options = this.getNodeParameter('options', i, {});
const filters = this.getNodeParameter('filters', i, {});
const qs = {};
Object.assign(qs, GenericFunctions_1.prepareQuery.call(this, filters, i), options, i);
if (returnAll) {
responseData = await GenericFunctions_1.googleApiRequestAllItems.call(this, 'messages', 'GET', '/gmail/v1/users/me/messages', {}, qs);
}
else {
qs.maxResults = this.getNodeParameter('limit', i);
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/messages', {}, qs);
responseData = responseData.messages;
}
if (responseData === undefined) {
responseData = [];
}
const simple = this.getNodeParameter('simple', i);
if (simple) {
qs.format = 'metadata';
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
}
else {
qs.format = 'raw';
}
for (let index = 0; index < responseData.length; index++) {
responseData[index] = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/gmail/v1/users/me/messages/${responseData[index].id}`, {}, qs);
if (!simple) {
const dataPropertyNameDownload = options.dataPropertyAttachmentsPrefixName || 'attachment_';
responseData[index] = await GenericFunctions_1.parseRawEmail.call(this, responseData[index], dataPropertyNameDownload);
}
}
if (simple) {
responseData = this.helpers.returnJsonArray(await GenericFunctions_1.simplifyOutput.call(this, responseData));
}
}
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 GenericFunctions_1.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 GenericFunctions_1.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 GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body);
}
if (operation === 'addLabels') {
const id = this.getNodeParameter('messageId', i);
const labelIds = this.getNodeParameter('labelIds', i);
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
const body = {
addLabelIds: labelIds,
};
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body);
}
if (operation === 'removeLabels') {
const id = this.getNodeParameter('messageId', i);
const labelIds = this.getNodeParameter('labelIds', i);
const endpoint = `/gmail/v1/users/me/messages/${id}/modify`;
const body = {
removeLabelIds: labelIds,
};
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body);
}
}
//------------------------------------------------------------------//
// drafts //
//------------------------------------------------------------------//
if (resource === 'draft') {
if (operation === 'create') {
// https://developers.google.com/gmail/api/v1/reference/users/drafts/create
const options = this.getNodeParameter('options', i);
let qs = {};
let to = '';
let cc = '';
let bcc = '';
let replyTo = '';
let fromAlias = '';
let threadId = null;
if (options.sendTo) {
to += GenericFunctions_1.prepareEmailsInput.call(this, options.sendTo, 'To', i);
}
if (options.ccList) {
cc = GenericFunctions_1.prepareEmailsInput.call(this, options.ccList, 'CC', i);
}
if (options.bccList) {
bcc = GenericFunctions_1.prepareEmailsInput.call(this, options.bccList, 'BCC', i);
}
if (options.replyTo) {
replyTo = GenericFunctions_1.prepareEmailsInput.call(this, options.replyTo, 'ReplyTo', i);
}
if (options.fromAlias) {
fromAlias = options.fromAlias;
}
if (options.threadId && typeof options.threadId === 'string') {
threadId = options.threadId;
}
let attachments = [];
if (options.attachmentsUi) {
attachments = await GenericFunctions_1.prepareEmailAttachments.call(this, options.attachmentsUi, i);
if (attachments.length) {
qs = {
userId: 'me',
uploadType: 'media',
};
}
}
const email = {
from: fromAlias,
to,
cc,
bcc,
replyTo,
subject: this.getNodeParameter('subject', i),
...GenericFunctions_1.prepareEmailBody.call(this, i),
attachments,
};
if (threadId) {
// 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
await draft_1.addThreadHeadersToEmail.call(this, email, threadId);
}
const body = {
message: {
raw: await (0, GenericFunctions_1.encodeEmail)(email),
threadId: threadId || undefined,
},
};
responseData = await GenericFunctions_1.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 = {};
const options = this.getNodeParameter('options', i);
qs.format = 'raw';
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', endpoint, {}, qs);
const dataPropertyNameDownload = options.dataPropertyAttachmentsPrefixName || 'attachment_';
const nodeExecutionData = await GenericFunctions_1.parseRawEmail.call(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 GenericFunctions_1.googleApiRequest.call(this, 'DELETE', endpoint);
responseData = { success: true };
}
if (operation === 'getAll') {
const returnAll = this.getNodeParameter('returnAll', i);
const options = this.getNodeParameter('options', i);
const qs = {};
Object.assign(qs, options);
if (returnAll) {
responseData = await GenericFunctions_1.googleApiRequestAllItems.call(this, 'drafts', 'GET', '/gmail/v1/users/me/drafts', {}, qs);
}
else {
qs.maxResults = this.getNodeParameter('limit', i);
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/drafts', {}, qs);
responseData = responseData.drafts;
}
if (responseData === undefined) {
responseData = [];
}
qs.format = 'raw';
for (let index = 0; index < responseData.length; index++) {
responseData[index] = await GenericFunctions_1.googleApiRequest.call(this, 'GET', `/gmail/v1/users/me/drafts/${responseData[index].id}`, {}, qs);
const dataPropertyNameDownload = options.dataPropertyAttachmentsPrefixName || 'attachment_';
const id = responseData[index].id;
responseData[index] = await GenericFunctions_1.parseRawEmail.call(this, responseData[index].message, dataPropertyNameDownload);
// Add the draft-id
responseData[index].json.messageId = responseData[index].json.id;
responseData[index].json.id = id;
}
}
}
//------------------------------------------------------------------//
// 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 GenericFunctions_1.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}`;
const options = this.getNodeParameter('options', i);
const onlyMessages = options.returnOnlyMessages || false;
const qs = {};
const simple = this.getNodeParameter('simple', i);
if (simple) {
qs.format = 'metadata';
qs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
}
else {
qs.format = 'full';
}
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', endpoint, {}, qs);
if (onlyMessages) {
responseData = this.helpers.returnJsonArray(await GenericFunctions_1.simplifyOutput.call(this, responseData.messages));
}
else {
responseData.messages = await GenericFunctions_1.simplifyOutput.call(this, responseData.messages);
responseData = [{ json: responseData }];
}
}
if (operation === 'getAll') {
//https://developers.google.com/gmail/api/reference/rest/v1/users.threads/list
const returnAll = this.getNodeParameter('returnAll', i);
const filters = this.getNodeParameter('filters', i);
const qs = {};
Object.assign(qs, GenericFunctions_1.prepareQuery.call(this, filters, i));
if (returnAll) {
responseData = await GenericFunctions_1.googleApiRequestAllItems.call(this, 'threads', 'GET', '/gmail/v1/users/me/threads', {}, qs);
}
else {
qs.maxResults = this.getNodeParameter('limit', i);
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'GET', '/gmail/v1/users/me/threads', {}, qs);
responseData = responseData.threads;
}
if (responseData === undefined) {
responseData = [];
}
responseData = this.helpers.returnJsonArray(responseData);
}
if (operation === 'reply') {
const messageIdGmail = this.getNodeParameter('messageId', i);
const options = this.getNodeParameter('options', i);
responseData = await replyToEmail_1.replyToEmail.call(this, messageIdGmail, options, i, nodeVersion);
}
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 GenericFunctions_1.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 GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint);
}
if (operation === 'addLabels') {
const id = this.getNodeParameter('threadId', i);
const labelIds = this.getNodeParameter('labelIds', i);
const endpoint = `/gmail/v1/users/me/threads/${id}/modify`;
const body = {
addLabelIds: labelIds,
};
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body);
}
if (operation === 'removeLabels') {
const id = this.getNodeParameter('threadId', i);
const labelIds = this.getNodeParameter('labelIds', i);
const endpoint = `/gmail/v1/users/me/threads/${id}/modify`;
const body = {
removeLabelIds: labelIds,
};
responseData = await GenericFunctions_1.googleApiRequest.call(this, 'POST', endpoint, body);
}
}
//------------------------------------------------------------------//
const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(responseData), {
itemData: { item: i },
});
returnData.push(...executionData);
}
catch (error) {
error.message = `${error.message} (item ${i})`;
if (this.continueOnFail()) {
returnData.push({ json: { error: error.message }, pairedItem: { item: i } });
continue;
}
throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, {
description: error.description,
itemIndex: i,
});
}
}
if (['draft', 'message', 'thread'].includes(resource) &&
['get', 'getAll'].includes(operation)) {
return [(0, GenericFunctions_1.unescapeSnippets)(returnData)];
}
return [returnData];
}
}
exports.GmailV2 = GmailV2;
//# sourceMappingURL=GmailV2.node.js.map