Skip to content

Commit

Permalink
gcal: leave manually created events alone
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyabhi committed Oct 9, 2023
1 parent 1998c61 commit 27c5988
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions calendar/gcal/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
googlecalendar "google.golang.org/api/calendar/v3"
)

const EventSourceTitle = "calsync"

// SyncCalendar will sync all events to Google Calendar
// - If event is present in Google Calendar but not in local calendar, it will be deleted
func (c *Client) SyncCalendar(calEvents []calendar.Event) error {
Expand All @@ -29,6 +31,7 @@ func (c *Client) SyncCalendar(calEvents []calendar.Event) error {
foundIndices := make([]int, 0)
// Clean up stale events at Google Calendar
for _, event := range eventsFromGoogle {
// Events already created, skip them
exists, position := dupFinder.isGCalinEvents(event, calEvents)
if exists {
log.Printf("Already synced: %s %s:%s", event.Summary, event.Start.DateTime, event.End.DateTime)
Expand All @@ -37,9 +40,15 @@ func (c *Client) SyncCalendar(calEvents []calendar.Event) error {
}

// Exists in Google, but not local calendar, time to delete
log.Printf("Stale event, deleting: %s %s:%s", event.Summary, event.Start.DateTime, event.End.DateTime)
if err := c.Svc.Events.Delete(c.workCalID, event.Id).Do(); err != nil {
return fmt.Errorf("Cleanup up existing event failed: %w", err)
if event.Source != nil && event.Source.Title == EventSourceTitle {
log.Printf("Stale, deleting: %s %s:%s", event.Summary, event.Start.DateTime, event.End.DateTime)
if err := c.Svc.Events.Delete(c.workCalID, event.Id).Do(); err != nil {
return fmt.Errorf("Cleanup up existing event failed: %w", err)
}
} else {
// Manually created event, not via calsync, leave it alone!
log.Printf("Skipped deletion: this is not calsync managed: %s %s:%s",
event.Summary, event.Start.DateTime, event.End.DateTime)
}
}

Expand All @@ -50,7 +59,7 @@ func (c *Client) SyncCalendar(calEvents []calendar.Event) error {
continue
}
if err := c.publishEvent(event); err != nil {
return fmt.Errorf("Publishing the event failed: event: %s , %w", event, err)
return fmt.Errorf("publishing event: %s ,%w", event, err)
}
}

Expand Down Expand Up @@ -130,7 +139,7 @@ func (c *Client) DeleteAllEvents() error {

for _, event := range eventsFromGoogle {
// Manually created event, not via calsync, leave it alone!
if event.Source.Title != "calsync" {
if event.Source.Title != EventSourceTitle {
log.Printf("Skipping deletion of event: %s", event.Summary)
}

Expand Down

0 comments on commit 27c5988

Please sign in to comment.