Skip to content

Commit e1f32e0

Browse files
committed
[FIX] Gmail Upgraded lib version
fixed historyId
1 parent e15ccad commit e1f32e0

File tree

20 files changed

+240
-101
lines changed

20 files changed

+240
-101
lines changed

components/gmail/actions/add-label-to-email/add-label-to-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "gmail-add-label-to-email",
55
name: "Add Label to Email",
66
description: "Add label(s) to an email message. [See the docs](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify)",
7-
version: "0.0.9",
7+
version: "0.0.10",
88
type: "action",
99
props: {
1010
gmail,

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

Lines changed: 1 addition & 1 deletion
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.5",
7+
version: "0.0.7",
88
type: "action",
99
props: {
1010
gmail,

components/gmail/actions/archive-email/archive-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "gmail-archive-email",
66
name: "Archive Email",
77
description: "Archive an email message. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify)",
8-
version: "0.0.3",
8+
version: "0.0.5",
99
type: "action",
1010
props: {
1111
gmail,

components/gmail/actions/create-draft/create-draft.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-create-draft",
77
name: "Create Draft",
88
description: "Create a draft from your Google Workspace email account. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.drafts/create)",
9-
version: "0.1.3",
9+
version: "0.1.5",
1010
type: "action",
1111
props: {
1212
gmail,

components/gmail/actions/download-attachment/download-attachment.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
key: "gmail-download-attachment",
88
name: "Download Attachment",
99
description: "Download an attachment by attachmentId to the /tmp directory. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.messages.attachments/get)",
10-
version: "0.0.7",
10+
version: "0.0.9",
1111
type: "action",
1212
props: {
1313
gmail,

components/gmail/actions/find-email/find-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-find-email",
77
name: "Find Email",
88
description: "Find an email using Google's Search Engine. [See the docs](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/list)",
9-
version: "0.1.1",
9+
version: "0.1.3",
1010
type: "action",
1111
props: {
1212
gmail,

components/gmail/actions/list-labels/list-labels.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "gmail-list-labels",
55
name: "List Labels",
66
description: "List all the existing labels in the connected account. [See the docs](https://developers.google.com/gmail/api/reference/rest/v1/users.labels/list)",
7-
version: "0.0.4",
7+
version: "0.0.6",
88
type: "action",
99
props: {
1010
gmail,

components/gmail/actions/remove-label-from-email/remove-label-from-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "gmail-remove-label-from-email",
55
name: "Remove Label from Email",
66
description: "Remove label(s) from an email message. [See the docs](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/modify)",
7-
version: "0.0.6",
7+
version: "0.0.8",
88
type: "action",
99
props: {
1010
gmail,

components/gmail/actions/send-email/send-email.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
key: "gmail-send-email",
77
name: "Send Email",
88
description: "Send an email from your Google Workspace email account. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send)",
9-
version: "0.1.11",
9+
version: "0.1.13",
1010
type: "action",
1111
props: {
1212
gmail,

components/gmail/actions/update-org-signature/update-org-signature.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
name: "Update Signature for Email in Organization",
99
description: `Update the signature for a specific email address in an organization.
1010
A Google Cloud service account with delegated domain-wide authority is required for this action. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/update)`,
11-
version: "0.0.7",
11+
version: "0.0.9",
1212
type: "action",
1313
props: {
1414
gmail,

components/gmail/actions/update-primary-signature/update-primary-signature.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "gmail-update-primary-signature",
55
name: "Update Signature for Primary Email Address",
66
description: "Update the signature for the primary email address. [See the documentation](https://developers.google.com/gmail/api/reference/rest/v1/users.settings.sendAs/update)",
7-
version: "0.0.7",
7+
version: "0.0.9",
88
type: "action",
99
props: {
1010
gmail,

components/gmail/gmail.app.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ export default {
397397
});
398398
return data;
399399
},
400+
async getProfile() {
401+
const { data } = await this._client().users.getProfile({
402+
userId: constants.USER_ID,
403+
});
404+
return data;
405+
},
400406
async getMessageSubject({ id }) {
401407
const message = await this.getMessage({
402408
id,

components/gmail/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/gmail",
3-
"version": "0.3.4",
3+
"version": "1.0.0",
44
"description": "Pipedream Gmail Components",
55
"main": "gmail.app.mjs",
66
"keywords": [
@@ -15,10 +15,9 @@
1515
"dependencies": {
1616
"@google-cloud/local-auth": "^2.1.0",
1717
"@google-cloud/pubsub": "^4.7.0",
18-
"@googleapis/gmail": "^0.3.4",
18+
"@googleapis/gmail": "^13.0.1",
1919
"@pipedream/platform": "^3.0.3",
2020
"google-auth-library": "^8.7.0",
21-
"googleapis": "^105.0.0",
2221
"html-to-text": "^9.0.5",
2322
"mime": "^3.0.0",
2423
"nodemailer": "^6.7.8",

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

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,53 @@ export default {
2626
const params = {
2727
maxResults: constants.HISTORICAL_EVENTS,
2828
};
29+
30+
let allMessages = [];
31+
2932
if (this.labels?.length) {
30-
params.labelIds = this.labels;
33+
// Make individual calls for each labelId to make it inclusive (OR logic)
34+
for (const labelId of this.labels) {
35+
const { messages } = await this.gmail.listMessages({
36+
...params,
37+
labelIds: [
38+
labelId,
39+
],
40+
});
41+
if (messages?.length) {
42+
allMessages.push(...messages);
43+
}
44+
}
45+
} else {
46+
// No labels specified, get all messages
47+
const { messages } = await this.gmail.listMessages(params);
48+
if (messages?.length) {
49+
allMessages.push(...messages);
50+
}
3151
}
32-
let { messages } = await this.gmail.listMessages({
33-
...params,
34-
});
35-
if (!messages?.length) {
36-
return;
52+
53+
if (!allMessages.length) {
54+
const { historyId } = await this.gmail.getProfile();
55+
return historyId;
3756
}
38-
const messageIds = messages.map(({ id }) => id);
39-
messages = await this.gmail.getMessages(messageIds);
40-
messages = messages.sort((a, b) => (a.internalDate - b.internalDate));
57+
58+
// Remove duplicates based on message id
59+
const uniqueMessages = allMessages.filter((message, index, self) =>
60+
index === self.findIndex((m) => m.id === message.id));
61+
62+
const messageIds = uniqueMessages.map(({ id }) => id);
63+
const messagesWithHistoryId = await this.gmail.getMessages(messageIds);
64+
const sortedMessages =
65+
Array.from(messagesWithHistoryId)
66+
.sort((a, b) => (Number(b.historyId) - Number(a.historyId)));
67+
4168
const { historyId } = await this.gmail.getMessage({
42-
id: messages[messages.length - 1].id,
69+
id: sortedMessages[0].id,
4370
});
4471
return historyId;
4572
},
4673
async emitHistories(startHistoryId) {
4774
const opts = {
48-
startHistoryId,
75+
startHistoryId: String(startHistoryId),
4976
historyTypes: this.getHistoryTypes(),
5077
};
5178

@@ -59,19 +86,42 @@ export default {
5986
opts.labelId = this.labels[i];
6087
}
6188

62-
const {
63-
history, historyId,
64-
} = await this.gmail.listHistory(opts);
89+
try {
90+
const {
91+
history, historyId,
92+
} = await this.gmail.listHistory(opts);
6593

66-
if (!history) {
67-
continue;
68-
}
94+
maxHistoryId = Math.max(maxHistoryId, historyId);
6995

70-
maxHistoryId = Math.max(maxHistoryId, historyId);
71-
const responseArray = this.filterHistory(history);
96+
if (!history) {
97+
continue;
98+
}
7299

73-
for (const item of responseArray) {
74-
await this.emitFullMessage(item.messages[0].id);
100+
const responseArray = this.filterHistory(history);
101+
102+
for (const item of responseArray) {
103+
await this.emitFullMessage(item.messages[0].id);
104+
}
105+
} catch (error) {
106+
// Handle expired or invalid historyId (HTTP 404)
107+
if (error.status === 404 || error.message.includes("Requested entity was not found")) {
108+
console.log(`History request failed with expired historyId: ${error.message}`);
109+
console.log("Getting fresh historyId from profile");
110+
111+
// Get fresh historyId from profile
112+
const profile = await this.gmail.getProfile();
113+
const freshHistoryId = profile.historyId;
114+
115+
if (freshHistoryId) {
116+
this._setLastHistoryId(freshHistoryId);
117+
}
118+
119+
// Skip processing for this run since we're resetting the historyId
120+
return;
121+
} else {
122+
// Re-throw other errors
123+
throw error;
124+
}
75125
}
76126
}
77127
if (maxHistoryId > 0) {
@@ -82,15 +132,40 @@ export default {
82132
const opts = {
83133
maxResults: constants.HISTORICAL_EVENTS,
84134
};
135+
136+
let allMessages = [];
137+
85138
if (this.labels?.length) {
86-
opts.labelIds = this.labels;
139+
// Make individual calls for each labelId to make it inclusive (OR logic)
140+
for (const labelId of this.labels) {
141+
const { messages } = await this.gmail.listMessages({
142+
...opts,
143+
labelIds: [
144+
labelId,
145+
],
146+
});
147+
if (messages?.length) {
148+
allMessages.push(...messages);
149+
}
150+
}
151+
} else {
152+
// No labels specified, get all messages
153+
const { messages } = await this.gmail.listMessages(opts);
154+
if (messages?.length) {
155+
allMessages.push(...messages);
156+
}
87157
}
88-
let { messages } = await this.gmail.listMessages(opts);
89-
if (!messages?.length) {
158+
159+
if (!allMessages.length) {
90160
return;
91161
}
92-
messages = messages.sort((a, b) => (a.internalDate - b.internalDate));
93-
for (const message of messages) {
162+
163+
// Remove duplicates based on message id
164+
const uniqueMessages = allMessages.filter((message, index, self) =>
165+
index === self.findIndex((m) => m.id === message.id));
166+
167+
const sortedMessages = uniqueMessages.sort((a, b) => (a.internalDate - b.internalDate));
168+
for (const message of sortedMessages) {
94169
await this.emitFullMessage(message.id);
95170
}
96171
},

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

Lines changed: 1 addition & 1 deletion
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.12",
9+
version: "0.0.14",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {

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

Lines changed: 1 addition & 1 deletion
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.10",
9+
version: "0.0.12",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {

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

Lines changed: 1 addition & 1 deletion
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.2.5",
18+
version: "0.2.7",
1919
dedupe: "unique",
2020
props: {
2121
gmail,

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

Lines changed: 1 addition & 1 deletion
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.12",
11+
version: "0.0.13",
1212
dedupe: "unique",
1313
props: {
1414
gmail,

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

Lines changed: 1 addition & 1 deletion
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.11",
9+
version: "0.0.13",
1010
type: "source",
1111
dedupe: "unique",
1212
props: {

0 commit comments

Comments
 (0)