@pipedream/google_calendar 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/create-event/create-event.mjs +128 -0
- package/actions/delete-event/delete-event.mjs +37 -0
- package/actions/get-calendar/get-calendar.mjs +27 -0
- package/actions/get-event/get-event.mjs +37 -0
- package/actions/list-calendars/list-calendars.mjs +19 -0
- package/actions/query-free-busy-calendars/query-free-busy-calendars.mjs +34 -0
- package/actions/quick-add-event/quick-add-event.mjs +33 -0
- package/actions/update-event-attendees/update-event-attendees.mjs +74 -0
- package/common/constants.mjs +83 -0
- package/google_calendar.app.mjs +456 -0
- package/package.json +22 -20
- package/sources/common.mjs +74 -0
- package/sources/event-cancelled/event-cancelled.mjs +29 -0
- package/sources/event-ended/event-ended.mjs +35 -0
- package/sources/event-start/event-start.mjs +35 -0
- package/sources/new-calendar/new-calendar.mjs +68 -0
- package/sources/new-event/new-event.mjs +27 -0
- package/sources/new-event-search/new-event-search.mjs +38 -0
- package/sources/new-or-updated-event/new-or-updated-event.mjs +39 -0
- package/sources/new-or-updated-event-instant/new-or-updated-event-instant.mjs +289 -0
- package/google_calendar.app.js +0 -154
- package/sources/event-cancelled/event-cancelled.js +0 -65
- package/sources/event-ended/event-ended.js +0 -68
- package/sources/event-search/new-event-search.js +0 -70
- package/sources/event-start/event-start.js +0 -68
- package/sources/new-calendar/new-calendar.js +0 -50
- package/sources/new-event/new-event.js +0 -65
- package/sources/new-or-updated-event/new-or-updated-event.js +0 -64
- package/sources/new-or-updated-event-instant/new-or-updated-event-instant.js +0 -200
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-event-ended",
|
|
6
|
-
name: "Event Ended",
|
|
7
|
-
description: "Emits when an event ends",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
dedupe: "unique", // Dedupe events based on the Google Calendar event ID
|
|
10
|
-
props: {
|
|
11
|
-
googleCalendar,
|
|
12
|
-
calendarId: {
|
|
13
|
-
type: "string",
|
|
14
|
-
async options() {
|
|
15
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
16
|
-
const calendars = _.get(calListResp, "data.items");
|
|
17
|
-
if (calendars) {
|
|
18
|
-
const calendarIds = calendars.map((item) => {
|
|
19
|
-
return { value: item.id, label: item.summary };
|
|
20
|
-
});
|
|
21
|
-
return calendarIds;
|
|
22
|
-
}
|
|
23
|
-
return [];
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
timer: {
|
|
27
|
-
type: "$.interface.timer",
|
|
28
|
-
default: {
|
|
29
|
-
intervalSeconds: 5 * 60,
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
async run(event) {
|
|
34
|
-
const intervalMs = 1000 * (event.interval_seconds || 300); // fall through to default for manual testing
|
|
35
|
-
const now = new Date();
|
|
36
|
-
|
|
37
|
-
const timeMin = new Date(now.getTime() - intervalMs).toISOString();
|
|
38
|
-
const timeMax = new Date(now.getTime()).toISOString();
|
|
39
|
-
|
|
40
|
-
const config = {
|
|
41
|
-
calendarId: this.calendarId,
|
|
42
|
-
timeMax,
|
|
43
|
-
timeMin,
|
|
44
|
-
singleEvents: true,
|
|
45
|
-
orderBy: this.orderBy,
|
|
46
|
-
};
|
|
47
|
-
const resp = await this.googleCalendar.getEvents(config);
|
|
48
|
-
|
|
49
|
-
const events = _.get(resp.data, "items");
|
|
50
|
-
if (Array.isArray(events)) {
|
|
51
|
-
for (const event of events) {
|
|
52
|
-
const eventEnd = _.get(event, "end.dateTime");
|
|
53
|
-
const end = new Date(eventEnd);
|
|
54
|
-
const msFromEnd = now.getTime() - end.getTime();
|
|
55
|
-
if (eventEnd && msFromEnd > 0 && msFromEnd < intervalMs) {
|
|
56
|
-
const { summary, id } = event;
|
|
57
|
-
this.$emit(event, {
|
|
58
|
-
summary,
|
|
59
|
-
id,
|
|
60
|
-
ts: +new Date(event.start.dateTime),
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
console.log("nothing to emit");
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
};
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-new-event-search",
|
|
6
|
-
name: "Event Search",
|
|
7
|
-
description: "Emit when an event is created that matches a search",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
dedupe: "unique", // Dedupe events based on the Google Calendar event ID
|
|
10
|
-
props: {
|
|
11
|
-
googleCalendar,
|
|
12
|
-
q: {
|
|
13
|
-
propDefinition: [googleCalendar, "q"],
|
|
14
|
-
},
|
|
15
|
-
calendarId: {
|
|
16
|
-
type: "string",
|
|
17
|
-
async options() {
|
|
18
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
19
|
-
const calendars = _.get(calListResp, "data.items");
|
|
20
|
-
if (calendars) {
|
|
21
|
-
const calendarIds = calendars.map((item) => {
|
|
22
|
-
return { value: item.id, label: item.summary };
|
|
23
|
-
});
|
|
24
|
-
return calendarIds;
|
|
25
|
-
}
|
|
26
|
-
return [];
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
timer: {
|
|
30
|
-
type: "$.interface.timer",
|
|
31
|
-
default: {
|
|
32
|
-
intervalSeconds: 5 * 60, // five minutes
|
|
33
|
-
},
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
async run(event) {
|
|
37
|
-
const intervalMs = 1000 * (event.interval_seconds || 300); // fall through to default for manual testing
|
|
38
|
-
const now = new Date();
|
|
39
|
-
const past = new Date(now.getTime() - intervalMs);
|
|
40
|
-
|
|
41
|
-
const updatedMin = past.toISOString();
|
|
42
|
-
|
|
43
|
-
const config = {
|
|
44
|
-
calendarId: this.calendarId,
|
|
45
|
-
updatedMin,
|
|
46
|
-
q: this.q,
|
|
47
|
-
singleEvents: true,
|
|
48
|
-
orderBy: "startTime",
|
|
49
|
-
};
|
|
50
|
-
const resp = await this.googleCalendar.getEvents(config);
|
|
51
|
-
|
|
52
|
-
const events = _.get(resp.data, "items");
|
|
53
|
-
if (Array.isArray(events)) {
|
|
54
|
-
for (const event of events) {
|
|
55
|
-
const created = new Date(event.created);
|
|
56
|
-
// created in last 5 mins and not cancelled
|
|
57
|
-
if (created > past && event.status !== "cancelled") {
|
|
58
|
-
const { summary, id } = event;
|
|
59
|
-
this.$emit(event, {
|
|
60
|
-
summary,
|
|
61
|
-
id,
|
|
62
|
-
ts: +new Date(event.start.dateTime),
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
} else {
|
|
67
|
-
console.log("nothing to emit");
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
};
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-event-start",
|
|
6
|
-
name: "Event Start",
|
|
7
|
-
description: "Emits a specified time before an event starts",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
dedupe: "unique", // Dedupe events based on the Google Calendar event ID
|
|
10
|
-
props: {
|
|
11
|
-
googleCalendar,
|
|
12
|
-
calendarId: {
|
|
13
|
-
type: "string",
|
|
14
|
-
async options() {
|
|
15
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
16
|
-
const calendars = _.get(calListResp, "data.items");
|
|
17
|
-
if (calendars) {
|
|
18
|
-
const calendarIds = calendars.map((item) => {
|
|
19
|
-
return { value: item.id, label: item.summary };
|
|
20
|
-
});
|
|
21
|
-
return calendarIds;
|
|
22
|
-
}
|
|
23
|
-
return [];
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
timer: {
|
|
27
|
-
type: "$.interface.timer",
|
|
28
|
-
default: {
|
|
29
|
-
intervalSeconds: 5 * 60, // five minutes
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
async run(event) {
|
|
34
|
-
const intervalMs = 1000 * (event.interval_seconds || 300); // fall through to default for manual testing
|
|
35
|
-
const now = new Date();
|
|
36
|
-
|
|
37
|
-
const timeMin = now.toISOString();
|
|
38
|
-
const timeMax = new Date(now.getTime() + intervalMs).toISOString();
|
|
39
|
-
|
|
40
|
-
const config = {
|
|
41
|
-
calendarId: this.calendarId,
|
|
42
|
-
timeMax,
|
|
43
|
-
timeMin,
|
|
44
|
-
singleEvents: true,
|
|
45
|
-
orderBy: "startTime",
|
|
46
|
-
};
|
|
47
|
-
const resp = await this.googleCalendar.getEvents(config);
|
|
48
|
-
|
|
49
|
-
const events = _.get(resp.data, "items");
|
|
50
|
-
if (Array.isArray(events)) {
|
|
51
|
-
for (const event of events) {
|
|
52
|
-
const eventStart = _.get(event, "start.dateTime");
|
|
53
|
-
start = new Date(eventStart);
|
|
54
|
-
const msFromStart = start.getTime() - now.getTime();
|
|
55
|
-
if (eventStart && msFromStart > 0 && msFromStart < intervalMs) {
|
|
56
|
-
const { summary, id } = event;
|
|
57
|
-
this.$emit(event, {
|
|
58
|
-
summary,
|
|
59
|
-
id,
|
|
60
|
-
ts: +new Date(event.start.dateTime),
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
console.log("nothing to emit");
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-new-calendar",
|
|
6
|
-
name: "New Calendar",
|
|
7
|
-
description: "Emit an event when a calendar is created.",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
props: {
|
|
10
|
-
db: "$.service.db",
|
|
11
|
-
googleCalendar,
|
|
12
|
-
timer: {
|
|
13
|
-
type: "$.interface.timer",
|
|
14
|
-
default: {
|
|
15
|
-
intervalSeconds: 5 * 60,
|
|
16
|
-
},
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
hooks: {
|
|
20
|
-
async activate() {
|
|
21
|
-
// get list of calendars
|
|
22
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
23
|
-
const calendars = _.get(calListResp, "data.items");
|
|
24
|
-
const calendarIds = calendars.map((item) => item.id);
|
|
25
|
-
this.db.set("calendarIds", calendarIds);
|
|
26
|
-
},
|
|
27
|
-
deactivate() {
|
|
28
|
-
this.db.set("calendarIds", []);
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
async run(event) {
|
|
32
|
-
const previousCalendarIds = this.db.get("calendarIds") || [];
|
|
33
|
-
|
|
34
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
35
|
-
const calendars = _.get(calListResp, "data.items");
|
|
36
|
-
const currentCalendarIds = [];
|
|
37
|
-
|
|
38
|
-
for (const calendar of calendars) {
|
|
39
|
-
currentCalendarIds.push(calendar.id);
|
|
40
|
-
if (!previousCalendarIds.includes(calendar.id)) {
|
|
41
|
-
const { summary, id } = calendar;
|
|
42
|
-
this.$emit(calendar, {
|
|
43
|
-
summary,
|
|
44
|
-
id,
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
this.db.set("calendarIds", currentCalendarIds);
|
|
49
|
-
},
|
|
50
|
-
};
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-new-event",
|
|
6
|
-
name: "New Event",
|
|
7
|
-
description: "Emits when an event is created",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
dedupe: "unique", // Dedupe events based on the Google Calendar event ID
|
|
10
|
-
props: {
|
|
11
|
-
googleCalendar,
|
|
12
|
-
calendarId: {
|
|
13
|
-
type: "string",
|
|
14
|
-
async options() {
|
|
15
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
16
|
-
const calendars = _.get(calListResp, "data.items");
|
|
17
|
-
if (calendars) {
|
|
18
|
-
const calendarIds = calendars.map((item) => {
|
|
19
|
-
return { value: item.id, label: item.summary };
|
|
20
|
-
});
|
|
21
|
-
return calendarIds;
|
|
22
|
-
}
|
|
23
|
-
return [];
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
timer: {
|
|
27
|
-
type: "$.interface.timer",
|
|
28
|
-
default: {
|
|
29
|
-
intervalSeconds: 5 * 60, // five minutes
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
async run(event) {
|
|
34
|
-
const intervalMs = 1000 * (event.interval_seconds || 300); // fall through to default for manual testing
|
|
35
|
-
const now = new Date();
|
|
36
|
-
const past = new Date(now.getTime() - intervalMs);
|
|
37
|
-
|
|
38
|
-
const updatedMin = past.toISOString();
|
|
39
|
-
|
|
40
|
-
const config = {
|
|
41
|
-
calendarId: this.calendarId,
|
|
42
|
-
updatedMin,
|
|
43
|
-
singleEvents: true,
|
|
44
|
-
orderBy: "startTime",
|
|
45
|
-
};
|
|
46
|
-
const resp = await this.googleCalendar.getEvents(config);
|
|
47
|
-
|
|
48
|
-
const events = _.get(resp.data, "items");
|
|
49
|
-
if (Array.isArray(events)) {
|
|
50
|
-
for (const event of events) {
|
|
51
|
-
const created = new Date(event.created);
|
|
52
|
-
if (created > past && event.status !== "cancelled") {
|
|
53
|
-
const { summary, id } = event;
|
|
54
|
-
this.$emit(event, {
|
|
55
|
-
summary,
|
|
56
|
-
id,
|
|
57
|
-
ts: +new Date(event.start.dateTime),
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
} else {
|
|
62
|
-
console.log("nothing to emit");
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
3
|
-
|
|
4
|
-
module.exports = {
|
|
5
|
-
key: "google_calendar-new-or-updated-event",
|
|
6
|
-
name: "New or Updated Event",
|
|
7
|
-
description: "Emits when an event is created or updated (except when it's cancelled)",
|
|
8
|
-
version: "0.0.1",
|
|
9
|
-
dedupe: "unique", // Dedupe events based on the Google Calendar event ID
|
|
10
|
-
props: {
|
|
11
|
-
googleCalendar,
|
|
12
|
-
calendarId: {
|
|
13
|
-
type: "string",
|
|
14
|
-
async options() {
|
|
15
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
16
|
-
const calendars = _.get(calListResp, "data.items");
|
|
17
|
-
if (calendars) {
|
|
18
|
-
const calendarIds = calendars.map((item) => {
|
|
19
|
-
return { value: item.id, label: item.summary };
|
|
20
|
-
});
|
|
21
|
-
return calendarIds;
|
|
22
|
-
}
|
|
23
|
-
return [];
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
timer: {
|
|
27
|
-
type: "$.interface.timer",
|
|
28
|
-
default: {
|
|
29
|
-
intervalSeconds: 5 * 60, // five minutes
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
async run(event) {
|
|
34
|
-
const intervalMs = 1000 * (event.interval_seconds || 300); // fall through to default for manual testing
|
|
35
|
-
const now = new Date();
|
|
36
|
-
const past = new Date(now.getTime() - intervalMs);
|
|
37
|
-
|
|
38
|
-
const updatedMin = past.toISOString();
|
|
39
|
-
|
|
40
|
-
const config = {
|
|
41
|
-
calendarId: this.calendarId,
|
|
42
|
-
updatedMin,
|
|
43
|
-
singleEvents: true,
|
|
44
|
-
orderBy: "startTime",
|
|
45
|
-
};
|
|
46
|
-
const resp = await this.googleCalendar.getEvents(config);
|
|
47
|
-
|
|
48
|
-
const events = _.get(resp.data, "items");
|
|
49
|
-
if (Array.isArray(events)) {
|
|
50
|
-
for (const event of events) {
|
|
51
|
-
if (event.status !== "cancelled") {
|
|
52
|
-
const { summary, id } = event;
|
|
53
|
-
this.$emit(event, {
|
|
54
|
-
summary,
|
|
55
|
-
id,
|
|
56
|
-
ts: +new Date(event.start.dateTime),
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
} else {
|
|
61
|
-
console.log("nothing to emit");
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
};
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
const get = require("lodash.get");
|
|
2
|
-
const { v4: uuidv4 } = require("uuid");
|
|
3
|
-
const googleCalendar = require("../../google_calendar.app.js");
|
|
4
|
-
|
|
5
|
-
module.exports = {
|
|
6
|
-
key: "google_calendar-new-or-updated-event-instant",
|
|
7
|
-
name: "New or Updated Event (Instant)",
|
|
8
|
-
description:
|
|
9
|
-
"Emits when an event is created or updated (except when it's cancelled)",
|
|
10
|
-
version: "0.0.3",
|
|
11
|
-
props: {
|
|
12
|
-
googleCalendar,
|
|
13
|
-
db: "$.service.db",
|
|
14
|
-
calendarId: {
|
|
15
|
-
type: "string",
|
|
16
|
-
label: "Calendar",
|
|
17
|
-
async options() {
|
|
18
|
-
const calListResp = await this.googleCalendar.calendarList();
|
|
19
|
-
const calendars = get(calListResp, "data.items");
|
|
20
|
-
if (calendars) {
|
|
21
|
-
const calendarIds = calendars.map((item) => {
|
|
22
|
-
return { value: item.id, label: item.summary };
|
|
23
|
-
});
|
|
24
|
-
return calendarIds;
|
|
25
|
-
}
|
|
26
|
-
return [];
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
newOnly: {
|
|
30
|
-
label: "New events only?",
|
|
31
|
-
type: "boolean",
|
|
32
|
-
description: "Emit new events only, and not updates to existing events",
|
|
33
|
-
},
|
|
34
|
-
http: "$.interface.http",
|
|
35
|
-
timer: {
|
|
36
|
-
label: "Push notification renewal schedule",
|
|
37
|
-
description:
|
|
38
|
-
"The Google Calendar API requires occasional renewal of push notification subscriptions. **This runs in the background, so you should not need to modify this schedule**.",
|
|
39
|
-
type: "$.interface.timer",
|
|
40
|
-
default: {
|
|
41
|
-
intervalSeconds: 60 * 60 * 24,
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
hooks: {
|
|
46
|
-
async activate() {
|
|
47
|
-
// make watch request that will hit this http interface
|
|
48
|
-
const config = {
|
|
49
|
-
calendarId: this.calendarId,
|
|
50
|
-
requestBody: {
|
|
51
|
-
id: uuidv4(),
|
|
52
|
-
type: "web_hook",
|
|
53
|
-
address: this.http.endpoint,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
const watchResp = await this.googleCalendar.watch(config);
|
|
57
|
-
const data = watchResp.data;
|
|
58
|
-
|
|
59
|
-
// initial full sync get next sync token
|
|
60
|
-
const nextSyncToken = await this.googleCalendar.fullSync(this.calendarId);
|
|
61
|
-
|
|
62
|
-
this.db.set("nextSyncToken", nextSyncToken);
|
|
63
|
-
this.db.set("channelId", data.id);
|
|
64
|
-
this.db.set("resourceId", data.resourceId);
|
|
65
|
-
this.db.set("expiration", data.expiration);
|
|
66
|
-
},
|
|
67
|
-
async deactivate() {
|
|
68
|
-
const id = this.db.get("channelId");
|
|
69
|
-
const resourceId = this.db.get("resourceId");
|
|
70
|
-
if (id && resourceId) {
|
|
71
|
-
const config = {
|
|
72
|
-
requestBody: {
|
|
73
|
-
id,
|
|
74
|
-
resourceId,
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
const stopResp = await this.googleCalendar.stop(config);
|
|
78
|
-
if (stopResp.status === 204) {
|
|
79
|
-
console.log("webhook deactivated");
|
|
80
|
-
this.db.set("nextSyncToken", null);
|
|
81
|
-
this.db.set("channelId", null);
|
|
82
|
-
this.db.set("resourceId", null);
|
|
83
|
-
this.db.set("expiration", null);
|
|
84
|
-
} else {
|
|
85
|
-
console.log("there was a problem deactivating the webhook");
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
async run(event) {
|
|
91
|
-
// refresh watch
|
|
92
|
-
if (event.interval_seconds) {
|
|
93
|
-
// get time
|
|
94
|
-
const now = new Date();
|
|
95
|
-
const intervalMs = event.interval_seconds * 1000;
|
|
96
|
-
// get expriration
|
|
97
|
-
const expiration = this.db.get("expiration");
|
|
98
|
-
const expireDate = new Date(parseInt(expiration));
|
|
99
|
-
|
|
100
|
-
// if now + interval > expiration, refresh watch
|
|
101
|
-
if (now.getTime() + intervalMs > expireDate.getTime()) {
|
|
102
|
-
// do the webhook refresh
|
|
103
|
-
const config = {
|
|
104
|
-
calendarId: this.calendarId,
|
|
105
|
-
requestBody: {
|
|
106
|
-
id: uuidv4(),
|
|
107
|
-
type: "web_hook",
|
|
108
|
-
address: this.http.endpoint,
|
|
109
|
-
},
|
|
110
|
-
};
|
|
111
|
-
const watchResp = await this.googleCalendar.watch(config);
|
|
112
|
-
const data = watchResp.data;
|
|
113
|
-
// full sync get next sync token
|
|
114
|
-
const nextSyncToken = await this.googleCalendar.fullSync(
|
|
115
|
-
this.calendarId
|
|
116
|
-
);
|
|
117
|
-
|
|
118
|
-
// stop the previous watch
|
|
119
|
-
const id = this.db.get("channelId");
|
|
120
|
-
const resourceId = this.db.get("resourceId");
|
|
121
|
-
if (id && resourceId) {
|
|
122
|
-
const config = {
|
|
123
|
-
requestBody: {
|
|
124
|
-
id,
|
|
125
|
-
resourceId,
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
const stopResp = await this.googleCalendar.stop(config);
|
|
129
|
-
if (stopResp.status === 204) {
|
|
130
|
-
console.log("webhook deactivated");
|
|
131
|
-
} else {
|
|
132
|
-
console.log("there was a problem deactivating the webhook");
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
this.db.set("nextSyncToken", nextSyncToken);
|
|
137
|
-
this.db.set("channelId", data.id);
|
|
138
|
-
this.db.set("resourceId", data.resourceId);
|
|
139
|
-
this.db.set("expiration", data.expiration);
|
|
140
|
-
}
|
|
141
|
-
} else {
|
|
142
|
-
// verify channel id
|
|
143
|
-
const expectedChannelId = this.db.get("channelId");
|
|
144
|
-
const channelId = get(event, "headers.x-goog-channel-id");
|
|
145
|
-
if (expectedChannelId != channelId) {
|
|
146
|
-
console.log(
|
|
147
|
-
`expected ${expectedChannelId} but got ${channelId}. Most likely there are multiple webhooks active.`
|
|
148
|
-
);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
// check that resource state is exists
|
|
152
|
-
const state = get(event, "headers.x-goog-resource-state");
|
|
153
|
-
switch (state) {
|
|
154
|
-
case "exists":
|
|
155
|
-
// there's something to emit
|
|
156
|
-
break;
|
|
157
|
-
case "not_exists":
|
|
158
|
-
// TODO handle this?
|
|
159
|
-
case "sync":
|
|
160
|
-
console.log("new channel created");
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
// do a listing and then emit everything?
|
|
164
|
-
const syncToken = this.db.get("nextSyncToken");
|
|
165
|
-
let nextSyncToken = null;
|
|
166
|
-
let nextPageToken = null;
|
|
167
|
-
while (!nextSyncToken) {
|
|
168
|
-
const listConfig = {
|
|
169
|
-
calendarId: this.calendarId,
|
|
170
|
-
syncToken,
|
|
171
|
-
};
|
|
172
|
-
listConfig.pageToken = nextPageToken;
|
|
173
|
-
const syncResp = await this.googleCalendar.list(listConfig);
|
|
174
|
-
if (syncResp.status == 410) {
|
|
175
|
-
nextSyncToken = await this.googleCalendar.fullSync(this.calendarId);
|
|
176
|
-
console.log("sync token is gone, resyncing");
|
|
177
|
-
break;
|
|
178
|
-
}
|
|
179
|
-
nextPageToken = get(syncResp, "data.nextPageToken");
|
|
180
|
-
nextSyncToken = get(syncResp, "data.nextSyncToken");
|
|
181
|
-
|
|
182
|
-
// loop and emit
|
|
183
|
-
const events = get(syncResp, "data.items");
|
|
184
|
-
if (Array.isArray(events)) {
|
|
185
|
-
for (const event of events) {
|
|
186
|
-
const { summary, id, updated, sequence } = event;
|
|
187
|
-
if (this.newOnly && sequence != 0) continue;
|
|
188
|
-
this.$emit(event, {
|
|
189
|
-
summary,
|
|
190
|
-
id,
|
|
191
|
-
ts: +new Date(updated),
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
this.db.set("nextSyncToken", nextSyncToken);
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
};
|