n8n-nodes-gmail-custom 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -49,6 +49,24 @@ class GmailTriggerCustom {
|
|
|
49
49
|
default: true,
|
|
50
50
|
description: 'Whether to return a simplified version of the response instead of the raw data',
|
|
51
51
|
},
|
|
52
|
+
{
|
|
53
|
+
displayName: 'Max Emails per Poll',
|
|
54
|
+
name: 'maxResults',
|
|
55
|
+
type: 'number',
|
|
56
|
+
default: 10,
|
|
57
|
+
typeOptions: {
|
|
58
|
+
minValue: 1,
|
|
59
|
+
maxValue: 50,
|
|
60
|
+
},
|
|
61
|
+
description: 'Maximum number of emails to fetch each time the node polls. Remaining emails are picked up in subsequent polls.',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
displayName: 'Mark as Read',
|
|
65
|
+
name: 'markAsRead',
|
|
66
|
+
type: 'boolean',
|
|
67
|
+
default: false,
|
|
68
|
+
description: 'Whether to mark processed emails as read (adds 1 API call per email)',
|
|
69
|
+
},
|
|
52
70
|
{
|
|
53
71
|
displayName: 'Filters',
|
|
54
72
|
name: 'filters',
|
|
@@ -128,6 +146,8 @@ class GmailTriggerCustom {
|
|
|
128
146
|
const privateKey = this.getNodeParameter('privateKey', 0) || '';
|
|
129
147
|
const delegatedEmail = this.getNodeParameter('delegatedEmail', 0) || '';
|
|
130
148
|
const simple = this.getNodeParameter('simple', 0);
|
|
149
|
+
let maxResults = this.getNodeParameter('maxResults', 0) || 10;
|
|
150
|
+
const markAsRead = this.getNodeParameter('markAsRead', 0);
|
|
131
151
|
const filters = this.getNodeParameter('filters', 0, {});
|
|
132
152
|
|
|
133
153
|
const accessToken = await getOrRefreshAccessToken(this, serviceAccountEmail, privateKey, delegatedEmail);
|
|
@@ -166,11 +186,18 @@ class GmailTriggerCustom {
|
|
|
166
186
|
return;
|
|
167
187
|
}
|
|
168
188
|
|
|
169
|
-
if (
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
189
|
+
if (markAsRead) {
|
|
190
|
+
try {
|
|
191
|
+
await this.helpers.httpRequest({
|
|
192
|
+
method: 'POST',
|
|
193
|
+
url: `https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}/modify`,
|
|
194
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
195
|
+
body: { removeLabelIds: ['UNREAD'] },
|
|
196
|
+
});
|
|
197
|
+
} catch (e) {}
|
|
173
198
|
}
|
|
199
|
+
|
|
200
|
+
responseData.push({ json: fullMessage });
|
|
174
201
|
};
|
|
175
202
|
|
|
176
203
|
try {
|
|
@@ -219,41 +246,58 @@ class GmailTriggerCustom {
|
|
|
219
246
|
qs.q = parts.join(' ');
|
|
220
247
|
|
|
221
248
|
if (this.getMode() === 'manual') {
|
|
222
|
-
|
|
249
|
+
maxResults = 1;
|
|
223
250
|
}
|
|
224
251
|
|
|
225
|
-
|
|
226
|
-
method: 'GET',
|
|
227
|
-
url: 'https://gmail.googleapis.com/gmail/v1/users/me/messages',
|
|
228
|
-
headers: { Authorization: `Bearer ${accessToken}` },
|
|
229
|
-
qs,
|
|
230
|
-
});
|
|
252
|
+
let budget = maxResults;
|
|
231
253
|
|
|
232
|
-
|
|
254
|
+
// Process pending messages from previous poll first.
|
|
255
|
+
const pendingIds = nodeStaticData.pendingMessageIds || [];
|
|
256
|
+
if (pendingIds.length > 0) {
|
|
257
|
+
const idsToFetch = pendingIds.slice(0, budget);
|
|
258
|
+
nodeStaticData.pendingMessageIds = pendingIds.slice(budget);
|
|
233
259
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const possibleDuplicates = new Set(nodeStaticData.possibleDuplicates || []);
|
|
239
|
-
const filteredMessages = possibleDuplicates.size > 0
|
|
240
|
-
? messages.filter((m) => !possibleDuplicates.has(m.id))
|
|
241
|
-
: messages;
|
|
260
|
+
const fetchQs = {};
|
|
261
|
+
if (!simple) { fetchQs.format = 'raw'; }
|
|
262
|
+
else { fetchQs.format = 'metadata'; fetchQs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject']; }
|
|
242
263
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
const fetchQs = {};
|
|
248
|
-
if (!simple) {
|
|
249
|
-
fetchQs.format = 'raw';
|
|
250
|
-
} else {
|
|
251
|
-
fetchQs.format = 'metadata';
|
|
252
|
-
fetchQs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject'];
|
|
264
|
+
for (const id of idsToFetch) {
|
|
265
|
+
await fetchAndProcessMessage(id, fetchQs);
|
|
266
|
+
}
|
|
267
|
+
budget -= idsToFetch.length;
|
|
253
268
|
}
|
|
254
269
|
|
|
255
|
-
|
|
256
|
-
|
|
270
|
+
// Only list new messages if budget remains.
|
|
271
|
+
if (budget > 0) {
|
|
272
|
+
const messagesResponse = await this.helpers.httpRequest({
|
|
273
|
+
method: 'GET',
|
|
274
|
+
url: 'https://gmail.googleapis.com/gmail/v1/users/me/messages',
|
|
275
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
276
|
+
qs,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const messages = messagesResponse.messages || [];
|
|
280
|
+
|
|
281
|
+
if (messages.length > 0) {
|
|
282
|
+
const possibleDuplicates = new Set(nodeStaticData.possibleDuplicates || []);
|
|
283
|
+
const filteredMessages = possibleDuplicates.size > 0
|
|
284
|
+
? messages.filter((m) => !possibleDuplicates.has(m.id))
|
|
285
|
+
: messages;
|
|
286
|
+
|
|
287
|
+
let messagesToProcess = filteredMessages;
|
|
288
|
+
if (filteredMessages.length > budget) {
|
|
289
|
+
messagesToProcess = filteredMessages.slice(0, budget);
|
|
290
|
+
nodeStaticData.pendingMessageIds = filteredMessages.slice(budget).map((m) => m.id);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const fetchQs = {};
|
|
294
|
+
if (!simple) { fetchQs.format = 'raw'; }
|
|
295
|
+
else { fetchQs.format = 'metadata'; fetchQs.metadataHeaders = ['From', 'To', 'Cc', 'Bcc', 'Subject']; }
|
|
296
|
+
|
|
297
|
+
for (const message of messagesToProcess) {
|
|
298
|
+
await fetchAndProcessMessage(message.id, fetchQs);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
257
301
|
}
|
|
258
302
|
} catch (error) {
|
|
259
303
|
if (this.getMode() === 'manual' || !nodeStaticData.lastTimeChecked) {
|
|
@@ -273,7 +317,9 @@ class GmailTriggerCustom {
|
|
|
273
317
|
);
|
|
274
318
|
|
|
275
319
|
nodeStaticData.possibleDuplicates = allFetchedMessages.map((m) => m.id);
|
|
276
|
-
nodeStaticData.
|
|
320
|
+
if (!nodeStaticData.pendingMessageIds || nodeStaticData.pendingMessageIds.length === 0) {
|
|
321
|
+
nodeStaticData.lastTimeChecked = Math.max(lastEmailDate || startDate, startDate);
|
|
322
|
+
}
|
|
277
323
|
|
|
278
324
|
return responseData.length > 0 ? [responseData] : null;
|
|
279
325
|
}
|
package/package.json
CHANGED