Skip to content

Commit 0b409f1

Browse files
authored
Merging pull request #18699
* updates * pnpm-lock.yaml * update
1 parent c1ca75a commit 0b409f1

File tree

4 files changed

+166
-14
lines changed

4 files changed

+166
-14
lines changed

components/google_calendar/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/google_calendar",
3-
"version": "0.5.11",
3+
"version": "0.5.12",
44
"description": "Pipedream Google_calendar Components",
55
"main": "google_calendar.app.mjs",
66
"keywords": [
@@ -11,7 +11,7 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"dependencies": {
1313
"@googleapis/calendar": "^1.0.2",
14-
"@pipedream/platform": "^3.0.0",
14+
"@pipedream/platform": "^3.1.0",
1515
"color-2-name": "^1.4.4",
1616
"lodash.get": "^4.4.2",
1717
"moment-timezone": "^0.5.33",
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { axios } from "@pipedream/platform";
2+
import { v4 as uuid } from "uuid";
3+
4+
export default {
5+
methods: {
6+
async _makeAPIRequest({
7+
$ = this, apiKey, ...opts
8+
}) {
9+
if (!opts.headers) opts.headers = {};
10+
opts.headers["Authorization"] = `Bearer ${apiKey}`;
11+
opts.headers["Content-Type"] = "application/json";
12+
opts.headers["user-agent"] = "@PipedreamHQ/pipedream v0.1";
13+
const { path } = opts;
14+
delete opts.path;
15+
opts.url = `https://api.pipedream.com/v1${path[0] === "/"
16+
? ""
17+
: "/"
18+
}${path}`;
19+
return axios($, opts);
20+
},
21+
selfChannel() {
22+
return "self";
23+
},
24+
queuedEventsChannel() {
25+
return "$in";
26+
},
27+
async subscribe(emitter_id, listener_id, event_name = null, apiKey) {
28+
let params = {
29+
emitter_id,
30+
listener_id,
31+
};
32+
if (event_name) {
33+
params.event_name = event_name;
34+
}
35+
return await this._makeAPIRequest({
36+
method: "POST",
37+
path: "/subscriptions",
38+
params,
39+
apiKey,
40+
});
41+
},
42+
async selfSubscribe(apiKey) {
43+
const isSubscribedToSelf = this.db.get("isSubscribedToSelf");
44+
if (!isSubscribedToSelf) {
45+
const componentId = process.env.PD_COMPONENT;
46+
const selfChannel = this.selfChannel();
47+
console.log(`Subscribing to ${selfChannel} channel for event source`);
48+
console.log(
49+
await this.subscribe(componentId, componentId, selfChannel, apiKey),
50+
);
51+
this.db.set("isSubscribedToSelf", true);
52+
}
53+
},
54+
emitScheduleEvent(event, timestamp) {
55+
const selfChannel = this.selfChannel();
56+
const epoch = Date.parse(timestamp);
57+
const $id = uuid();
58+
59+
console.log(`Scheduled event to emit on: ${new Date(epoch)}`);
60+
61+
this.$emit(
62+
{
63+
...event,
64+
$channel: selfChannel,
65+
$id,
66+
},
67+
{
68+
name: selfChannel,
69+
id: $id,
70+
delivery_ts: epoch,
71+
},
72+
);
73+
74+
return $id;
75+
},
76+
async deleteScheduledEvent(event, apiKey) {
77+
const componentId = process.env.PD_COMPONENT;
78+
const inChannel = this.queuedEventsChannel();
79+
80+
// The user must pass a scheduled event UUID they'd like to cancel
81+
// We lookup the event by ID and delete it
82+
const { id } = event.body;
83+
84+
// List events in the $in channel - the queue of scheduled events, to be emitted in the future
85+
const events = await this.listEvents(
86+
componentId,
87+
inChannel,
88+
apiKey,
89+
);
90+
console.log("Events: ", events);
91+
92+
// Find the event in the list by id
93+
const eventToCancel = events.data.find((e) => {
94+
const { metadata } = e;
95+
return metadata.id === id;
96+
});
97+
98+
console.log("Event to cancel: ", eventToCancel);
99+
100+
if (!eventToCancel) {
101+
console.log(`No event with ${id} found`);
102+
return false;
103+
}
104+
105+
// Delete the event
106+
await this.deleteEvent(
107+
componentId,
108+
eventToCancel.id,
109+
inChannel,
110+
apiKey,
111+
);
112+
return true;
113+
},
114+
async listEvents(dcID, event_name, apiKey) {
115+
return await this._makeAPIRequest({
116+
path: `/sources/${dcID}/event_summaries`,
117+
params: {
118+
event_name,
119+
},
120+
apiKey,
121+
});
122+
},
123+
async deleteEvent(dcID, eventID, event_name, apiKey) {
124+
return await this._makeAPIRequest({
125+
method: "DELETE",
126+
path: `/sources/${dcID}/events`,
127+
params: {
128+
start_id: eventID,
129+
end_id: eventID,
130+
event_name,
131+
},
132+
apiKey,
133+
});
134+
},
135+
emitEvent(event, summary) {
136+
const id = event.$id;
137+
delete event.$channel;
138+
delete event.$id;
139+
140+
this.$emit(event, {
141+
summary: summary ?? JSON.stringify(event),
142+
id,
143+
ts: +new Date(),
144+
});
145+
},
146+
},
147+
};

components/google_calendar/sources/upcoming-event-alert/upcoming-event-alert.mjs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
import taskScheduler from "../../../pipedream/sources/new-scheduled-tasks/new-scheduled-tasks.mjs";
21
import googleCalendar from "../../google_calendar.app.mjs";
2+
import taskScheduler from "../common/taskScheduler.mjs";
33
import sampleEmit from "./test-event.mjs";
44

55
export default {
66
key: "google_calendar-upcoming-event-alert",
77
name: "New Upcoming Event Alert",
8-
description: `Emit new event based on a time interval before an upcoming event in the calendar. This source uses Pipedream's Task Scheduler.
9-
[See the documentation](https://pipedream.com/docs/examples/waiting-to-execute-next-step-of-workflow/#step-1-create-a-task-scheduler-event-source)
10-
for more information and instructions for connecting your Pipedream account.`,
11-
version: "0.0.10",
8+
description: "Emit new event based on a time interval before an upcoming event in the calendar.",
9+
version: "0.1.0",
1210
type: "source",
1311
props: {
14-
pipedream: taskScheduler.props.pipedream,
1512
googleCalendar,
1613
db: "$.service.db",
1714
http: "$.interface.http",
15+
pipedreamApiKey: {
16+
type: "string",
17+
label: "Pipedream API Key",
18+
description: "[Click here to find your Pipedream API key](https://pipedream.com/settings/user)",
19+
secret: true,
20+
},
1821
calendarId: {
1922
propDefinition: [
2023
googleCalendar,
@@ -55,11 +58,11 @@ export default {
5558
return;
5659
}
5760
for (const id of ids) {
58-
if (await this.deleteEvent({
61+
if (await this.deleteScheduledEvent({
5962
body: {
6063
id,
6164
},
62-
})) {
65+
}, this.pipedreamApiKey)) {
6366
console.log("Cancelled scheduled event");
6467
}
6568
}
@@ -112,7 +115,7 @@ export default {
112115
async run(event) {
113116
// self subscribe only on the first time
114117
if (!this._hasDeployed()) {
115-
await this.selfSubscribe();
118+
await this.selfSubscribe(this.pipedreamApiKey);
116119
}
117120

118121
const scheduledEventIds = this._getScheduledEventIds() || [];
@@ -121,7 +124,9 @@ export default {
121124
if (event.$channel === this.selfChannel()) {
122125
const remainingScheduledEventIds = scheduledEventIds.filter((id) => id !== event["$id"]);
123126
this._setScheduledEventIds(remainingScheduledEventIds);
124-
this.emitEvent(event, `Upcoming ${event.summary} event`);
127+
this.emitEvent(event, `Upcoming ${event.summary
128+
? event.summary + " "
129+
: ""}event`);
125130
return;
126131
}
127132

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)