Skip to content

Commit 6690cca

Browse files
committed
Refactor deletion and addition of cloud calendar events
1 parent 80e9923 commit 6690cca

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

internal/model/reminder_data.go

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type ReminderData struct {
3636

3737
// SyncCalendar syncs pending notes to Cloud Calendar.
3838
func (rd *ReminderData) SyncCalendar(calOptions *calendar.Options) error {
39+
lookAheadYears := 5
3940
if !EnableCalendar {
4041
logger.Warn("Google Calendar is disabled.")
4142
return nil
@@ -48,15 +49,13 @@ func (rd *ReminderData) SyncCalendar(calOptions *calendar.Options) error {
4849
return fmt.Errorf("Unable to retrieve Calendar client: %w", err)
4950
}
5051

51-
// Get list of all upcoming events
52+
// Get list of all upcoming events, and display them
5253
logger.Info("Fetch the list of all upcoming Calendar Events with each type of recurring event as single unit.")
53-
existingEvents, eventDetails, timeZone, err := calendar.FetchUpcomingEventsAndDetails(srv, 5, "")
54+
existingEvents, eventDetails, timeZone, err := calendar.FetchUpcomingEventsAndDetails(srv, 0, lookAheadYears, "")
5455
if err != nil {
5556
return err
5657
}
57-
5858
fmt.Println(eventDetails)
59-
6059
// Iterating through the Cloud Calendar Events
6160
fmt.Printf("Listing upcoming calendar events (%v):\n", len(existingEvents))
6261
fmt.Println("Note: It may take some time for Google Calendar read API to pick up the recently added events, or Calendar trash https://calendar.google.com/calendar/u/0/r/trash can also cause issues.")
@@ -67,52 +66,25 @@ func (rd *ReminderData) SyncCalendar(calOptions *calendar.Options) error {
6766
if item.Summary == "" {
6867
continue
6968
}
70-
owned := false
71-
if strings.HasPrefix(item.Summary, calendar.TitlePrefix) {
72-
owned = true
73-
}
74-
fmt.Printf(" - %v | %v | owned=%v\n", item.Summary, item.Recurrence, owned)
69+
fmt.Printf(" - %v | %v\n", item.Summary, item.Recurrence)
7570
}
7671
}
7772
fmt.Println() // just print a blank line
7873

74+
// Delete existing reminder events from Cloud Calendar
7975
// Note: Your might like to check your Calendar trash
8076
// https://calendar.google.com/calendar/u/0/r/trash and clear it from
8177
// time to time. Otherwise, this obstructs with visibility of newly
8278
// added event in the API call.
83-
logger.Info("Fetch all the events registered by reminder app (and some other matching the query).")
84-
reminderEvents, _, _, err := calendar.FetchUpcomingEventsAndDetails(srv, 5, calendar.TitlePrefix)
79+
logger.Info("Fetch all the events registered by reminder app.")
80+
reminderEvents, _, _, err := calendar.FetchUpcomingEventsAndDetails(srv, 2, lookAheadYears, calendar.TitlePrefix)
8581
if err != nil {
86-
return fmt.Errorf("Unable to retrieve the events: %v", err)
82+
return fmt.Errorf("Unable to retrieve the events: %w", err)
8783
}
88-
fmt.Printf("Listing matching events (%v) and deleting the ones registered by reminder app:\n", len(reminderEvents))
89-
if len(reminderEvents) == 0 {
90-
logger.Warn("No registered events found.")
91-
} else {
92-
deletionCount := 0
93-
for _, item := range reminderEvents {
94-
owned := false
95-
if strings.HasPrefix(item.Summary, calendar.TitlePrefix) {
96-
owned = true
97-
}
98-
fmt.Printf(" - %q -- %q (owned=%v)\n", calendar.EventString(item), item.Id, owned)
99-
if owned {
100-
if calOptions.DryMode {
101-
logger.Warn(fmt.Sprintf("Dry mode is enabled; skipping deletion of the event %q.", item.Id))
102-
// continue with next iteration
103-
continue
104-
}
105-
if err := srv.Events.Delete("primary", item.Id).Do(); err != nil {
106-
return fmt.Errorf("Couldn't delete the Calendar event %q | %q | %v", item.Id, item.Summary, err)
107-
} else {
108-
deletionCount += 1
109-
fmt.Printf(" - Deleted the Calendar event %q | %q\n", item.Id, calendar.EventString(item))
110-
}
111-
}
112-
}
113-
if deletionCount > 0 {
114-
fmt.Printf("\nWaring! Deletion count is %v; you might like to clear your trash manually by visiting https://calendar.google.com/calendar/u/0/r/trash\n", deletionCount)
115-
}
84+
fmt.Printf("Listing (%v) and deleting the events registered by the reminder app:\n", len(reminderEvents))
85+
err = calendar.DeleteEvents(srv, reminderEvents, calOptions.DryMode)
86+
if err != nil {
87+
return fmt.Errorf("Unable to delete the events: %w", err)
11688
}
11789

11890
// Add events to Cloud Calendar
@@ -121,15 +93,15 @@ func (rd *ReminderData) SyncCalendar(calOptions *calendar.Options) error {
12193
return err
12294
}
12395
waitFor := 10 * time.Second
124-
fmt.Printf("\nSyncing %v events (pending ones with due-date) to Google Calendar. Hit Ctrl-c if you don't want to do it at the moment. The process will wait for %v.\n", len(newEvents), waitFor)
96+
fmt.Printf("\nAdding %v events (pending ones with due-date) to Google Calendar. Hit Ctrl-c if you don't want to do it at the moment. The process will wait for %v.\n", len(newEvents), waitFor)
12597
fmt.Printf("waiting for %v...\n", waitFor)
12698
time.Sleep(waitFor)
127-
fmt.Println("Starting the syncing process.")
99+
fmt.Println("Starting the event addition process.")
128100
err = calendar.AddEvents(srv, newEvents, calOptions.DryMode)
129101
if err != nil {
130102
return err
131103
}
132-
fmt.Println("Done with syncing process.")
104+
fmt.Println("Done with event addition process.")
133105
return nil
134106
}
135107

pkg/calendar/calendar.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ import (
2222

2323
const TitlePrefix string = "[reminder] "
2424

25+
// DeleteEvents delete passed cloud calendar events
26+
func DeleteEvents(srv *gc.Service, events []*gc.Event, dryMode bool) error {
27+
if len(events) == 0 {
28+
logger.Warn("No registered events found.")
29+
} else {
30+
deletionCount := 0
31+
for _, item := range events {
32+
fmt.Printf(" - %q -- %q\n", EventString(item), item.Id)
33+
if dryMode {
34+
logger.Warn(fmt.Sprintf("Dry mode is enabled; skipping deletion of the event %q.", item.Id))
35+
// continue with next iteration
36+
continue
37+
}
38+
if err := srv.Events.Delete("primary", item.Id).Do(); err != nil {
39+
return fmt.Errorf("Couldn't delete the Calendar event %q | %q | %v", item.Id, item.Summary, err)
40+
} else {
41+
deletionCount += 1
42+
fmt.Printf(" - Deleted the Calendar event %q | %q\n", item.Id, EventString(item))
43+
}
44+
}
45+
if deletionCount > 0 {
46+
logger.Warn(fmt.Sprintf("\nWaring! Deletion count is %v; you might like to clear your trash manually by visiting https://calendar.google.com/calendar/u/0/r/trash\n", deletionCount))
47+
}
48+
}
49+
return nil
50+
}
51+
2552
// AddEvents adds passed calendar events to the Cloud
2653
func AddEvents(srv *gc.Service, events []*gc.Event, dryMode bool) error {
2754
for _, event := range events {
@@ -42,18 +69,18 @@ func AddEvents(srv *gc.Service, events []*gc.Event, dryMode bool) error {
4269

4370
// FetchUpcomingEvents returns slice of `Event` objects for
4471
// specified number of years, with some default settings.
45-
func FetchUpcomingEventsAndDetails(srv *gc.Service, years int, query string) ([]*gc.Event, string, string, error) {
72+
func FetchUpcomingEventsAndDetails(srv *gc.Service, backYears int, aheadYears int, query string) ([]*gc.Event, string, string, error) {
4673
// Get list of all upcomming events, with recurring events as a
4774
// unit (and not as separate single events).
4875
var allEvents []*gc.Event
4976
var calendarDetails string
5077
var timeZone string
5178
currentTime := time.Now()
52-
tStart := currentTime.Format(time.RFC3339)
53-
tStop := currentTime.AddDate(years, 0, 0).Format(time.RFC3339) // until given number of years from now
79+
tStart := currentTime.AddDate(-backYears, 0, 0).Format(time.RFC3339)
80+
tStop := currentTime.AddDate(aheadYears, 0, 0).Format(time.RFC3339) // until given number of aheadYears from now
5481
var pageToken string
5582
maxPage := 25 // just a temporarily hard limit (not expected to be reached) to keep the loop bounded
56-
logger.Info(fmt.Sprintf("Fetching Calendar items with query %q, of %d years starting from %s", query, years, tStart))
83+
logger.Info(fmt.Sprintf("Fetching Calendar items with query %q, of %d years starting from %s", query, aheadYears, tStart))
5784
for i := 0; i < maxPage; i++ {
5885
logger.Info(fmt.Sprintf("Fetching Page-%d with token %q", i, pageToken))
5986
eventsList := srv.Events.List("primary").

0 commit comments

Comments
 (0)