Skip to content

Commit 6d0f7fe

Browse files
Moving clientId, updating language
1 parent 97b8755 commit 6d0f7fe

File tree

4 files changed

+81
-49
lines changed

4 files changed

+81
-49
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
const restrictedClientId = "38931588176-fnd4m13k1mjb6djallp1m9kr7o8kslcu.apps.googleusercontent.com";
1+
import { PD_OFFICIAL_GMAIL_OAUTH_CLIENT_ID } from "@pipedream/platform";
22

33
export default {
44
methods: {
55
async checkClientId() {
6-
return this.gmail.$auth.oauth_client_id !== restrictedClientId;
6+
return (
7+
this.gmail.$auth.oauth_client_id !== PD_OFFICIAL_GMAIL_OAUTH_CLIENT_ID
8+
);
79
},
810
},
911
};

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

Lines changed: 74 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import gmail from "../../gmail.app.mjs";
22
import common from "../common/polling-history.mjs";
33
import {
4-
axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, ConfigurationError,
4+
axios,
5+
DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
6+
ConfigurationError,
57
} from "@pipedream/platform";
68
import { PubSub } from "@google-cloud/pubsub";
79
import { v4 as uuidv4 } from "uuid";
@@ -13,7 +15,7 @@ export default {
1315
name: "New Email Received",
1416
description: "Emit new event when a new email is received.",
1517
type: "source",
16-
version: "0.1.0",
18+
version: "0.1.1",
1719
dedupe: "unique",
1820
props: {
1921
gmail,
@@ -25,7 +27,8 @@ export default {
2527
"webhook",
2628
"polling",
2729
],
28-
description: "Configuring this trigger as a `webhook` requires a Custom OAuth client, which is available on Pipedream's [Advanced plan or higher](https://pipedream.com/pricing?plan=Advanced). Refer to the getting started guide [here](https://pipedream.com/apps/gmail/triggers/new-email-received#getting-started).",
30+
description:
31+
"Configuring this source as a `webhook` (instant) trigger requires a custom OAuth client. [Refer to the guide here to get started](https://pipedream.com/apps/gmail/triggers/new-email-received#getting-started).",
2932
reloadProps: true,
3033
},
3134
serviceAccountKeyJson: {
@@ -48,7 +51,8 @@ export default {
4851
topicType: {
4952
type: "string",
5053
label: "Pub/Sub Topic",
51-
description: "Do you have an existing Pub/Sub topic, or would you like to create a new one?",
54+
description:
55+
"Do you have an existing Pub/Sub topic, or would you like to create a new one?",
5256
options: [
5357
"existing",
5458
"new",
@@ -93,7 +97,8 @@ export default {
9397
latencyWarningAlert: {
9498
type: "alert",
9599
alertType: "warning",
96-
content: "Please allow up to 1 minute for deployment. We're setting up your real-time email notifications behind the scenes.",
100+
content:
101+
"Please allow up to 1 minute for deployment. We're setting up your real-time email notifications behind the scenes.",
97102
hidden: true,
98103
},
99104
},
@@ -111,7 +116,9 @@ export default {
111116
// verify that a Custom OAuth client is being used
112117
const isValidClientId = await this.checkClientId();
113118
if (!isValidClientId) {
114-
throw new ConfigurationError("You must use a custom OAuth client to use this component. Please see [here](https://pipedream.com/docs/connected-accounts/oauth-clients) for more details.");
119+
throw new ConfigurationError(
120+
"Configuring this source as a `webhook` (instant) trigger requires a custom OAuth client. [Refer to the guide here to get started](https://pipedream.com/apps/gmail/triggers/new-email-received#getting-started).",
121+
);
115122
}
116123

117124
newProps.http = "$.interface.http";
@@ -143,7 +150,9 @@ export default {
143150
if (this.topicType === "new") {
144151
const authKeyJSON = JSON.parse(this.serviceAccountKeyJson);
145152
const { project_id: projectId } = authKeyJSON;
146-
topicName = `projects/${projectId}/topics/${this.convertNameToValidPubSubTopicName(uuidv4())}`;
153+
topicName = `projects/${projectId}/topics/${this.convertNameToValidPubSubTopicName(
154+
uuidv4(),
155+
)}`;
147156
props.topic.default = topicName;
148157
props.topic.reloadProps = false;
149158
} else {
@@ -160,9 +169,14 @@ export default {
160169
const [
161170
policy,
162171
] = await topic.iam.getPolicy();
163-
hasPublisherRole = policy.bindings.find(({
164-
members, role,
165-
}) => members.includes("serviceAccount:[email protected]") && role === "roles/pubsub.publisher");
172+
hasPublisherRole = policy.bindings.find(
173+
({
174+
members, role,
175+
}) =>
176+
members.includes(
177+
"serviceAccount:[email protected]",
178+
) && role === "roles/pubsub.publisher",
179+
);
166180
} catch {
167181
console.log("Could not retrieve iam policy");
168182
}
@@ -216,7 +230,8 @@ export default {
216230

217231
// Create subscription
218232
const pushEndpoint = this.http.endpoint;
219-
const subscriptionName = this.convertNameToValidPubSubTopicName(pushEndpoint);
233+
const subscriptionName =
234+
this.convertNameToValidPubSubTopicName(pushEndpoint);
220235
const subscriptionOptions = {
221236
pushConfig: {
222237
pushEndpoint,
@@ -267,9 +282,7 @@ export default {
267282
sdkParams() {
268283
const authKeyJSON = JSON.parse(this.serviceAccountKeyJson);
269284
const {
270-
project_id: projectId,
271-
client_email,
272-
private_key,
285+
project_id: projectId, client_email, private_key,
273286
} = authKeyJSON;
274287
const sdkParams = {
275288
credentials: {
@@ -291,18 +304,18 @@ export default {
291304
},
292305
convertNameToValidPubSubTopicName(name) {
293306
// For valid names, see https://cloud.google.com/pubsub/docs/admin#resource_names
294-
return name
295-
// Must not start with `goog`. We add a `pd-` at the beginning if that's the case.
296-
.replace(/(^goog.*)/g, "pd-$1")
297-
// Must start with a letter, otherwise we add `pd-` at the beginning.
298-
.replace(/^(?![a-zA-Z]+)/, "pd-")
299-
// Only certain characters are allowed, the rest will be replaced with a `-`.
300-
.replace(/[^a-zA-Z0-9_\-.~+%]+/g, "-");
307+
return (
308+
name
309+
// Must not start with `goog`. We add a `pd-` at the beginning if that's the case.
310+
.replace(/(^goog.*)/g, "pd-$1")
311+
// Must start with a letter, otherwise we add `pd-` at the beginning.
312+
.replace(/^(?![a-zA-Z]+)/, "pd-")
313+
// Only certain characters are allowed, the rest will be replaced with a `-`.
314+
.replace(/[^a-zA-Z0-9_\-.~+%]+/g, "-")
315+
);
301316
},
302317
makeRequest({
303-
$ = this,
304-
path,
305-
...opts
318+
$ = this, path, ...opts
306319
}) {
307320
return axios($, {
308321
url: `https://gmail.googleapis.com/gmail/v1${path}`,
@@ -339,7 +352,8 @@ export default {
339352
] = await pubSubClient.createTopic(topicName);
340353
console.log(`Topic ${topicName} created.`);
341354
} catch (error) {
342-
if (error.code === 6) { // Already exists
355+
if (error.code === 6) {
356+
// Already exists
343357
topic = pubSubClient.topic(topicName);
344358
} else {
345359
throw error;
@@ -354,7 +368,8 @@ export default {
354368
return {
355369
id: msg.id,
356370
threadId: msg.threadId,
357-
subject: headers.find((h) => h.name.toLowerCase() === "subject")?.value,
371+
subject: headers.find((h) => h.name.toLowerCase() === "subject")
372+
?.value,
358373
from: headers.find((h) => h.name.toLowerCase() === "from")?.value,
359374
to: headers.find((h) => h.name.toLowerCase() === "to")?.value,
360375
date: headers.find((h) => h.name.toLowerCase() === "date")?.value,
@@ -370,16 +385,18 @@ export default {
370385
generateMeta(message) {
371386
return {
372387
id: message.id,
373-
summary: `A new message with ID: ${message.id} was received"`,
388+
summary: message.snippet,
374389
ts: message.internalDate,
375390
};
376391
},
377392
filterHistory(history) {
378393
return this.label
379-
? history.filter((item) =>
380-
item.messagesAdded?.length
381-
&& item.messagesAdded[0].message.labelIds
382-
&& item.messagesAdded[0].message.labelIds.includes(this.label))
394+
? history.filter(
395+
(item) =>
396+
item.messagesAdded?.length &&
397+
item.messagesAdded[0].message.labelIds &&
398+
item.messagesAdded[0].message.labelIds.includes(this.label),
399+
)
383400
: history.filter((item) => item.messagesAdded?.length);
384401
},
385402
},
@@ -414,7 +431,9 @@ export default {
414431
if (!pubsubMessage) {
415432
return;
416433
}
417-
const decodedData = JSON.parse(Buffer.from(pubsubMessage.data, "base64").toString());
434+
const decodedData = JSON.parse(
435+
Buffer.from(pubsubMessage.data, "base64").toString(),
436+
);
418437

419438
console.log("Decoded Pub/Sub data:", decodedData);
420439

@@ -425,8 +444,10 @@ export default {
425444
console.log("Last processed historyId:", lastProcessedHistoryId);
426445

427446
// Use the minimum of lastProcessedHistoryId and the received historyId
428-
const startHistoryId =
429-
Math.min(parseInt(lastProcessedHistoryId), parseInt(receivedHistoryId));
447+
const startHistoryId = Math.min(
448+
parseInt(lastProcessedHistoryId),
449+
parseInt(receivedHistoryId),
450+
);
430451
console.log("Using startHistoryId:", startHistoryId);
431452

432453
// Fetch the history
@@ -438,14 +459,19 @@ export default {
438459
labelId: this.label,
439460
});
440461

441-
console.log("History response:", JSON.stringify(historyResponse, null, 2));
462+
console.log(
463+
"History response:",
464+
JSON.stringify(historyResponse, null, 2),
465+
);
442466

443467
// Process history to find new messages
444468
const newMessages = [];
445469
if (historyResponse.history) {
446470
for (const historyItem of historyResponse.history) {
447471
if (historyItem.messagesAdded) {
448-
newMessages.push(...historyItem.messagesAdded.map((msg) => msg.message));
472+
newMessages.push(
473+
...historyItem.messagesAdded.map((msg) => msg.message),
474+
);
449475
}
450476
}
451477
}
@@ -466,15 +492,18 @@ export default {
466492
console.log("Updated lastProcessedHistoryId:", latestHistoryId);
467493

468494
if (processedEmails?.length) {
469-
this.$emit({
470-
newEmailsCount: processedEmails.length,
471-
emails: processedEmails,
472-
lastProcessedHistoryId: latestHistoryId,
473-
}, {
474-
id: processedEmails[0].id,
475-
summary: processedEmails[0].subject,
476-
ts: Date.now(),
477-
});
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+
);
478507
}
479508
}
480509
},

platform/lib/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const DEFAULT_POLLING_SOURCE_TIMER_INTERVAL = 60 * 15
1+
export const DEFAULT_POLLING_SOURCE_TIMER_INTERVAL = 60 * 15;
2+
export const PD_OFFICIAL_GMAIL_OAUTH_CLIENT_ID = "38931588176-fnd4m13k1mjb6djallp1m9kr7o8kslcu.apps.googleusercontent.com";

platform/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/platform",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Pipedream platform globals (typing and runtime type checking)",
55
"homepage": "https://pipedream.com",
66
"main": "dist/index.js",

0 commit comments

Comments
 (0)