Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.10",
"version": "0.1.11",
"description": "Pipedream Gmail Components",
"main": "gmail.app.mjs",
"keywords": [
Expand Down
34 changes: 25 additions & 9 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.7",
version: "0.1.8",
dedupe: "unique",
props: {
gmail,
Expand Down Expand Up @@ -419,6 +419,15 @@ export default {
}));
return messages;
},
getHistoryResponse(startHistoryId) {
return this.gmail.listHistory({
startHistoryId,
historyTypes: [
"messageAdded",
],
labelId: this.label,
});
},
},
async run(event) {
if (this.triggerType === "polling") {
Expand Down Expand Up @@ -479,20 +488,27 @@ export default {
console.log("Last processed historyId:", lastProcessedHistoryId);

// Use the minimum of lastProcessedHistoryId and the received historyId
const startHistoryId = Math.min(
let startHistoryId = Math.min(
parseInt(lastProcessedHistoryId),
parseInt(receivedHistoryId),
);
console.log("Using startHistoryId:", startHistoryId);

// Fetch the history
const historyResponse = await this.gmail.listHistory({
startHistoryId,
historyTypes: [
"messageAdded",
],
labelId: this.label,
});
let historyResponse;
try {
historyResponse = await this.getHistoryResponse(startHistoryId);
} catch {
// catch error thrown if startHistoryId is invalid or expired

// emit recent messages to attempt to avoid missing any messages
await this.emitRecentMessages();

// set startHistoryId to the historyId received from the webhook
startHistoryId = parseInt(receivedHistoryId);
console.log("Using startHistoryId:", startHistoryId);
historyResponse = await this.getHistoryResponse(startHistoryId);
}

console.log(
"History response:",
Expand Down
Loading