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.9",
"version": "0.1.10",
"description": "Pipedream Gmail Components",
"main": "gmail.app.mjs",
"keywords": [
Expand Down
55 changes: 47 additions & 8 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.6",
version: "0.1.7",
dedupe: "unique",
props: {
gmail,
Expand Down Expand Up @@ -121,11 +121,14 @@ export default {
);
}

newProps.http = "$.interface.http";
newProps.http = {
type: "$.interface.http",
customResponse: true,
};
newProps.timer = {
type: "$.interface.timer",
default: {
intervalSeconds: 24 * 60 * 60,
intervalSeconds: 60 * 60,
},
hidden: true,
};
Expand Down Expand Up @@ -203,12 +206,19 @@ export default {

props.latencyWarningAlert.hidden = false;

const historyId = await this.setupGmailNotifications(topicName);
const {
historyId, expiration,
} = await this.setupGmailNotifications(topicName);
newProps.initialHistoryId = {
type: "string",
default: historyId,
hidden: true,
};
newProps.expiration = {
type: "string",
default: expiration,
hidden: true,
};
}
}
props.label.hidden = false;
Expand Down Expand Up @@ -279,6 +289,18 @@ export default {
_setLastProcessedHistoryId(lastProcessedHistoryId) {
this.db.set("lastProcessedHistoryId", lastProcessedHistoryId);
},
_getExpiration() {
return this.db.get("expiration");
},
_setExpiration(expiration) {
this.db.set("expiration", expiration);
},
_getLastReceivedTime() {
return this.db.get("lastReceivedTime");
},
_setLastReceivedTime(lastReceivedTime) {
this.db.set("lastReceivedTime", lastReceivedTime);
},
sdkParams() {
const authKeyJSON = JSON.parse(this.serviceAccountKeyJson);
const {
Expand Down Expand Up @@ -338,7 +360,7 @@ export default {
},
});
console.log("Watch response:", watchResponse);
return watchResponse.historyId;
return watchResponse;
},
async getOrCreateTopic(name) {
const sdkParams = this.sdkParams();
Expand Down Expand Up @@ -413,17 +435,32 @@ export default {
// event was triggered by timer
const topicName = this._getTopicName();
if (topicName) {
// renew Gmail push notifications
await this.setupGmailNotifications(topicName);
// renew Gmail push notifications if expiring within the next hour
// or if no email has been received within the last hour
const currentExpiration = this._getExpiration();
const lastReceivedTime = this._getLastReceivedTime();
if (
(+currentExpiration < (event.timestamp + 3600) * 1000)
|| (lastReceivedTime < (event.timestamp - 3600) * 1000)
) {
const { expiration } = await this.setupGmailNotifications(topicName);
this._setExpiration(expiration);
}
return;
} else {
// first run, no need to renew push notifications
this._setTopicName(this.topic);
this._setLastProcessedHistoryId(this.initialHistoryId);
const initialHistoryId = this.initialHistoryId || this._getLastHistoryId();
this._setLastProcessedHistoryId(initialHistoryId);
this._setExpiration(this.expiration);
return;
}
}

this.http.respond({
status: 200,
});

// Extract the Pub/Sub message data
const pubsubMessage = event.body.message;
if (!pubsubMessage) {
Expand Down Expand Up @@ -491,6 +528,8 @@ export default {
this._setLastProcessedHistoryId(latestHistoryId);
console.log("Updated lastProcessedHistoryId:", latestHistoryId);

this._setLastReceivedTime(Date.now());

messageDetails.forEach((message) => {
if (message?.id) {
this.emitEvent(message);
Expand Down
Loading