forked from hk21702/YA-GCal-Notion-Sync-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
967 lines (840 loc) · 26.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
const NAME_NOTION = "Name";
const DATE_NOTION = "Date";
const TAGS_NOTION = "Tags";
const LOCATION_NOTION = "Location";
const DESCRIPTION_NOTION = "Description";
const LINK_NOTION = "Link";
const EVENT_ID_NOTION = "Event ID";
const CALENDAR_NAME_NOTION = "Calendar";
const CALENDAR_ID_NOTION = "Calendar ID";
const LAST_SYNC_NOTION = "Last Sync";
const ARCHIVE_CANCELLED_EVENTS = true;
const DELETE_CANCELLED_EVENTS = true;
const IGNORE_RECENTLY_PUSHED = true;
const SKIP_BAD_EVENTS = true;
// Deprecated
const FULL_SYNC = false;
const CANCELLED_TAG_NAME = "Cancelled/Removed";
const IGNORE_SYNC_TAG_NAME = "Ignore Sync";
// Relative to the time of last full sync in days.
const RELATIVE_MAX_DAY = 1095; // 3 years
const RELATIVE_MIN_DAY = 30;
function main() {
parseNotionProperties();
if (DELETE_CANCELLED_EVENTS) {
deleteCancelledEvents();
}
let modified_eIds = syncToGCal();
modified_eIds = IGNORE_RECENTLY_PUSHED ? modified_eIds : new Set();
for (var c_name of Object.keys(CALENDAR_IDS)) {
syncFromGCal(c_name, false, modified_eIds);
}
}
/**
* Syncs all calendars from google calendar to Notion using a full sync.
*
* -- Will discard the old page token and generate a new one. --
* -- Will reset time min and time max to use the the current time as origin time --
**/
function fullSync() {
parseNotionProperties();
console.log(
"Preforming full sync. Page token, time min, and time max will be reset."
);
for (var c_name of Object.keys(CALENDAR_IDS)) {
syncFromGCal(c_name, true, new Set());
}
}
/**
* Sync to google calendar from Notion
* @returns {Set[String]} - Array of event IDs that were modified through event creation
*/
function syncToGCal() {
console.log("[+GC] Syncing to Google Calendar.");
// Get 100 pages in order of when they were last edited.
const url = getDatabaseURL();
const payload = {
sorts: [{ timestamp: "last_edited_time", direction: "descending" }],
filter: {
property: TAGS_NOTION,
multi_select: {
does_not_contain: IGNORE_SYNC_TAG_NAME,
},
},
};
const response_data = notionFetch(url, payload, "POST");
let modified_eIds = new Set();
for (let i = 0; i < response_data.results.length; i++) {
let result = response_data.results[i];
if (!isPageUpdatedRecently(result)) continue;
let event = convertToGCalEvent(result);
if (!event) {
console.log(
"[+GC] Skipping page %s because it is not in the correct format and or is missing required information.",
result.id
);
continue;
}
let calendar_id = result.properties[CALENDAR_ID_NOTION].select;
calendar_id = calendar_id ? calendar_id.name : null;
let calendar_name = result.properties[CALENDAR_NAME_NOTION].select;
calendar_name = calendar_name ? calendar_name.name : null;
if (CALENDAR_IDS[calendar_name] && calendar_id && event.id) {
if (calendar_id === CALENDAR_IDS[calendar_name]) {
// Update event in original calendar.
console.log("[+GC] Updating event %s in %s.", event.id, calendar_name);
pushEventUpdate(event, event.id, calendar_id);
continue;
}
// Event being moved to a new calendar - delete from old calendar and then create using calendar name
let modified_eId;
if (
deleteEvent(event.id, calendar_id) &&
(modified_eId = createEvent(result, event, calendar_name))
) {
console.log("[+GC] Event %s moved to %s.", event.id, calendar_name);
modified_eIds.add(modified_eId);
continue;
}
console.log(
"[+GC] Event %s failed to move to %s.",
event.id,
calendar_name
);
continue;
}
if (CALENDAR_IDS[calendar_name]) {
// attempt to create using calendar name
let modified_eId;
if ((modified_eId = createEvent(result, event, calendar_name))) {
console.log("[+GC] Event created in %s.", calendar_name);
modified_eIds.add(modified_eId);
}
continue;
}
// Calendar name not found in dictonary. Abort.
console.log(
"[+GC] Calendar name %s not found in dictionary. Aborting sync.",
calendar_name
);
}
return modified_eIds;
}
/**
* Syncs from google calendar to Notion
* @param {String} c_name Calendar name
* @param {Boolean} fullSync Whenever or not to discard the old page token
* @param {Set[String]} ignored_eIds Event IDs to not act on.
*/
function syncFromGCal(c_name, fullSync, ignored_eIds) {
console.log("[+ND] Syncing from Google Calendar: %s", c_name);
let properties = PropertiesService.getUserProperties();
let options = {
maxResults: 100,
singleEvents: true, // allow recurring events
};
let syncToken = properties.getProperty("syncToken");
if (syncToken && !fullSync) {
options.syncToken = syncToken;
} else {
// Sync events up to thirty days in the past.
options.timeMin = getRelativeDate(-RELATIVE_MIN_DAY, 0).toISOString();
// Sync events up to x days in the future.
options.timeMax = getRelativeDate(RELATIVE_MAX_DAY, 0).toISOString();
}
// Retrieve events one page at a time.
let events;
let pageToken;
do {
try {
options.pageToken = pageToken;
events = Calendar.Events.list(CALENDAR_IDS[c_name], options);
} catch (e) {
// Check to see if the sync token was invalidated by the server;
// if so, perform a full sync instead.
if (
e.message === "Sync token is no longer valid, a full sync is required."
) {
properties.deleteProperty("syncToken");
syncFromGCal(CALENDAR_IDS[c_name], true, ignored_eIds);
return;
} else {
throw new Error(e.message);
}
}
events["c_name"] = c_name;
if (events.items && events.items.length === 0) {
console.log("[+ND] No events found. %s", c_name);
return;
}
console.log("[+ND] Parsing new events. %s", c_name);
parseEvents(events, ignored_eIds);
pageToken = events.nextPageToken;
} while (pageToken);
properties.setProperty("syncToken", events.nextSyncToken);
}
/**
* Determine if gcal events need to be updated, removed, or added to the database
* @param {CalendarEvent[]} events Google calendar events
* @param {Set[String]} ignored_eIds Event IDs to not act on.
*/
function parseEvents(events, ignored_eIds) {
let requests = [];
for (let i = 0; i < events.items.length; i++) {
let event = events.items[i];
event["c_name"] = events["c_name"];
if (ignored_eIds.has(event.id)) {
console.log("[+ND] Ignoring event %s", event.id);
continue;
}
if (event.status === "cancelled") {
console.log("[+ND] Event %s was cancelled.", event.id);
// Remove the event from the database
handleEventCancelled(event);
continue;
}
let start;
let end;
if (event.start.date) {
// All-day event.
start = new Date(event.start.date);
end = new Date(event.end.date);
console.log(
"[+ND] Event found %s %s (%s -- %s)",
event.id,
event.summary,
start.toLocaleDateString(),
end.toLocaleDateString()
);
} else {
// Events that don't last all day; they have defined start times.
start = event.start.dateTime;
end = event.end.dateTime;
console.log(
"[+ND] Event found %s %s (%s)",
event.id,
event.summary,
start.toLocaleString()
);
}
let page_response = getPageFromEvent(event);
if (page_response) {
console.log(
"[+ND] Event %s database page %s exists already. Attempting update.",
event.id,
page_response.id
);
let tags = page_response.properties[TAGS_NOTION].multi_select;
requests.push(
updateDatabaseEntry(event, page_response.id, tags ? tags : [])
);
continue;
}
console.log("[+ND] Creating database entry.");
try {
requests.push(createDatabaseEntry(event));
} catch (err) {
if ((err instanceof InvalidEventError) & SKIP_BAD_EVENTS) {
console.log(
"[+ND] Skipping creation of event %s due to invalid properties.",
event.id
);
continue;
}
throw err;
}
}
console.log("[+ND] Finished parsing page. Sending batch request.");
const responses = UrlFetchApp.fetchAll(requests);
for (let i = 0; i < responses.length; i++) {
let response = responses[i];
if (response.getResponseCode() === 401) {
throw new Error("[+ND] Notion token is invalid.");
} else if (response.getResponseCode() === 404) {
throw new Error("[+ND] Notion page not found.");
} else if (response.getResponseCode() === 403) {
throw new Error("[+ND] Notion page is private.");
} else if (response.getResponseCode() !== 200) {
throw new Error(response.getContentText());
}
}
}
/**
* Update database entry with new event information
* @param {CalendarEvent} event Modified Google calendar event
* @param {String} page_id Page ID of database entry
* @param {String[]} existing_tags Existing tags of the page to keep.
* @param {Boolean} multi Whenever or not the update is meant for a multi-fetch
* @returns {*} request object if multi is true, fetch response if multi is false
*/
function updateDatabaseEntry(event, page_id, existing_tags = [], multi = true) {
let properties = convertToNotionProperty(event, existing_tags);
let archive = ARCHIVE_CANCELLED_EVENTS && event.status === "cancelled";
return pushDatabaseUpdate(properties, page_id, archive, multi);
}
/**
* Push update to notion database for page
* @param {Object} properties
* @param {String} page_id page id to update
* @param {Boolean} archive whenever or not to archive the page
* @param {Boolean} multi whenever or not to use single fetch, or return options for fetchAll
* @returns {*} request object if multi, otherwise URL fetch response
*/
function pushDatabaseUpdate(
properties,
page_id,
archive = false,
multi = false
) {
const url = "https://api.notion.com/v1/pages/" + page_id;
let payload = {};
payload["properties"] = properties;
payload["archived"] = archive;
if (archive) {
console.log("Archiving cancelled event.");
}
let options = {
method: "PATCH",
headers: getNotionHeaders(),
muteHttpExceptions: true,
payload: JSON.stringify(payload),
};
if (multi) {
options["url"] = url;
return options;
}
return UrlFetchApp.fetch(url, options);
}
/**
* Create a new database entry for the event
* @param {CalendarEvent} event modified GCal event object
* @returns {*} request object
*/
function createDatabaseEntry(event) {
const url = "https://api.notion.com/v1/pages";
let payload = {};
payload["parent"] = {
type: "database_id",
database_id: DATABASE_ID,
};
payload["properties"] = convertToNotionProperty(event);
if (!checkNotionProperty(payload["properties"])) {
throw new InvalidEventError("Invalid Notion property structure");
}
let options = {
url: url,
method: "POST",
headers: getNotionHeaders(),
muteHttpExceptions: true,
payload: JSON.stringify(payload),
};
return options;
}
/**
* Checks if the properties are valid for Notion
*
* @param {*} properties Properties object to check
* @returns false if invalid, true if valid
*/
function checkNotionProperty(properties) {
// Check if description is too long
if (properties[DESCRIPTION_NOTION].rich_text[0].text.content.length > 2000) {
console.log("Event description is too long.");
return false;
}
return true;
}
/**
* Determine if a page exists for the event, and the page needs to be updated. Returns page response if found.
* @param {CalendarEvent} event
* @returns {*} Page response if found.
*/
function getPageFromEvent(event) {
const url = getDatabaseURL();
const payload = {
filter: {
and: [
{ property: EVENT_ID_NOTION, rich_text: { equals: event.id } },
{
property: LAST_SYNC_NOTION,
date: { before: new Date().toISOString(event.updated) },
},
],
},
};
const response_data = notionFetch(url, payload, "POST");
if (response_data.results.length > 0) {
if (response_data.results.length > 1) {
console.log(
"Found multiple entries with event id %s. This should not happen. Only considering index zero entry.",
event.id
);
}
return response_data.results[0];
}
return false;
}
/**
* Retrieve notion page using page id
* @deprecated This is not used anymore due to Notion API change on Aug 31, 2022, but kept for reference.
* @param {Object} result
* @param {String} property - notion property name key
* @returns {Object} request response object
*/
function getPageProperty(result, property) {
console.log("Warning. Using deprecated function getPageProperty.");
let page_id = result.id;
try {
let property_id = result.properties[property].id;
const url =
"https://api.notion.com/v1/pages/" +
page_id +
"/properties/" +
property_id;
return notionFetch(url, null, "GET");
} catch (e) {
throw new Error(
`Error trying to get page property ${property} from page ${page_id}. Ensure that the database is setup correctly! EM: ${e.message}`
);
}
}
/**
* Interact with notion API
* @param {String} url - url to send request to
* @param {Object} payload_dict - payload to send with request
* @param {String} method - method to use for request
* @returns {Object} request response object
*/
function notionFetch(url, payload_dict, method = "POST") {
// UrlFetchApp is sync even if async is specified
let options = {
method: method,
headers: getNotionHeaders(),
muteHttpExceptions: true,
...(payload_dict && { payload: JSON.stringify(payload_dict) }),
};
const response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 200) {
const response_data = JSON.parse(response.getContentText());
if (response_data.length == 0) {
throw new Error(
"No data returned from Notion API. Check your Notion token."
);
}
return response_data;
} else if (response.getResponseCode() === 401) {
throw new Error("Notion token is invalid.");
} else {
throw new Error(response.getContentText());
}
}
function getNotionHeaders() {
return {
Authorization: `Bearer ${NOTION_TOKEN}`,
Accept: "application/json",
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
}
function getDatabaseURL() {
return `https://api.notion.com/v1/databases/${DATABASE_ID}/query`;
}
function getNotionParent() {
return {
database_id: DATABASE_ID,
};
}
function getRelativeDate(daysOffset, hour) {
let date = new Date();
date.setDate(date.getDate() + daysOffset);
date.setHours(hour);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
}
/**
* Return notion JSON property object based on event data
* @param {CalendarEvent} event modified GCal event object
* @param {String[]} existing_tags - existing tags to add to event
* @returns {Object} notion property object
*/
function convertToNotionProperty(event, existing_tags = []) {
let properties = getBaseNotionProperties(event.id, event.c_name);
properties[DESCRIPTION_NOTION] = {
type: "rich_text",
rich_text: [
{
text: {
content: event.description ? event.description : "",
},
},
],
};
properties[LOCATION_NOTION] = {
type: "rich_text",
rich_text: [
{
text: {
content: event.location ? event.location : "",
},
},
],
};
if (event.conferenceData)
{
entryPoints = event.conferenceData.entryPoints;
if (entryPoints)
{
properties[LINK_NOTION] = {
type: "url",
url: entryPoints[0].uri ? entryPoints[0].uri : ""
};
}
}
if (event.start) {
let start_time;
let end_time;
if (event.start.date) {
// All-day event.
start_time = new Date(event.start.date);
end_time = new Date(event.end.date);
// Offset timezone
start_time.setTime(
start_time.getTime() + start_time.getTimezoneOffset() * 60 * 1000
);
end_time.setTime(
end_time.getTime() + end_time.getTimezoneOffset() * 60 * 1000
);
// Offset by 1 day to get end date.
end_time.setDate(end_time.getDate() - 1);
start_time = start_time.toISOString().split("T")[0];
end_time = end_time.toISOString().split("T")[0];
end_time = start_time == end_time ? null : end_time;
} else {
// Events that don't last all day; they have defined start times.
start_time = event.start.dateTime;
end_time = event.end.dateTime;
}
properties[DATE_NOTION] = {
type: "date",
date: {
start: start_time,
end: end_time,
},
};
properties[NAME_NOTION] = {
type: "title",
title: [
{
type: "text",
text: {
content: event.summary ? event.summary : "",
},
},
],
};
}
if (event.status === "cancelled") {
properties[TAGS_NOTION] = { multi_select: existing_tags };
properties[TAGS_NOTION].multi_select.push({
name: CANCELLED_TAG_NAME,
});
}
return properties;
}
/**
* Return base notion JSON property object including generation time
* @param {String} event_id - event id
* @param {String} calendar_name - calendar key name
* @returns {Object} - base notion property object
* */
function getBaseNotionProperties(event_id, calendar_name) {
return {
[LAST_SYNC_NOTION]: {
type: "date",
date: {
start: new Date().toISOString(),
},
},
[EVENT_ID_NOTION]: {
type: "rich_text",
rich_text: [
{
text: {
content: event_id, //use ICal uid?
},
},
],
},
[CALENDAR_ID_NOTION]: {
select: {
name: CALENDAR_IDS[calendar_name],
},
},
[CALENDAR_NAME_NOTION]: {
select: {
name: calendar_name,
},
},
};
}
/**
* Return GCal event object based on page properties
* @param {Object} page_result - Notion page result object
* @returns {Object} - GCal event object Return False if required properties not found
*/
function convertToGCalEvent(page_result) {
let e_id = page_result.properties[EVENT_ID_NOTION].rich_text;
e_id = flattenRichText(e_id);
let e_summary = page_result.properties[NAME_NOTION].title;
e_summary = flattenRichText(e_summary);
let e_description = page_result.properties[DESCRIPTION_NOTION].rich_text;
e_description = flattenRichText(e_description);
let e_location = page_result.properties[LOCATION_NOTION].rich_text;
e_location = flattenRichText(e_location);
let dates = page_result.properties[DATE_NOTION];
if (dates.date) {
let all_day = dates.date.end === null;
if (dates.date.start && dates.date.start.search(/([A-Z])/g) === -1) {
dates.date.start += "T00:00:00";
all_day = true;
} else if (
!dates.date.end &&
dates.date.start &&
dates.date.start.search(/([A-Z])/g) !== -1
) {
all_day = false;
let default_end = new Date(dates.date.start);
default_end.setMinutes(default_end.getMinutes() + 30);
dates.date.end = default_end.toISOString();
} else if (dates.date.end && dates.date.end.search(/([A-Z])/g) === -1) {
dates.date.end += "T00:00:00";
all_day = true;
}
let event = {
...(e_id && { id: e_id }),
...(e_summary && { summary: e_summary }),
...(e_description && { description: e_description }),
...(e_location && { location: e_location }),
...(dates.date.start && { start: dates.date.start }),
...(dates.date.end && { end: dates.date.end }),
all_day: all_day,
};
return event;
} else {
return false;
}
}
/**
* Parses Notion information from project properties and declares them into global variables
*/
function parseNotionProperties() {
let properties = PropertiesService.getScriptProperties();
NOTION_TOKEN = properties.getProperty("NOTION_TOKEN");
let reURLInformation =
/^(([^@:\/\s]+):\/?)?\/?(([^@:\/\s]+)(:([^@:\/\s]+))?@)?([^@:\/\s]+)(:(\d+))?(((\/\w+)*\/)([\w\-\.]+[^#?\s]*)?(.*)?(#[\w\-]+)?)?$/;
let database_url = properties
.getProperty("DATABASE_ID")
.match(reURLInformation);
DATABASE_ID = database_url[13];
}
/**
* Get notion page ID of corresponding gCal event. Returns null if no page found.
* @param {CalendarEvent} event - Modiffied gCal event object
*/
function getPageId(event) {
const url = getDatabaseURL();
const payload = {
filter: {
and: [
{ property: EVENT_ID_NOTION, rich_text: { equals: event.id } },
{
property: TAGS_NOTION,
multi_select: {
does_not_contain: IGNORE_SYNC_TAG_NAME,
},
},
],
},
};
const response_data = notionFetch(url, payload, "POST");
if (response_data.results.length > 0) {
if (response_data.results.length > 1) {
console.log(
"Found multiple entries with event id %s. This should not happen. Only processing index zero entry.",
event.id
);
}
return response_data.results[0].id;
}
return null;
}
/**
* Deals with event cancelled from gCal side
* @param {CalendarEvent} event - Modiffied gCal event object
*/
function handleEventCancelled(event) {
const page_id = getPageId(event);
if (page_id) {
updateDatabaseEntry(event, page_id, [], false);
} else {
console.log("Event %s not found in Notion database. Skipping.", event.id);
}
}
/** Delete events marked as cancelled in gcal */
function deleteCancelledEvents() {
console.log("[-GCal] Deleting cancel tagged events from GCal");
const url = getDatabaseURL();
const payload = {
filter: {
property: TAGS_NOTION,
multi_select: {
contains: CANCELLED_TAG_NAME,
does_not_contain: IGNORE_SYNC_TAG_NAME,
},
},
};
const response_data = notionFetch(url, payload, "POST");
for (let i = 0; i < response_data.results.length; i++) {
let result = response_data.results[i];
if (isPageUpdatedRecently(result)) {
try {
let event_id = result.properties[EVENT_ID_NOTION].rich_text;
let calendar_id = result.properties[CALENDAR_ID_NOTION].select.name;
event_id = flattenRichText(event_id);
deleteEvent(event_id, calendar_id);
} catch (e) {
if (e instanceof TypeError) {
console.log("[-GCal] Error. Page missing calendar id or event ID");
} else throw e;
} finally {
ARCHIVE_CANCELLED_EVENTS
? pushDatabaseUpdate([], result.id, true)
: null;
}
}
}
}
/** Delete event from Google calendar
* @param {String} event_id - Event id to delete
* @param {String} calendar_id - Calendar id to delete event from
* @returns {Boolean} - True if event was deleted, false if not
*/
function deleteEvent(event_id, calendar_id) {
console.log("Deleting event %s from gCal %s", event_id, calendar_id);
try {
let calendar = CalendarApp.getCalendarById(calendar_id);
calendar.getEventById(event_id).deleteEvent();
return true;
} catch (e) {
console.log(e);
return false;
}
}
/** Determine if a page result has been updated recently
* @param {Object} page_result - Page result from Notion database
* @return {Boolean} - True if page has been updated recently, false otherwise
* */
function isPageUpdatedRecently(page_result) {
let last_sync_date = page_result.properties[LAST_SYNC_NOTION];
last_sync_date = last_sync_date.date ? last_sync_date.date.start : 0;
return new Date(last_sync_date) < new Date(page_result.last_edited_time);
}
/**
* Flattens rich text properties into a singular string.
* @param {Object} rich_text_result - Rich text property to flatten
* @return {String} - Flattened rich text
* */
function flattenRichText(rich_text_result) {
let plain_text = "";
for (let i = 0; i < rich_text_result.length; i++) {
plain_text += rich_text_result[i].rich_text
? rich_text_result[i].rich_text.plain_text
: rich_text_result[i].plain_text;
}
return plain_text;
}
/** Create event to Google calendar. Return event ID if successful
* @param {Object} page - Page object from Notion database
* @param {Object} event - Event object for gCal
* @param {String} calendar_name - name of calendar to push event to
* @return {String} - Event ID if successful, false otherwise
*/
function createEvent(page, event, calendar_name) {
event.summary = event.summary || "";
event.description = event.description || "";
event.location = event.location || "";
let calendar_id = CALENDAR_IDS[calendar_name];
let options = [event.summary, new Date(event.start)];
if (event.end && event.all_day) {
// add and shift
var shifted_date = new Date(event.end);
shifted_date.setDate(shifted_date.getDate() + 1);
options.push(shifted_date);
} else if (event.end) {
options.push(new Date(event.end));
}
options.push({ description: event.description, location: event.location });
let calendar = CalendarApp.getCalendarById(calendar_id);
try {
let new_event = event.all_day
? calendar.createAllDayEvent(...options)
: calendar.createEvent(...options);
new_event_id = new_event.getId().split("@")[0];
} catch (e) {
console.log("Failed to push new event to GCal. %s", e);
return false;
}
if (!new_event_id) {
console.log("Event %s not created in gCal.", event.summary);
return false;
}
let properties = getBaseNotionProperties(new_event_id, calendar_name);
pushDatabaseUpdate(properties, page.id);
return new_event_id;
}
/** Update Google calendar event
* @param {CalendarEvent} event - Modified event object for gCal
* @param {String} page_id - Page ID of Notion page to update
* @param {String} calendar_id - Calendar ID of calendar to update event from
* @return {Boolean} True if successful, false otherwise
*/
function pushEventUpdate(event, event_id, calendar_id) {
event.summary = event.summary || "";
event.description = event.description || "";
event.location = event.location || "";
try {
let calendar = CalendarApp.getCalendarById(calendar_id);
let cal_event = calendar.getEventById(event_id);
cal_event.setDescription(event.description);
cal_event.setTitle(event.summary);
cal_event.setLocation(event.location);
if (event.end && event.all_day) {
// all day, multi day
var shifted_date = new Date(event.end);
shifted_date.setDate(shifted_date.getDate() + 2);
cal_event.setAllDayDates(new Date(event.start), shifted_date);
} else if (event.all_day) {
// all day, single day
cal_event.setAllDayDate(new Date(event.start));
} else {
// not all day
cal_event.setTime(new Date(event.start), new Date(event.end) || null);
}
return true;
} catch (e) {
console.log("Failed to push event update to GCal. %s", e);
return false;
}
}
/**
* Error thrown when an event is invalid and cannot be
* pushed to either Google Calendar or Notion.
*/
class InvalidEventError extends Error {
constructor(message) {
super(message);
this.name = "InvalidEventError";
}
}