forked from callumalpass/tasknotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskCalendarSyncService.ts
More file actions
1084 lines (942 loc) · 35.1 KB
/
Copy pathTaskCalendarSyncService.ts
File metadata and controls
1084 lines (942 loc) · 35.1 KB
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Notice, TFile } from "obsidian";
import { format } from "date-fns";
import TaskNotesPlugin from "../main";
import { GoogleCalendarService } from "./GoogleCalendarService";
import { TaskInfo } from "../types";
import { convertToGoogleRecurrence } from "../utils/rruleConverter";
import { TokenRefreshError } from "./errors";
/** Debounce delay for rapid task updates (ms) */
const SYNC_DEBOUNCE_MS = 500;
/** Max concurrent API calls during bulk sync to avoid rate limits */
const SYNC_CONCURRENCY_LIMIT = 5;
/** Minimum spacing between Google Calendar API calls (ms) to reduce 403 rate limits.
* Google Calendar enforces ~10 req/s per-user; 100ms keeps us comfortably under that. */
const GOOGLE_API_CALL_SPACING_MS = 100;
/**
* Service for syncing TaskNotes tasks to Google Calendar.
* Handles creating, updating, and deleting calendar events when tasks change.
*/
export class TaskCalendarSyncService {
private plugin: TaskNotesPlugin;
private googleCalendarService: GoogleCalendarService;
private rateLimitChain: Promise<unknown> = Promise.resolve();
private lastApiCallAt = 0;
/** Debounce timers for pending syncs, keyed by task path */
private pendingSyncs: Map<string, ReturnType<typeof setTimeout>> = new Map();
/** In-flight sync operations to prevent concurrent syncs for the same task */
private inFlightSyncs: Map<string, Promise<void>> = new Map();
/** Track previous task state for detecting recurrence removal */
private previousTaskState: Map<string, TaskInfo> = new Map();
/** Store the latest explicitly passed task object during debounce to avoid cache race conditions */
private pendingTasks: Map<string, TaskInfo> = new Map();
constructor(plugin: TaskNotesPlugin, googleCalendarService: GoogleCalendarService) {
this.plugin = plugin;
this.googleCalendarService = googleCalendarService;
}
/**
* Clean up pending timers (call on plugin unload)
*/
destroy(): void {
for (const timer of this.pendingSyncs.values()) {
clearTimeout(timer);
}
this.pendingSyncs.clear();
this.previousTaskState.clear();
this.pendingTasks.clear();
}
/**
* Process items in parallel with a concurrency limit.
* Executes up to SYNC_CONCURRENCY_LIMIT operations simultaneously.
*/
private async processInParallel<T>(
items: T[],
processor: (item: T) => Promise<void>
): Promise<void> {
const executing: Promise<void>[] = [];
for (const item of items) {
const promise = processor(item).then(() => {
executing.splice(executing.indexOf(promise), 1);
});
executing.push(promise);
if (executing.length >= SYNC_CONCURRENCY_LIMIT) {
await Promise.race(executing);
}
}
await Promise.all(executing);
}
/**
* Ensure Google API calls are spaced out to avoid 403 rate limits.
* Calls are queued and executed with at least GOOGLE_API_CALL_SPACING_MS between them.
*/
private withGoogleRateLimit<T>(fn: () => Promise<T>): Promise<T> {
return new Promise<T>((resolve, reject) => {
this.rateLimitChain = this.rateLimitChain.then(async () => {
const now = Date.now();
const wait = Math.max(0, GOOGLE_API_CALL_SPACING_MS - (now - this.lastApiCallAt));
if (wait > 0) {
await new Promise((r) => setTimeout(r, wait));
}
try {
const result = await fn();
this.lastApiCallAt = Date.now();
resolve(result);
} catch (e) {
this.lastApiCallAt = Date.now();
reject(e);
}
}, () => {
// Previous call in chain failed; continue the chain
fn().then(resolve, reject);
});
});
}
/**
* Check if the sync service is enabled and properly configured
*/
isEnabled(): boolean {
const settings = this.plugin.settings.googleCalendarExport;
const enabled = settings.enabled;
const hasTargetCalendar = !!settings.targetCalendarId;
// Check if Google Calendar is connected by verifying calendars are available
// (populated during GoogleCalendarService.initialize() when OAuth is connected)
const isConnected = this.googleCalendarService.getAvailableCalendars().length > 0;
return enabled && hasTargetCalendar && isConnected;
}
/**
* Determine if a task should be synced based on settings and task properties
*/
shouldSyncTask(task: TaskInfo): boolean {
if (!this.isEnabled()) return false;
const settings = this.plugin.settings.googleCalendarExport;
// Don't sync archived tasks
if (task.archived) return false;
// Check if task has the required date(s) based on sync trigger setting
switch (settings.syncTrigger) {
case "scheduled":
return !!task.scheduled;
case "due":
return !!task.due;
case "both":
return !!task.scheduled || !!task.due;
default:
return false;
}
}
/**
* Get the Google Calendar event ID from the task's frontmatter
*/
getTaskEventId(task: TaskInfo): string | undefined {
return task.googleCalendarEventId;
}
/**
* Determines if a task should be synced as a Google Calendar recurring event.
* Only scheduled-based recurring tasks are synced as recurring events.
* Completion-based recurring tasks remain as single events (since their
* DTSTART shifts on each completion, which doesn't map well to Google Calendar).
*/
private shouldSyncAsRecurring(task: TaskInfo): boolean {
// Must have a recurrence rule
if (!task.recurrence) return false;
// Only scheduled-based recurrence syncs as recurring events
// Completion-based recurrence stays as single events (existing behavior)
const anchor = task.recurrence_anchor || "scheduled";
return anchor === "scheduled";
}
/**
* Save the Google Calendar event ID to the task's frontmatter
*/
private async saveTaskEventId(taskPath: string, eventId: string): Promise<void> {
const file = this.plugin.app.vault.getAbstractFileByPath(taskPath);
if (!(file instanceof TFile)) {
console.warn(`Cannot save event ID: file not found at ${taskPath}`);
return;
}
const fieldName = this.plugin.fieldMapper.toUserField("googleCalendarEventId");
await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => {
frontmatter[fieldName] = eventId;
});
}
/**
* Remove the Google Calendar event ID from the task's frontmatter
*/
private async removeTaskEventId(taskPath: string): Promise<void> {
const file = this.plugin.app.vault.getAbstractFileByPath(taskPath);
if (!(file instanceof TFile)) {
console.warn(`Cannot remove event ID: file not found at ${taskPath}`);
return;
}
const fieldName = this.plugin.fieldMapper.toUserField("googleCalendarEventId");
await this.plugin.app.fileManager.processFrontMatter(file, (frontmatter) => {
delete frontmatter[fieldName];
});
}
/**
* Apply the title template to generate the event title
*/
private applyTitleTemplate(task: TaskInfo): string {
const template = this.plugin.settings.googleCalendarExport.eventTitleTemplate;
// Get human-readable labels for status and priority
const statusConfig = task.status
? this.plugin.statusManager.getStatusConfig(task.status)
: null;
const priorityConfig = task.priority
? this.plugin.priorityManager.getPriorityConfig(task.priority)
: null;
const untitledTask = this.plugin.i18n.translate("settings.integrations.googleCalendarExport.eventDescription.untitledTask");
return template
.replace(/\{\{title\}\}/g, task.title || untitledTask)
.replace(/\{\{status\}\}/g, statusConfig?.label || task.status || "")
.replace(/\{\{priority\}\}/g, priorityConfig?.label || task.priority || "")
.replace(/\{\{due\}\}/g, task.due || "")
.replace(/\{\{scheduled\}\}/g, task.scheduled || "")
.trim();
}
/**
* Build the event description from task properties
*/
private buildEventDescription(task: TaskInfo): string {
const settings = this.plugin.settings.googleCalendarExport;
const t = (key: string, params?: Record<string, string | number>) =>
this.plugin.i18n.translate(`settings.integrations.googleCalendarExport.eventDescription.${key}` as any, params);
const parts: string[] = [];
// Add task metadata
if (task.priority && task.priority !== "none") {
const priorityConfig = this.plugin.priorityManager.getPriorityConfig(task.priority);
parts.push(t("priority", { value: priorityConfig?.label || task.priority }));
}
if (task.status) {
const statusConfig = this.plugin.statusManager.getStatusConfig(task.status);
parts.push(t("status", { value: statusConfig?.label || task.status }));
}
// Add dates
if (task.due) {
parts.push(t("due", { value: task.due }));
}
if (task.scheduled) {
parts.push(t("scheduled", { value: task.scheduled }));
}
// Add time estimate
if (task.timeEstimate) {
const hours = Math.floor(task.timeEstimate / 60);
const minutes = task.timeEstimate % 60;
const estimateStr = hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
parts.push(t("timeEstimate", { value: estimateStr }));
}
// Add tags
if (task.tags && task.tags.length > 0) {
parts.push(t("tags", { value: task.tags.map((tag) => `#${tag}`).join(", ") }));
}
// Add contexts
if (task.contexts && task.contexts.length > 0) {
parts.push(t("contexts", { value: task.contexts.map((c) => `@${c}`).join(", ") }));
}
// Add projects
if (task.projects && task.projects.length > 0) {
parts.push(t("projects", { value: task.projects.join(", ") }));
}
// Add separator before link
if (parts.length > 0 && settings.includeObsidianLink) {
parts.push("");
parts.push("---");
}
// Add Obsidian link (as HTML anchor for clickability in Google Calendar)
if (settings.includeObsidianLink) {
const vaultName = this.plugin.app.vault.getName();
const encodedPath = encodeURIComponent(task.path);
const obsidianUri = `obsidian://open?vault=${encodeURIComponent(vaultName)}&file=${encodedPath}`;
// Google Calendar renders HTML in descriptions, so use an anchor tag
const linkText = t("openInObsidian");
parts.push(`<a href="${obsidianUri}">${linkText}</a>`);
}
return parts.join("\n");
}
/**
* Get the date to use for the calendar event based on settings
*/
private getEventDate(task: TaskInfo): string | undefined {
const settings = this.plugin.settings.googleCalendarExport;
switch (settings.syncTrigger) {
case "scheduled":
return task.scheduled;
case "due":
return task.due;
case "both":
// Prefer scheduled, fall back to due
return task.scheduled || task.due;
default:
return undefined;
}
}
/**
* Parse a task date string and determine if it's all-day or timed
*/
private parseDateForEvent(dateStr: string): {
date?: string;
dateTime?: string;
timeZone?: string;
isAllDay: boolean;
} {
// Check if the date includes a time component (has 'T')
if (dateStr.includes("T")) {
// Timed event - format with local timezone offset for Google Calendar.
// Using date-fns format with 'xxx' to produce an offset-aware RFC 3339 string
// (e.g. "2024-03-15T14:30:00+05:00") so the dateTime matches the timeZone.
const date = new Date(dateStr);
return {
dateTime: format(date, "yyyy-MM-dd'T'HH:mm:ssxxx"),
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
isAllDay: false,
};
} else {
// All-day event - just use the date string
return {
date: dateStr,
isAllDay: true,
};
}
}
/**
* Calculate the end date/time for an event
*/
private getEventEnd(
startInfo: { date?: string; dateTime?: string; timeZone?: string; isAllDay: boolean },
task: TaskInfo
): { date?: string; dateTime?: string; timeZone?: string } {
const settings = this.plugin.settings.googleCalendarExport;
if (startInfo.isAllDay || settings.createAsAllDay) {
// All-day events: end is the same date (or next day for multi-day)
// Google Calendar requires end date to be the day AFTER for all-day events
if (startInfo.date) {
const startDate = new Date(startInfo.date + "T00:00:00");
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
return { date: format(endDate, "yyyy-MM-dd") };
}
// Fallback for dateTime that should be all-day
const startDate = new Date(startInfo.dateTime!);
const endDate = new Date(startDate);
endDate.setDate(endDate.getDate() + 1);
return { date: format(endDate, "yyyy-MM-dd") };
} else {
// Timed events: use duration
const duration = task.timeEstimate || settings.defaultEventDuration;
const startDate = new Date(startInfo.dateTime!);
const endDate = new Date(startDate.getTime() + duration * 60 * 1000);
return {
dateTime: format(endDate, "yyyy-MM-dd'T'HH:mm:ssxxx"),
timeZone: startInfo.timeZone,
};
}
}
/**
* Parse ISO 8601 duration format and return milliseconds.
* Based on the parser from NotificationService.
*/
private parseISO8601Duration(duration: string): number | null {
// Parse ISO 8601 duration format (e.g., "-PT15M", "P2D", "-PT1H30M")
const match = duration.match(
/^(-?)P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/
);
if (!match) {
return null;
}
const [, sign, years, months, weeks, days, hours, minutes, seconds] = match;
let totalMs = 0;
// Note: For simplicity, we treat months as 30 days and years as 365 days
if (years) totalMs += parseInt(years) * 365 * 24 * 60 * 60 * 1000;
if (months) totalMs += parseInt(months) * 30 * 24 * 60 * 60 * 1000;
if (weeks) totalMs += parseInt(weeks) * 7 * 24 * 60 * 60 * 1000;
if (days) totalMs += parseInt(days) * 24 * 60 * 60 * 1000;
if (hours) totalMs += parseInt(hours) * 60 * 60 * 1000;
if (minutes) totalMs += parseInt(minutes) * 60 * 1000;
if (seconds) totalMs += parseInt(seconds) * 1000;
// Apply sign for negative durations (before the anchor date)
return sign === "-" ? -totalMs : totalMs;
}
/**
* Convert task reminders to Google Calendar reminder format.
* Returns an array of reminder overrides in the format Google Calendar API expects.
*
* @param task - The task with reminders
* @param eventStartTime - The event start time (ISO string or date string)
* @param eventDateSource - Which date field was used for the event ('due' or 'scheduled')
* @returns Array of { method: string; minutes: number } or null if no valid reminders
*/
private convertTaskRemindersToGoogleFormat(
task: TaskInfo,
eventStartTime: string,
eventDateSource: 'due' | 'scheduled'
): Array<{ method: string; minutes: number }> | null {
if (!task.reminders || !Array.isArray(task.reminders) || task.reminders.length === 0) {
return null;
}
const googleReminders: Array<{ method: string; minutes: number }> = [];
const GOOGLE_MAX_REMINDER_MINUTES = 40320; // 4 weeks in minutes (Google Calendar API limit)
// Parse event start time to get a timestamp
let eventStartMs: number;
try {
// Handle both ISO timestamps and date-only strings
if (eventStartTime.includes('T')) {
eventStartMs = new Date(eventStartTime).getTime();
} else {
// Date-only string - assume start of day in local timezone
eventStartMs = new Date(eventStartTime + 'T00:00:00').getTime();
}
if (isNaN(eventStartMs)) {
console.warn('[TaskCalendarSync] Invalid event start time:', eventStartTime);
return null;
}
} catch (error) {
console.warn('[TaskCalendarSync] Error parsing event start time:', error);
return null;
}
for (const reminder of task.reminders) {
if (!reminder.type) continue;
if (reminder.type === 'relative') {
// Only include relative reminders that match the event's date source
if (reminder.relatedTo !== eventDateSource) {
continue;
}
// Parse the ISO 8601 duration
if (!reminder.offset) continue;
const durationMs = this.parseISO8601Duration(reminder.offset);
if (durationMs === null) {
console.warn('[TaskCalendarSync] Invalid duration format:', reminder.offset);
continue;
}
// Convert to minutes before the event
// Negative duration means "before", which is what we want
// Zero duration means "at event time"
// Positive duration means "after", which Google Calendar doesn't support for reminders
const minutesBefore = Math.abs(Math.round(durationMs / (60 * 1000)));
// Skip if reminder is after the event (positive duration without negative sign)
if (durationMs > 0) {
console.warn('[TaskCalendarSync] Skipping reminder after event:', reminder);
continue;
}
// Cap at Google Calendar's limit
const cappedMinutes = Math.min(minutesBefore, GOOGLE_MAX_REMINDER_MINUTES);
// Include 0-minute reminders (at event time)
if (cappedMinutes >= 0) {
googleReminders.push({ method: 'popup', minutes: cappedMinutes });
}
} else if (reminder.type === 'absolute') {
// Calculate minutes before event start
if (!reminder.absoluteTime) continue;
try {
const reminderTimeMs = new Date(reminder.absoluteTime).getTime();
if (isNaN(reminderTimeMs)) {
console.warn('[TaskCalendarSync] Invalid absolute time:', reminder.absoluteTime);
continue;
}
// Calculate difference in minutes
const diffMs = eventStartMs - reminderTimeMs;
const minutesBefore = Math.round(diffMs / (60 * 1000));
// Skip if reminder is after the event start
if (minutesBefore < 0) {
console.warn('[TaskCalendarSync] Skipping absolute reminder after event:', reminder);
continue;
}
// Cap at Google Calendar's limit
const cappedMinutes = Math.min(minutesBefore, GOOGLE_MAX_REMINDER_MINUTES);
// Include 0-minute reminders (at event time)
googleReminders.push({ method: 'popup', minutes: cappedMinutes });
} catch (error) {
console.warn('[TaskCalendarSync] Error parsing absolute reminder time:', error);
continue;
}
}
}
return googleReminders.length > 0 ? googleReminders : null;
}
/**
* Convert a task to a Google Calendar event payload
*/
private taskToCalendarEvent(task: TaskInfo, clearRecurrence?: boolean): {
summary: string;
description?: string;
start: { date?: string; dateTime?: string; timeZone?: string };
end: { date?: string; dateTime?: string; timeZone?: string };
colorId?: string;
reminders?: {
useDefault: boolean;
overrides?: Array<{ method: string; minutes: number }>;
};
recurrence?: string[];
} | null {
const eventDate = this.getEventDate(task);
if (!eventDate) return null;
const settings = this.plugin.settings.googleCalendarExport;
const startInfo = this.parseDateForEvent(eventDate);
// If user prefers all-day events, convert timed to all-day
let start: { date?: string; dateTime?: string; timeZone?: string };
if (settings.createAsAllDay && !startInfo.isAllDay) {
// Convert to all-day - use local date to handle timezone correctly
// e.g., "2024-01-15T23:00:00" in UTC+5 should become "2024-01-16" not "2024-01-15"
const localDate = new Date(eventDate);
const dateOnly = format(localDate, "yyyy-MM-dd");
start = { date: dateOnly };
} else if (startInfo.isAllDay) {
start = { date: startInfo.date };
} else {
start = { dateTime: startInfo.dateTime, timeZone: startInfo.timeZone };
}
// Calculate end based on start and duration
const adjustedStartInfo = {
...startInfo,
isAllDay: settings.createAsAllDay || startInfo.isAllDay,
date: start.date,
dateTime: start.dateTime,
};
const end = this.getEventEnd(adjustedStartInfo, task);
const event: {
summary: string;
description?: string;
start: { date?: string; dateTime?: string; timeZone?: string };
end: { date?: string; dateTime?: string; timeZone?: string };
colorId?: string;
reminders?: {
useDefault: boolean;
overrides?: Array<{ method: string; minutes: number }>;
};
recurrence?: string[];
} = {
summary: this.applyTitleTemplate(task),
start,
end,
};
if (settings.includeDescription) {
event.description = this.buildEventDescription(task);
}
if (settings.eventColorId) {
event.colorId = settings.eventColorId;
}
// Determine which date field was used for the event (for reminder conversion)
let eventDateSource: 'due' | 'scheduled';
if (settings.syncTrigger === 'scheduled' || (settings.syncTrigger === 'both' && task.scheduled)) {
eventDateSource = 'scheduled';
} else {
eventDateSource = 'due';
}
// Add reminders - prioritize task reminders, fall back to default
const taskReminders = this.convertTaskRemindersToGoogleFormat(
task,
eventDate,
eventDateSource
);
if (taskReminders && taskReminders.length > 0) {
// Use task-specific reminders
event.reminders = {
useDefault: false,
overrides: taskReminders,
};
} else if (settings.defaultReminderMinutes !== null && settings.defaultReminderMinutes > 0) {
// For all-day events, use Google Calendar's default all-day notifications
// (configured by the user in their Google Calendar settings) rather than
// overriding with minutes-based reminders which would fire at the wrong time
// (e.g., 11:30 PM the night before instead of 9 AM day-of). See #1465.
const isAllDay = settings.createAsAllDay || startInfo.isAllDay;
if (isAllDay) {
event.reminders = { useDefault: true };
} else {
event.reminders = {
useDefault: false,
overrides: [{ method: "popup", minutes: settings.defaultReminderMinutes }],
};
}
}
// Add recurrence rules for scheduled-based recurring tasks
if (this.shouldSyncAsRecurring(task) && task.recurrence) {
const recurrenceData = convertToGoogleRecurrence(task.recurrence, {
completedInstances: task.complete_instances,
skippedInstances: task.skipped_instances,
});
if (recurrenceData) {
event.recurrence = recurrenceData.recurrence;
// Override start date with DTSTART from recurrence rule
// This ensures the recurring event starts from the correct date
if (recurrenceData.dtstart) {
if (settings.createAsAllDay || !recurrenceData.hasTime) {
event.start = { date: recurrenceData.dtstart };
// Recalculate end for all-day event
const endDate = new Date(recurrenceData.dtstart + "T00:00:00");
endDate.setDate(endDate.getDate() + 1);
event.end = { date: format(endDate, "yyyy-MM-dd") };
} else if (recurrenceData.time) {
const dateTimeStr = `${recurrenceData.dtstart}T${recurrenceData.time}`;
const startDate = new Date(dateTimeStr);
event.start = {
dateTime: format(startDate, "yyyy-MM-dd'T'HH:mm:ssxxx"),
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
// Recalculate end based on duration
const duration = task.timeEstimate || settings.defaultEventDuration;
const endDate = new Date(startDate.getTime() + duration * 60 * 1000);
event.end = {
dateTime: format(endDate, "yyyy-MM-dd'T'HH:mm:ssxxx"),
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone,
};
}
}
}
} else if (clearRecurrence) {
// Explicitly clear recurrence when it was removed from the task
// Google Calendar API requires an empty array to remove recurrence
event.recurrence = [];
}
return event;
}
/**
* Sync a task to Google Calendar (create or update)
*/
async syncTaskToCalendar(task: TaskInfo, previous?: TaskInfo): Promise<void> {
if (!this.shouldSyncTask(task)) {
return;
}
const settings = this.plugin.settings.googleCalendarExport;
const existingEventId = this.getTaskEventId(task);
try {
// Check if recurrence was removed (previous had recurrence, current doesn't)
const clearRecurrence = !!(previous?.recurrence && !task.recurrence);
const eventData = this.taskToCalendarEvent(task, clearRecurrence);
if (!eventData) {
console.warn("[TaskCalendarSync] Could not convert task to event:", task.path);
return;
}
if (existingEventId) {
// Update existing event
await this.withGoogleRateLimit(() =>
this.googleCalendarService.updateEvent(
settings.targetCalendarId,
existingEventId,
eventData
)
);
} else {
// Create new event — pass structured start/end objects to preserve timeZone
const createdEvent = await this.withGoogleRateLimit(() =>
this.googleCalendarService.createEvent(
settings.targetCalendarId,
{
...eventData,
isAllDay: !!eventData.start.date,
}
)
);
// Extract the actual event ID from the ICSEvent ID format
// Format is "google-{calendarId}-{eventId}"
// Calendar IDs can contain hyphens, so strip the known prefix
const prefix = `google-${settings.targetCalendarId}-`;
const eventId = createdEvent.id.startsWith(prefix)
? createdEvent.id.slice(prefix.length)
: createdEvent.id;
// Save the event ID to the task's frontmatter
await this.saveTaskEventId(task.path, eventId);
}
} catch (error: any) {
// Check if it's a 404 error (event was deleted externally)
if (error.status === 404 && existingEventId) {
// Clear the stale link and retry as create
await this.removeTaskEventId(task.path);
// Retry without the link - refetch task to get updated version
const updatedTask = await this.plugin.cacheManager.getTaskInfo(task.path);
if (updatedTask) {
return this.syncTaskToCalendar(updatedTask, previous);
}
}
console.error("[TaskCalendarSync] Failed to sync task:", task.path, error);
// Show user-friendly message for token refresh errors
// TokenRefreshError indicates the OAuth connection expired and user needs to reconnect
if (error instanceof TokenRefreshError) {
new Notice(this.plugin.i18n.translate("settings.integrations.googleCalendarExport.notices.connectionExpired"));
} else {
new Notice(this.plugin.i18n.translate("settings.integrations.googleCalendarExport.notices.syncFailed", { message: error.message }));
}
}
}
/**
* Update a task in Google Calendar when it changes.
* Debounced to prevent rapid-fire API calls during quick successive edits.
*/
async updateTaskInCalendar(task: TaskInfo, previous?: TaskInfo): Promise<void> {
if (!this.plugin.settings.googleCalendarExport.syncOnTaskUpdate) {
return;
}
const taskPath = task.path;
// Store previous state for recurrence change detection
if (previous) {
this.previousTaskState.set(taskPath, previous);
}
// Cancel any pending debounced sync for this task
const existingTimer = this.pendingSyncs.get(taskPath);
if (existingTimer) {
clearTimeout(existingTimer);
}
// Store the authoritative task state passed to us so we don't rely on the async metadata cache
this.pendingTasks.set(taskPath, task);
// Return a promise that resolves when the debounced sync completes
return new Promise((resolve, reject) => {
const timer = setTimeout(async () => {
this.pendingSyncs.delete(taskPath);
// Wait for any in-flight sync to complete before starting a new one
const inFlight = this.inFlightSyncs.get(taskPath);
if (inFlight) {
await inFlight.catch(() => {}); // Ignore errors from previous sync
}
// Use the latest task data that was passed to us explicitly
const latestTask = this.pendingTasks.get(taskPath);
this.pendingTasks.delete(taskPath);
// Fallback to cache only if the pending task is missing
const freshTask = latestTask || await this.plugin.cacheManager.getTaskInfo(taskPath);
if (!freshTask) {
resolve();
return;
}
const syncPromise = this.executeTaskUpdate(freshTask);
this.inFlightSyncs.set(taskPath, syncPromise);
try {
await syncPromise;
resolve();
} catch (error) {
reject(error);
} finally {
this.inFlightSyncs.delete(taskPath);
}
}, SYNC_DEBOUNCE_MS);
this.pendingSyncs.set(taskPath, timer);
});
}
/**
* Internal method that performs the actual task update sync
*/
private async executeTaskUpdate(task: TaskInfo): Promise<void> {
const existingEventId = this.getTaskEventId(task);
// If task no longer meets sync criteria, delete the event
if (!this.shouldSyncTask(task)) {
if (existingEventId) {
const deleted = await this.deleteTaskFromCalendar(task);
if (!deleted) {
throw new Error(`Failed to delete task from Google Calendar: ${task.path}`);
}
}
// Clean up previous state
this.previousTaskState.delete(task.path);
return;
}
// Get previous state for recurrence change detection
const previousState = this.previousTaskState.get(task.path);
// Sync the updated task
await this.syncTaskToCalendar(task, previousState);
// Update previous state with current task
this.previousTaskState.set(task.path, task);
}
/**
* Handle task completion - update the calendar event.
* For recurring tasks, updates the EXDATE list to exclude the completed instance.
* For non-recurring tasks, adds a checkmark to the event title.
*/
async completeTaskInCalendar(task: TaskInfo): Promise<void> {
if (!this.plugin.settings.googleCalendarExport.syncOnTaskComplete) {
return;
}
const settings = this.plugin.settings.googleCalendarExport;
const existingEventId = this.getTaskEventId(task);
if (!existingEventId) {
return;
}
// For recurring tasks, update EXDATE to exclude completed instance
if (this.shouldSyncAsRecurring(task)) {
await this.updateRecurringEventExdates(task);
return;
}
try {
// Update the event title to indicate completion
const completedTitle = `✓ ${this.applyTitleTemplate(task)}`;
const description = settings.includeDescription
? this.buildEventDescription(task)
: undefined;
await this.withGoogleRateLimit(() =>
this.googleCalendarService.updateEvent(
settings.targetCalendarId,
existingEventId,
{
summary: completedTitle,
description,
}
)
);
} catch (error: any) {
if (error.status === 404) {
// Event was deleted externally, clean up the link
await this.removeTaskEventId(task.path);
return;
}
console.error("[TaskCalendarSync] Failed to update completed task:", task.path, error);
}
}
/**
* Updates a recurring event's EXDATE list when an instance is completed or skipped.
* This adds EXDATE entries for completed/skipped instances to hide them from the calendar.
*/
private async updateRecurringEventExdates(task: TaskInfo): Promise<void> {
if (!this.shouldSyncAsRecurring(task) || !task.recurrence) return;
const settings = this.plugin.settings.googleCalendarExport;
const eventId = this.getTaskEventId(task);
if (!eventId) return;
try {
const recurrenceData = convertToGoogleRecurrence(task.recurrence, {
completedInstances: task.complete_instances,
skippedInstances: task.skipped_instances,
});
if (recurrenceData) {
await this.withGoogleRateLimit(() =>
this.googleCalendarService.updateEvent(
settings.targetCalendarId,
eventId,
{ recurrence: recurrenceData.recurrence }
)
);
}
} catch (error: any) {
if (error.status === 404) {
// Event was deleted externally, clean up the link
await this.removeTaskEventId(task.path);
return;
}
console.error("[TaskCalendarSync] Failed to update recurring event EXDATEs:", task.path, error);
// Fall back to full resync
await this.syncTaskToCalendar(task);
}
}
/**
* Delete a task's calendar event
*/
async deleteTaskFromCalendar(task: TaskInfo): Promise<boolean> {
if (!this.plugin.settings.googleCalendarExport.syncOnTaskDelete) {
return true;
}
const settings = this.plugin.settings.googleCalendarExport;
const existingEventId = this.getTaskEventId(task);
if (!existingEventId) {
return true;
}
let deleteFailed = false;
try {
await this.withGoogleRateLimit(() =>
this.googleCalendarService.deleteEvent(
settings.targetCalendarId,
existingEventId
)
);
} catch (error: any) {
// 404 or 410 means event is already gone - that's fine
if (error.status !== 404 && error.status !== 410) {
deleteFailed = true;
console.error("[TaskCalendarSync] Failed to delete event:", task.path, error);
}
}
if (deleteFailed) {
return false;
}
// Only remove the event ID when deletion succeeded or the event is already gone
await this.removeTaskEventId(task.path);
return true;
}
/**
* Delete a task's calendar event by path (used when task is being deleted)
*/
async deleteTaskFromCalendarByPath(taskPath: string, eventId: string): Promise<void> {
if (!this.plugin.settings.googleCalendarExport.syncOnTaskDelete) {
return;
}
const settings = this.plugin.settings.googleCalendarExport;
try {
await this.withGoogleRateLimit(() =>
this.googleCalendarService.deleteEvent(settings.targetCalendarId, eventId)
);
} catch (error: any) {
// 404 or 410 means event is already gone - that's fine
if (error.status !== 404 && error.status !== 410) {
console.error("[TaskCalendarSync] Failed to delete event:", taskPath, error);
}
}
// No need to remove from frontmatter since the task file is being deleted
}
// handleTaskPathChange is no longer needed - event ID is stored in frontmatter
// and moves with the file automatically when renamed/moved
/**
* Sync all tasks to Google Calendar (initial sync or resync).
* Uses parallel execution with concurrency limits to improve performance.
*/