Skip to content

Commit 004872e

Browse files
Gmail - Improve New Email Received event shape (#13992)
* update event shape * updates --------- Co-authored-by: Danny Roosevelt <[email protected]>
1 parent 338a62f commit 004872e

File tree

8 files changed

+34
-46
lines changed

8 files changed

+34
-46
lines changed

components/gmail/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/gmail",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Pipedream Gmail Components",
55
"main": "gmail.app.mjs",
66
"keywords": [

components/gmail/sources/common/base.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,25 @@ export default {
4444
decodedContent: Buffer.from(firstPart.body.data, "base64").toString(),
4545
};
4646
},
47+
processEmail(msg) {
48+
// Process and structure the email data
49+
const headers = msg.payload.headers;
50+
return {
51+
"id": msg.id,
52+
"threadId": msg.threadId,
53+
"subject": headers.find((h) => h.name.toLowerCase() === "subject")?.value,
54+
"from": headers.find((h) => h.name.toLowerCase() === "from")?.value,
55+
"to": headers.find((h) => h.name.toLowerCase() === "to")?.value,
56+
"reply-to": headers.find((h) => h.name.toLowerCase() === "reply-to")?.value,
57+
"date": headers.find((h) => h.name.toLowerCase() === "date")?.value,
58+
"snippet": msg.snippet,
59+
};
60+
},
4761
emitEvent(message) {
62+
message = {
63+
...message,
64+
parsedHeaders: this.processEmail(message),
65+
};
4866
const meta = this.generateMeta(message);
4967
this.$emit(this.decodeContent(message), meta);
5068
},

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

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ export default {
66
hooks: {
77
...common.hooks,
88
async deploy() {
9-
if (this.triggerType === "webhook") {
10-
return;
11-
}
129
const historyId = await this.getHistoryId();
1310
if (!historyId) {
1411
return;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-new-attachment-received",
77
name: "New Attachment Received",
88
description: "Emit new event for each attachment in a message received. This source is capped at 100 max new messages per run.",
9-
version: "0.0.3",
9+
version: "0.0.4",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {
@@ -51,7 +51,7 @@ export default {
5151
return {
5252
id: `${message.id}${attachment.partId}`,
5353
summary: `New Attachment: ${attachment.filename}`,
54-
ts: message.internalDate,
54+
ts: +message.internalDate,
5555
};
5656
},
5757
emitEvent(message) {

components/gmail/sources/new-email-matching-search/new-email-matching-search.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-new-email-matching-search",
77
name: "New Email Matching Search",
88
description: "Emit new event when an email matching the search criteria is received. This source is capped at 100 max new messages per run.",
9-
version: "0.0.2",
9+
version: "0.0.3",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {
@@ -39,7 +39,7 @@ export default {
3939
return {
4040
id: message.id,
4141
summary: `New email: ${subject}`,
42-
ts: message.internalDate,
42+
ts: +message.internalDate,
4343
};
4444
},
4545
},

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

+7-34
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.2",
18+
version: "0.1.3",
1919
dedupe: "unique",
2020
props: {
2121
gmail,
@@ -361,22 +361,6 @@ export default {
361361
}
362362
return topic;
363363
},
364-
processEmails(messageDetails) {
365-
// Process and structure the email data
366-
return messageDetails.map((msg) => {
367-
const headers = msg.payload.headers;
368-
return {
369-
id: msg.id,
370-
threadId: msg.threadId,
371-
subject: headers.find((h) => h.name.toLowerCase() === "subject")
372-
?.value,
373-
from: headers.find((h) => h.name.toLowerCase() === "from")?.value,
374-
to: headers.find((h) => h.name.toLowerCase() === "to")?.value,
375-
date: headers.find((h) => h.name.toLowerCase() === "date")?.value,
376-
snippet: msg.snippet,
377-
};
378-
});
379-
},
380364
getHistoryTypes() {
381365
return [
382366
"messageAdded",
@@ -386,7 +370,7 @@ export default {
386370
return {
387371
id: message.id,
388372
summary: message.snippet,
389-
ts: message.internalDate,
373+
ts: +message.internalDate,
390374
};
391375
},
392376
filterHistory(history) {
@@ -482,29 +466,18 @@ export default {
482466
const newMessageIds = newMessages?.map(({ id }) => id) || [];
483467
const messageDetails = await this.gmail.getMessages(newMessageIds);
484468

485-
console.log("Fetched message details count:", messageDetails.length);
469+
if (!messageDetails?.length) {
470+
return;
471+
}
486472

487-
const processedEmails = this.processEmails(messageDetails);
473+
console.log("Fetched message details count:", messageDetails.length);
488474

489475
// Store the latest historyId in the db
490476
const latestHistoryId = historyResponse.historyId || receivedHistoryId;
491477
this._setLastProcessedHistoryId(latestHistoryId);
492478
console.log("Updated lastProcessedHistoryId:", latestHistoryId);
493479

494-
if (processedEmails?.length) {
495-
this.$emit(
496-
{
497-
newEmailsCount: processedEmails.length,
498-
emails: processedEmails,
499-
lastProcessedHistoryId: latestHistoryId,
500-
},
501-
{
502-
id: processedEmails[0].id,
503-
summary: processedEmails[0].subject,
504-
ts: Date.now(),
505-
},
506-
);
507-
}
480+
messageDetails.forEach((message) => this.emitEvent(message));
508481
}
509482
},
510483
};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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.3",
11+
version: "0.0.4",
1212
dedupe: "unique",
1313
props: {
1414
...common.props,
@@ -32,7 +32,7 @@ export default {
3232
return {
3333
id: `${message.id}-${this.label}`,
3434
summary: `A new message with ID: ${message.id} was labeled with "${this.label}"`,
35-
ts: Date.now(),
35+
ts: +message.internalDate,
3636
};
3737
},
3838
filterHistory(history) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-new-sent-email",
77
name: "New Sent Email",
88
description: "Emit new event for each new email sent. (Maximum of 100 events emited per execution)",
9-
version: "0.0.3",
9+
version: "0.0.4",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {
@@ -30,7 +30,7 @@ export default {
3030
return {
3131
id: message.id,
3232
summary: message.snippet,
33-
ts: new Date(message.internalDate),
33+
ts: +message.internalDate,
3434
};
3535
},
3636
},

0 commit comments

Comments
 (0)