Skip to content

Commit

Permalink
Gmail - Improve New Email Received event shape (#13992)
Browse files Browse the repository at this point in the history
* update event shape

* updates

---------

Co-authored-by: Danny Roosevelt <[email protected]>
  • Loading branch information
michelle0927 and dannyroosevelt authored Sep 20, 2024
1 parent 338a62f commit 004872e
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 46 deletions.
2 changes: 1 addition & 1 deletion components/gmail/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pipedream/gmail",
"version": "0.1.5",
"version": "0.1.6",
"description": "Pipedream Gmail Components",
"main": "gmail.app.mjs",
"keywords": [
Expand Down
18 changes: 18 additions & 0 deletions components/gmail/sources/common/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,25 @@ export default {
decodedContent: Buffer.from(firstPart.body.data, "base64").toString(),
};
},
processEmail(msg) {
// Process and structure the email data
const headers = msg.payload.headers;
return {
"id": msg.id,
"threadId": msg.threadId,
"subject": headers.find((h) => h.name.toLowerCase() === "subject")?.value,
"from": headers.find((h) => h.name.toLowerCase() === "from")?.value,
"to": headers.find((h) => h.name.toLowerCase() === "to")?.value,
"reply-to": headers.find((h) => h.name.toLowerCase() === "reply-to")?.value,
"date": headers.find((h) => h.name.toLowerCase() === "date")?.value,
"snippet": msg.snippet,
};
},
emitEvent(message) {
message = {
...message,
parsedHeaders: this.processEmail(message),
};
const meta = this.generateMeta(message);
this.$emit(this.decodeContent(message), meta);
},
Expand Down
3 changes: 0 additions & 3 deletions components/gmail/sources/common/polling-history.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ export default {
hooks: {
...common.hooks,
async deploy() {
if (this.triggerType === "webhook") {
return;
}
const historyId = await this.getHistoryId();
if (!historyId) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "gmail-new-attachment-received",
name: "New Attachment Received",
description: "Emit new event for each attachment in a message received. This source is capped at 100 max new messages per run.",
version: "0.0.3",
version: "0.0.4",
type: "source",
dedupe: "unique",
props: {
Expand Down Expand Up @@ -51,7 +51,7 @@ export default {
return {
id: `${message.id}${attachment.partId}`,
summary: `New Attachment: ${attachment.filename}`,
ts: message.internalDate,
ts: +message.internalDate,
};
},
emitEvent(message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "gmail-new-email-matching-search",
name: "New Email Matching Search",
description: "Emit new event when an email matching the search criteria is received. This source is capped at 100 max new messages per run.",
version: "0.0.2",
version: "0.0.3",
type: "source",
dedupe: "unique",
props: {
Expand Down Expand Up @@ -39,7 +39,7 @@ export default {
return {
id: message.id,
summary: `New email: ${subject}`,
ts: message.internalDate,
ts: +message.internalDate,
};
},
},
Expand Down
41 changes: 7 additions & 34 deletions components/gmail/sources/new-email-received/new-email-received.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
name: "New Email Received",
description: "Emit new event when a new email is received.",
type: "source",
version: "0.1.2",
version: "0.1.3",
dedupe: "unique",
props: {
gmail,
Expand Down Expand Up @@ -361,22 +361,6 @@ export default {
}
return topic;
},
processEmails(messageDetails) {
// Process and structure the email data
return messageDetails.map((msg) => {
const headers = msg.payload.headers;
return {
id: msg.id,
threadId: msg.threadId,
subject: headers.find((h) => h.name.toLowerCase() === "subject")
?.value,
from: headers.find((h) => h.name.toLowerCase() === "from")?.value,
to: headers.find((h) => h.name.toLowerCase() === "to")?.value,
date: headers.find((h) => h.name.toLowerCase() === "date")?.value,
snippet: msg.snippet,
};
});
},
getHistoryTypes() {
return [
"messageAdded",
Expand All @@ -386,7 +370,7 @@ export default {
return {
id: message.id,
summary: message.snippet,
ts: message.internalDate,
ts: +message.internalDate,
};
},
filterHistory(history) {
Expand Down Expand Up @@ -482,29 +466,18 @@ export default {
const newMessageIds = newMessages?.map(({ id }) => id) || [];
const messageDetails = await this.gmail.getMessages(newMessageIds);

console.log("Fetched message details count:", messageDetails.length);
if (!messageDetails?.length) {
return;
}

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

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

if (processedEmails?.length) {
this.$emit(
{
newEmailsCount: processedEmails.length,
emails: processedEmails,
lastProcessedHistoryId: latestHistoryId,
},
{
id: processedEmails[0].id,
summary: processedEmails[0].subject,
ts: Date.now(),
},
);
}
messageDetails.forEach((message) => this.emitEvent(message));
}
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default {
name: "New Labeled Email",
description: "Emit new event when a new email is labeled.",
type: "source",
version: "0.0.3",
version: "0.0.4",
dedupe: "unique",
props: {
...common.props,
Expand All @@ -32,7 +32,7 @@ export default {
return {
id: `${message.id}-${this.label}`,
summary: `A new message with ID: ${message.id} was labeled with "${this.label}"`,
ts: Date.now(),
ts: +message.internalDate,
};
},
filterHistory(history) {
Expand Down
4 changes: 2 additions & 2 deletions components/gmail/sources/new-sent-email/new-sent-email.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
key: "gmail-new-sent-email",
name: "New Sent Email",
description: "Emit new event for each new email sent. (Maximum of 100 events emited per execution)",
version: "0.0.3",
version: "0.0.4",
type: "source",
dedupe: "unique",
props: {
Expand All @@ -30,7 +30,7 @@ export default {
return {
id: message.id,
summary: message.snippet,
ts: new Date(message.internalDate),
ts: +message.internalDate,
};
},
},
Expand Down

0 comments on commit 004872e

Please sign in to comment.