Skip to content

Commit 5f4d07e

Browse files
authored
Merge branch 'master' into issue-13410
2 parents bd32636 + b38f664 commit 5f4d07e

File tree

32 files changed

+388
-180
lines changed

32 files changed

+388
-180
lines changed

components/gmail/actions/approve-workflow/approve-workflow.mjs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "gmail-approve-workflow",
55
name: "Approve Workflow",
66
description: "Suspend the workflow until approved by email. [See the documentation](https://pipedream.com/docs/code/nodejs/rerun#flowsuspend)",
7-
version: "0.0.1",
7+
version: "0.0.2",
88
type: "action",
99
props: {
1010
gmail,
@@ -20,14 +20,33 @@ export default {
2020
"subject",
2121
],
2222
},
23+
body: {
24+
propDefinition: [
25+
gmail,
26+
"body",
27+
],
28+
description: "Include an email body to send. Supports HTML",
29+
optional: true,
30+
},
31+
bodyType: {
32+
propDefinition: [
33+
gmail,
34+
"bodyType",
35+
],
36+
hidden: true,
37+
default: "html",
38+
},
2339
},
2440
async run({ $ }) {
2541
const {
2642
resume_url, cancel_url,
2743
} = $.flow.suspend();
44+
const approvalText = `Click here to approve:<br />${resume_url}<br /><br />Cancel here:<br />${cancel_url}`;
2845
const opts = await this.gmail.getOptionsToSendEmail($, {
29-
body: `Click here to approve the workflow: ${resume_url}, \nand cancel here: ${cancel_url}`,
3046
...this,
47+
body: this.body
48+
? `${this.body}<br /><br />${approvalText}`
49+
: `${approvalText}`,
3150
});
3251
const response = await this.gmail.sendEmail(opts);
3352
$.export("$summary", `Successfully sent email to ${this.to}`);

components/gmail/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/gmail",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Pipedream Gmail Components",
55
"main": "gmail.app.mjs",
66
"keywords": [

components/gmail/sources/common/polling-history.mjs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export default {
2626
const params = {
2727
maxResults: constants.HISTORICAL_EVENTS,
2828
};
29-
if (this.label) {
30-
params.labelIds = this.label;
29+
if (this.labels?.length) {
30+
params.labelIds = this.labels;
3131
}
3232
let { messages } = await this.gmail.listMessages({
33-
params,
33+
...params,
3434
});
3535
if (!messages?.length) {
3636
return;
@@ -48,30 +48,42 @@ export default {
4848
startHistoryId,
4949
historyTypes: this.getHistoryTypes(),
5050
};
51-
if (this.label) {
52-
opts.labelId = this.label;
53-
}
5451

55-
const {
56-
history, historyId,
57-
} = await this.gmail.listHistory(opts);
52+
const length = this.labels?.length > 0
53+
? this.labels.length
54+
: 1;
55+
let maxHistoryId = 0;
5856

59-
if (!history) {
60-
return;
61-
}
62-
this._setLastHistoryId(historyId);
57+
for (let i = 0; i < length; i++) {
58+
if (this.labels) {
59+
opts.labelId = this.labels[i];
60+
}
6361

64-
const responseArray = this.filterHistory(history);
65-
for (const item of responseArray) {
66-
await this.emitFullMessage(item.messages[0].id);
62+
const {
63+
history, historyId,
64+
} = await this.gmail.listHistory(opts);
65+
66+
if (!history) {
67+
continue;
68+
}
69+
70+
maxHistoryId = Math.max(maxHistoryId, historyId);
71+
const responseArray = this.filterHistory(history);
72+
73+
for (const item of responseArray) {
74+
await this.emitFullMessage(item.messages[0].id);
75+
}
76+
}
77+
if (maxHistoryId > 0) {
78+
this._setLastHistoryId(maxHistoryId);
6779
}
6880
},
6981
async emitRecentMessages() {
7082
const opts = {
7183
maxResults: constants.HISTORICAL_EVENTS,
7284
};
73-
if (this.label) {
74-
opts.labelIds = this.label;
85+
if (this.labels?.length) {
86+
opts.labelIds = this.labels;
7587
}
7688
let { messages } = await this.gmail.listMessages(opts);
7789
if (!messages?.length) {
@@ -88,6 +100,9 @@ export default {
88100
message = await this.gmail.getMessage({
89101
id,
90102
});
103+
if (this.excludeLabels && message.labelIds.some((i) => this.excludeLabels.includes(i))) {
104+
return;
105+
}
91106
} catch {
92107
console.log(`Message ${id} not found`);
93108
}

components/gmail/sources/new-email-received/new-email-received.mjs

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default {
1515
name: "New Email Received",
1616
description: "Emit new event when a new email is received.",
1717
type: "source",
18-
version: "0.1.9",
18+
version: "0.1.10",
1919
dedupe: "unique",
2020
props: {
2121
gmail,
@@ -72,12 +72,27 @@ export default {
7272
hidden: true,
7373
reloadProps: true,
7474
},
75-
label: {
75+
labels: {
7676
propDefinition: [
7777
gmail,
7878
"label",
7979
],
80-
default: "INBOX",
80+
type: "string[]",
81+
label: "Labels",
82+
default: [
83+
"INBOX",
84+
],
85+
optional: true,
86+
hidden: true,
87+
},
88+
excludeLabels: {
89+
propDefinition: [
90+
gmail,
91+
"label",
92+
],
93+
type: "string[]",
94+
label: "Exclude Labels",
95+
description: "Emails with the specified labels will be excluded from results",
8196
optional: true,
8297
hidden: true,
8398
},
@@ -221,7 +236,8 @@ export default {
221236
};
222237
}
223238
}
224-
props.label.hidden = false;
239+
props.labels.hidden = false;
240+
props.excludeLabels.hidden = false;
225241
return newProps;
226242
},
227243
hooks: {
@@ -354,8 +370,8 @@ export default {
354370
path: "/users/me/watch",
355371
data: {
356372
topicName,
357-
labelIds: [
358-
this.label || "INBOX",
373+
labelIds: this.labels || [
374+
"INBOX",
359375
],
360376
},
361377
});
@@ -396,14 +412,18 @@ export default {
396412
};
397413
},
398414
filterHistory(history) {
399-
return this.label
400-
? history.filter(
401-
(item) =>
402-
item.messagesAdded?.length &&
403-
item.messagesAdded[0].message.labelIds &&
404-
item.messagesAdded[0].message.labelIds.includes(this.label),
405-
)
406-
: history.filter((item) => item.messagesAdded?.length);
415+
let filteredHistory = history.filter((item) => item.messagesAdded?.length);
416+
if (this.labels) {
417+
filteredHistory = filteredHistory.filter((item) =>
418+
item.messagesAdded[0].message.labelIds &&
419+
item.messagesAdded[0].message.labelIds.some((i) => this.labels.includes(i)));
420+
}
421+
if (this.excludeLabels) {
422+
filteredHistory = filteredHistory.filter((item) =>
423+
item.messagesAdded[0].message.labelIds &&
424+
!(item.messagesAdded[0].message.labelIds.some((i) => this.excludeLabels.includes(i))));
425+
}
426+
return filteredHistory;
407427
},
408428
async getMessageDetails(ids) {
409429
const messages = await Promise.all(ids.map(async (id) => {
@@ -419,14 +439,19 @@ export default {
419439
}));
420440
return messages;
421441
},
422-
getHistoryResponse(startHistoryId) {
423-
return this.gmail.listHistory({
424-
startHistoryId,
425-
historyTypes: [
426-
"messageAdded",
427-
],
428-
labelId: this.label,
429-
});
442+
async getHistoryResponses(startHistoryId) {
443+
const historyResponses = [];
444+
for (const labelId of this.labels) {
445+
const response = await this.gmail.listHistory({
446+
startHistoryId,
447+
historyTypes: [
448+
"messageAdded",
449+
],
450+
labelId,
451+
});
452+
historyResponses.push(response);
453+
}
454+
return historyResponses;
430455
},
431456
},
432457
async run(event) {
@@ -495,9 +520,9 @@ export default {
495520
console.log("Using startHistoryId:", startHistoryId);
496521

497522
// Fetch the history
498-
let historyResponse;
523+
let historyResponses;
499524
try {
500-
historyResponse = await this.getHistoryResponse(startHistoryId);
525+
historyResponses = await this.getHistoryResponses(startHistoryId);
501526
} catch {
502527
// catch error thrown if startHistoryId is invalid or expired
503528

@@ -507,19 +532,20 @@ export default {
507532
// set startHistoryId to the historyId received from the webhook
508533
startHistoryId = parseInt(receivedHistoryId);
509534
console.log("Using startHistoryId:", startHistoryId);
510-
historyResponse = await this.getHistoryResponse(startHistoryId);
535+
historyResponses = await this.getHistoryResponses(startHistoryId);
511536
}
512537

513538
console.log(
514-
"History response:",
515-
JSON.stringify(historyResponse, null, 2),
539+
"History responses:",
540+
JSON.stringify(historyResponses, null, 2),
516541
);
517542

518543
// Process history to find new messages
519544
const newMessages = [];
520-
if (historyResponse.history) {
521-
for (const historyItem of historyResponse.history) {
522-
if (historyItem.messagesAdded) {
545+
for (const historyResponse of historyResponses) {
546+
if (historyResponse.history) {
547+
const historyResponseFiltered = this.filterHistory(historyResponse.history);
548+
for (const historyItem of historyResponseFiltered) {
523549
newMessages.push(
524550
...historyItem.messagesAdded.map((msg) => msg.message),
525551
);
@@ -540,7 +566,10 @@ export default {
540566
console.log("Fetched message details count:", messageDetails.length);
541567

542568
// Store the latest historyId in the db
543-
const latestHistoryId = historyResponse.historyId || receivedHistoryId;
569+
let latestHistoryId = receivedHistoryId;
570+
for (const historyResponse of historyResponses) {
571+
latestHistoryId = Math.max(latestHistoryId, historyResponse.historyId);
572+
}
544573
this._setLastProcessedHistoryId(latestHistoryId);
545574
console.log("Updated lastProcessedHistoryId:", latestHistoryId);
546575

components/gmail/sources/new-labeled-email/new-labeled-email.mjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ export default {
88
name: "New Labeled Email",
99
description: "Emit new event when a new email is labeled.",
1010
type: "source",
11-
version: "0.0.6",
11+
version: "0.0.7",
1212
dedupe: "unique",
1313
props: {
1414
...common.props,
1515
gmail,
16-
label: {
16+
labels: {
1717
propDefinition: [
1818
gmail,
1919
"label",
2020
],
21+
type: "string[]",
22+
label: "Labels",
2123
},
2224
},
2325
methods: {
@@ -30,17 +32,17 @@ export default {
3032
},
3133
generateMeta(message) {
3234
return {
33-
id: `${message.id}-${this.label}`,
34-
summary: `A new message with ID: ${message.id} was labeled with "${this.label}"`,
35+
id: `${message.id}-${message.historyId}`,
36+
summary: `A new message with ID: ${message.id} was labeled"`,
3537
ts: +message.internalDate,
3638
};
3739
},
3840
filterHistory(history) {
3941
return history.filter((item) =>
40-
(item.labelsAdded && item.labelsAdded[0].labelIds.includes(this.label))
42+
(item.labelsAdded && item.labelsAdded[0].labelIds.some((i) => this.labels.includes(i)))
4143
|| (item.messagesAdded
4244
&& item.messagesAdded[0].message.labelIds
43-
&& item.messagesAdded[0].message.labelIds.includes(this.label)));
45+
&& item.messagesAdded[0].message.labelIds.some((i) => this.labels.includes(i))));
4446
},
4547
},
4648
sampleEmit,

0 commit comments

Comments
 (0)