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 {
|
2022-09-08 05:44:34 -07:00
|
|
|
IBinaryKeyData,
|
|
|
|
IDataObject,
|
2023-03-06 08:33:32 -08:00
|
|
|
IExecuteFunctions,
|
2022-09-08 05:44:34 -07:00
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodePropertyOptions,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeBaseDescription,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type { IEmail } from '../GenericFunctions';
|
2022-09-08 05:44:34 -07:00
|
|
|
import {
|
|
|
|
encodeEmail,
|
|
|
|
extractEmail,
|
|
|
|
googleApiRequest,
|
|
|
|
googleApiRequestAllItems,
|
|
|
|
parseRawEmail,
|
|
|
|
} from '../GenericFunctions';
|
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
import { messageFields, messageOperations } from './MessageDescription';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
import { messageLabelFields, messageLabelOperations } from './MessageLabelDescription';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
import { labelFields, labelOperations } from './LabelDescription';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
import { draftFields, draftOperations } from './DraftDescription';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2023-02-23 07:16:05 -08:00
|
|
|
import isEmpty from 'lodash.isempty';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
const versionDescription: INodeTypeDescription = {
|
|
|
|
displayName: 'Gmail',
|
|
|
|
name: 'gmail',
|
|
|
|
icon: 'file:gmail.svg',
|
|
|
|
group: ['transform'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Consume the Gmail API',
|
|
|
|
defaults: {
|
|
|
|
name: 'Gmail',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'googleApi',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-11-18 06:26:22 -08:00
|
|
|
authentication: ['serviceAccount'],
|
2022-09-08 05:44:34 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'gmailOAuth2',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-11-18 06:26:22 -08:00
|
|
|
authentication: ['oAuth2'],
|
2022-09-08 05:44:34 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
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: 'Draft',
|
|
|
|
value: 'draft',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Label',
|
|
|
|
value: 'label',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message',
|
|
|
|
value: 'message',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Message Label',
|
|
|
|
value: 'messageLabel',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'draft',
|
|
|
|
},
|
|
|
|
//-------------------------------
|
|
|
|
// Draft Operations
|
|
|
|
//-------------------------------
|
|
|
|
...draftOperations,
|
|
|
|
...draftFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Label Operations
|
|
|
|
//-------------------------------
|
|
|
|
...labelOperations,
|
|
|
|
...labelFields,
|
|
|
|
//-------------------------------
|
|
|
|
// Message Operations
|
|
|
|
//-------------------------------
|
|
|
|
...messageOperations,
|
|
|
|
...messageFields,
|
|
|
|
//-------------------------------
|
|
|
|
// MessageLabel Operations
|
|
|
|
//-------------------------------
|
|
|
|
...messageLabelOperations,
|
|
|
|
...messageLabelFields,
|
|
|
|
//-------------------------------
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
export class GmailV1 implements INodeType {
|
|
|
|
description: INodeTypeDescription;
|
|
|
|
|
|
|
|
constructor(baseDescription: INodeTypeBaseDescription) {
|
|
|
|
this.description = {
|
|
|
|
...baseDescription,
|
|
|
|
...versionDescription,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the labels to display them to user so that he can
|
|
|
|
// select them easily
|
2022-11-18 06:26:22 -08:00
|
|
|
async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
2022-09-08 05:44:34 -07:00
|
|
|
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) => {
|
2022-11-18 06:26:22 -08:00
|
|
|
if (a.name < b.name) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (a.name > b.name) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-09-08 05:44:34 -07:00
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
let method = '';
|
|
|
|
let body: IDataObject = {};
|
|
|
|
let qs: IDataObject = {};
|
|
|
|
let endpoint = '';
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
try {
|
|
|
|
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('labelListVisibility', i) as string;
|
2022-11-18 06:26:22 -08:00
|
|
|
const messageListVisibility = this.getNodeParameter(
|
|
|
|
'messageListVisibility',
|
|
|
|
i,
|
|
|
|
) as string;
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = '/gmail/v1/users/me/labels';
|
|
|
|
|
|
|
|
body = {
|
|
|
|
labelListVisibility,
|
|
|
|
messageListVisibility,
|
|
|
|
name: labelName,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/labels/delete
|
|
|
|
const labelId = this.getNodeParameter('labelId', i) as string[];
|
|
|
|
|
|
|
|
method = 'DELETE';
|
|
|
|
endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/labels/get
|
|
|
|
const labelId = this.getNodeParameter('labelId', i);
|
|
|
|
|
|
|
|
method = 'GET';
|
|
|
|
endpoint = `/gmail/v1/users/me/labels/${labelId}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(
|
|
|
|
this,
|
|
|
|
'GET',
|
2022-12-29 03:20:43 -08:00
|
|
|
'/gmail/v1/users/me/labels',
|
2022-09-08 05:44:34 -07:00
|
|
|
{},
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
responseData = responseData.labels;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resource === 'messageLabel') {
|
|
|
|
if (operation === 'remove') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/messages/modify
|
|
|
|
const messageID = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${messageID}/modify`;
|
|
|
|
body = {
|
|
|
|
removeLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'add') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/modify
|
|
|
|
const messageID = this.getNodeParameter('messageId', i);
|
|
|
|
const labelIds = this.getNodeParameter('labelIds', i) as string[];
|
|
|
|
|
|
|
|
method = 'POST';
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${messageID}/modify`;
|
|
|
|
|
|
|
|
body = {
|
|
|
|
addLabelIds: labelIds,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
|
|
|
|
|
|
|
const toList = this.getNodeParameter('toList', i) as IDataObject[];
|
|
|
|
|
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
|
|
|
|
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
|
|
|
|
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
2022-11-18 06:26:22 -08:00
|
|
|
if (
|
|
|
|
attachmentsUi.hasOwnProperty('attachmentsBinary') &&
|
|
|
|
!isEmpty(attachmentsUi.attachmentsBinary) &&
|
|
|
|
items[i].binary
|
|
|
|
) {
|
2022-09-08 05:44:34 -07:00
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryProperty);
|
|
|
|
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
|
|
|
|
i,
|
|
|
|
binaryProperty,
|
|
|
|
);
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryDataBuffer,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const email: IEmail = {
|
2022-11-18 06:26:22 -08:00
|
|
|
from: (additionalFields.senderName as string) || '',
|
2022-09-08 05:44:34 -07:00
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean) {
|
2022-09-08 05:44:34 -07:00
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = '/gmail/v1/users/me/messages/send';
|
|
|
|
method = 'POST';
|
|
|
|
|
|
|
|
body = {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'reply') {
|
|
|
|
const id = this.getNodeParameter('messageId', i) as string;
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
|
|
|
|
|
|
|
const toList = this.getNodeParameter('toList', i) as IDataObject[];
|
|
|
|
|
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
|
|
|
|
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
|
|
|
|
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
2022-11-18 06:26:22 -08:00
|
|
|
if (
|
|
|
|
attachmentsUi.hasOwnProperty('attachmentsBinary') &&
|
|
|
|
!isEmpty(attachmentsUi.attachmentsBinary) &&
|
|
|
|
items[i].binary
|
|
|
|
) {
|
2022-09-08 05:44:34 -07:00
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryProperty);
|
|
|
|
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
|
|
|
|
i,
|
|
|
|
binaryProperty,
|
|
|
|
);
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryDataBuffer,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
|
|
|
|
|
|
|
qs.format = 'metadata';
|
|
|
|
|
|
|
|
const { payload } = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
|
|
|
|
if (toStr === '') {
|
|
|
|
for (const header of payload.headers as IDataObject[]) {
|
|
|
|
if (header.name === 'From') {
|
|
|
|
toStr = `<${extractEmail(header.value as string)}>,`;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
const subject =
|
|
|
|
payload.headers.filter(
|
|
|
|
(data: { [key: string]: string }) => data.name === 'Subject',
|
|
|
|
)[0]?.value || '';
|
|
|
|
const references =
|
|
|
|
payload.headers.filter(
|
|
|
|
(data: { [key: string]: string }) => data.name === 'References',
|
|
|
|
)[0]?.value || '';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
const email: IEmail = {
|
2022-11-18 06:26:22 -08:00
|
|
|
from: (additionalFields.senderName as string) || '',
|
2022-09-08 05:44:34 -07:00
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean) {
|
2022-09-08 05:44:34 -07:00
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = '/gmail/v1/users/me/messages/send';
|
|
|
|
method = 'POST';
|
|
|
|
|
|
|
|
email.inReplyTo = id;
|
|
|
|
email.reference = references;
|
|
|
|
|
|
|
|
body = {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
threadId: this.getNodeParameter('threadId', i) as string,
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
//https://developers.google.com/gmail/api/v1/reference/users/messages/get
|
|
|
|
method = 'GET';
|
|
|
|
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2023-01-19 04:37:19 -08:00
|
|
|
const format = additionalFields.format || 'resolved';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
|
|
|
|
let nodeExecutionData: INodeExecutionData;
|
|
|
|
if (format === 'resolved') {
|
2022-11-18 06:26:22 -08:00
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(additionalFields.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
nodeExecutionData = await parseRawEmail.call(
|
|
|
|
this,
|
|
|
|
responseData,
|
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
} else {
|
|
|
|
nodeExecutionData = {
|
|
|
|
json: responseData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = nodeExecutionData;
|
|
|
|
}
|
|
|
|
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 additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
if (qs.labelIds) {
|
|
|
|
if (qs.labelIds == '') {
|
|
|
|
delete qs.labelIds;
|
|
|
|
} else {
|
|
|
|
qs.labelIds = qs.labelIds as string[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:37:19 -08:00
|
|
|
const format = additionalFields.format || 'resolved';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (format !== 'ids') {
|
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (format === 'resolved') {
|
2022-11-18 06:26:22 -08:00
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(additionalFields.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index] = await parseRawEmail.call(
|
2022-11-18 06:26:22 -08:00
|
|
|
this,
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index],
|
2022-11-18 06:26:22 -08:00
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format !== 'resolved') {
|
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 === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/messages/delete
|
|
|
|
method = 'DELETE';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
|
|
|
|
endpoint = `/gmail/v1/users/me/messages/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
|
|
|
|
responseData = { success: true };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
let toStr = '';
|
|
|
|
let ccStr = '';
|
|
|
|
let bccStr = '';
|
|
|
|
let attachmentsList: IDataObject[] = [];
|
|
|
|
|
|
|
|
if (additionalFields.toList) {
|
|
|
|
const toList = additionalFields.toList as IDataObject[];
|
|
|
|
|
|
|
|
toList.forEach((email) => {
|
|
|
|
toStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.ccList) {
|
|
|
|
const ccList = additionalFields.ccList as IDataObject[];
|
|
|
|
|
|
|
|
ccList.forEach((email) => {
|
|
|
|
ccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.bccList) {
|
|
|
|
const bccList = additionalFields.bccList as IDataObject[];
|
|
|
|
|
|
|
|
bccList.forEach((email) => {
|
|
|
|
bccStr += `<${email}>, `;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (additionalFields.attachmentsUi) {
|
|
|
|
const attachmentsUi = additionalFields.attachmentsUi as IDataObject;
|
|
|
|
const attachmentsBinary = [];
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
|
|
|
if (!isEmpty(attachmentsUi)) {
|
2022-11-18 06:26:22 -08:00
|
|
|
if (
|
|
|
|
attachmentsUi.hasOwnProperty('attachmentsBinary') &&
|
|
|
|
!isEmpty(attachmentsUi.attachmentsBinary) &&
|
|
|
|
items[i].binary
|
|
|
|
) {
|
2022-09-08 05:44:34 -07:00
|
|
|
for (const { property } of attachmentsUi.attachmentsBinary as IDataObject[]) {
|
|
|
|
for (const binaryProperty of (property as string).split(',')) {
|
2023-03-06 08:33:32 -08:00
|
|
|
const binaryData = this.helpers.assertBinaryData(i, binaryProperty);
|
|
|
|
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(
|
|
|
|
i,
|
|
|
|
binaryProperty,
|
|
|
|
);
|
|
|
|
attachmentsBinary.push({
|
|
|
|
name: binaryData.fileName || 'unknown',
|
|
|
|
content: binaryDataBuffer,
|
|
|
|
type: binaryData.mimeType,
|
|
|
|
});
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
qs = {
|
|
|
|
userId: 'me',
|
|
|
|
uploadType: 'media',
|
|
|
|
};
|
|
|
|
|
|
|
|
attachmentsList = attachmentsBinary;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const email: IEmail = {
|
|
|
|
to: toStr,
|
|
|
|
cc: ccStr,
|
|
|
|
bcc: bccStr,
|
|
|
|
subject: this.getNodeParameter('subject', i) as string,
|
|
|
|
body: this.getNodeParameter('message', i) as string,
|
|
|
|
attachments: attachmentsList,
|
|
|
|
};
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (this.getNodeParameter('includeHtml', i, false) as boolean) {
|
2022-09-08 05:44:34 -07:00
|
|
|
email.htmlBody = this.getNodeParameter('htmlMessage', i) as string;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = '/gmail/v1/users/me/drafts';
|
|
|
|
method = 'POST';
|
|
|
|
|
|
|
|
body = {
|
|
|
|
message: {
|
|
|
|
raw: await encodeEmail(email),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
}
|
|
|
|
if (operation === 'get') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/get
|
|
|
|
method = 'GET';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
|
2022-11-18 07:29:44 -08:00
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i);
|
2023-01-19 04:37:19 -08:00
|
|
|
const format = additionalFields.format || 'resolved';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
|
|
|
|
const binaryData: IBinaryKeyData = {};
|
|
|
|
|
|
|
|
let nodeExecutionData: INodeExecutionData;
|
|
|
|
if (format === 'resolved') {
|
2022-11-18 06:26:22 -08:00
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(additionalFields.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
2022-11-18 06:26:22 -08:00
|
|
|
nodeExecutionData = await parseRawEmail.call(
|
|
|
|
this,
|
|
|
|
responseData.message,
|
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
// Add the draft-id
|
|
|
|
nodeExecutionData.json.messageId = nodeExecutionData.json.id;
|
|
|
|
nodeExecutionData.json.id = responseData.id;
|
|
|
|
} else {
|
|
|
|
nodeExecutionData = {
|
|
|
|
json: responseData,
|
|
|
|
binary: Object.keys(binaryData).length ? binaryData : undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
responseData = nodeExecutionData;
|
|
|
|
}
|
|
|
|
if (operation === 'delete') {
|
|
|
|
// https://developers.google.com/gmail/api/v1/reference/users/drafts/delete
|
|
|
|
method = 'DELETE';
|
|
|
|
const id = this.getNodeParameter('messageId', i);
|
|
|
|
|
|
|
|
endpoint = `/gmail/v1/users/me/drafts/${id}`;
|
|
|
|
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, qs);
|
|
|
|
|
|
|
|
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 additionalFields = this.getNodeParameter('additionalFields', i);
|
2022-09-08 05:44:34 -07:00
|
|
|
Object.assign(qs, additionalFields);
|
|
|
|
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
|
2023-01-19 04:37:19 -08:00
|
|
|
const format = additionalFields.format || 'resolved';
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
if (format !== 'ids') {
|
|
|
|
if (format === 'resolved') {
|
|
|
|
qs.format = 'raw';
|
|
|
|
} else {
|
|
|
|
qs.format = format;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
body,
|
|
|
|
qs,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (format === 'resolved') {
|
2022-11-18 06:26:22 -08:00
|
|
|
const dataPropertyNameDownload =
|
|
|
|
(additionalFields.dataPropertyAttachmentsPrefixName as string) || 'attachment_';
|
2022-12-02 12:54:28 -08:00
|
|
|
const id = responseData[index].id;
|
|
|
|
responseData[index] = await parseRawEmail.call(
|
2022-11-18 06:26:22 -08:00
|
|
|
this,
|
2022-12-02 12:54:28 -08:00
|
|
|
responseData[index].message,
|
2022-11-18 06:26:22 -08:00
|
|
|
dataPropertyNameDownload,
|
|
|
|
);
|
2022-09-08 05:44:34 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (format !== 'resolved') {
|
2023-02-27 19:39:43 -08:00
|
|
|
responseData = this.helpers.returnJsonArray(responseData as IDataObject[]);
|
2022-09-08 05:44:34 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const executionData = this.helpers.constructExecutionMetaData(
|
2023-02-27 19:39:43 -08:00
|
|
|
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
2022-09-08 05:44:34 -07:00
|
|
|
{ itemData: { item: i } },
|
|
|
|
);
|
|
|
|
|
|
|
|
returnData.push(...executionData);
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ json: { error: error.message } });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.prepareOutputData(returnData);
|
|
|
|
}
|
|
|
|
}
|