Skip to content

Commit

Permalink
events: hash now includes "description"
Browse files Browse the repository at this point in the history
  • Loading branch information
shadyabhi committed Oct 24, 2023
1 parent 9ed20d5 commit d98e383
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
14 changes: 7 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "main.go"
}

]
}
19 changes: 16 additions & 3 deletions calendar/calendar.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package calendar

import "time"
import (
"bytes"
"crypto/md5"
"encoding/hex"
"time"
)

type Calendar interface {
GetEvents()
Expand All @@ -19,6 +24,14 @@ func (e Event) String() string {
return e.Title + " " + e.Start.UTC().String() + " " + e.Stop.UTC().String() + " " + e.UID
}

func (e Event) EventHash() string {
return e.Title + e.Start.Format(time.RFC3339) + e.Stop.Format(time.RFC3339)
func (e Event) Hash() string {
var buffer bytes.Buffer
buffer.WriteString(e.Title)
buffer.WriteString(e.Start.Format(time.RFC3339))
buffer.WriteString(e.Stop.Format(time.RFC3339))
buffer.WriteString(e.Notes)

md5sum := md5.Sum(buffer.Bytes())

return hex.EncodeToString(md5sum[:])
}
11 changes: 5 additions & 6 deletions calendar/gcal/dupfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"log"

"calsync/calendar"

googlecalendar "google.golang.org/api/calendar/v3"
)

type DuplicateEventsFinder struct {
Expand All @@ -18,17 +16,18 @@ func newDuplicateEventsFinder() *DuplicateEventsFinder {
}
}

func (d *DuplicateEventsFinder) isGCalinEvents(gCalEvent *googlecalendar.Event, events []calendar.Event) (bool, int) {
eventHash := gCalEvent.Summary + gCalEvent.Start.DateTime + gCalEvent.End.DateTime
func (d *DuplicateEventsFinder) isGCalinEvents(event *Event, events []calendar.Event) (bool, int) {

Check failure on line 19 in calendar/gcal/dupfinder.go

View workflow job for this annotation

GitHub Actions / build

undefined: Event

Check failure on line 19 in calendar/gcal/dupfinder.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: Event
eventHash := event.Hash()

_, ok := d.alreadySeen[eventHash]
if ok {
log.Printf("Event already processed before, should be a duplicate: %s %s:%s", gCalEvent.Summary, gCalEvent.Start.DateTime, gCalEvent.End.DateTime)
log.Printf("Event already processed before, should be a duplicate: %s %s:%s", event.Summary, event.Start.DateTime, event.End.DateTime)
return false, -1
}

d.alreadySeen[eventHash] = true
for i, e := range events {
if e.EventHash() == eventHash {
if e.Hash() == eventHash {
return true, i
}
}
Expand Down
11 changes: 8 additions & 3 deletions calendar/gcal/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func (c *Client) SyncCalendar(calEvents []calendar.Event) error {
return nil
}

func (c *Client) GetAllGCalEvents(endTime time.Time) ([]*googlecalendar.Event, error) {
func (c *Client) GetAllGCalEvents(endTime time.Time) ([]*Event, error) {

Check failure on line 79 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / build

undefined: Event

Check failure on line 79 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: Event
start := time.Now()
log.Printf("Start getting all events...")

// Hack: Truncate works with UTC, so we need to include the whole day
// If we run script at 01:00, we shouldn't miss events from 00:00-01:00
startTimeMidnight := time.Now().Add(-24 * time.Hour).Truncate(24 * time.Hour)
eventsFromGoogle, err := c.Svc.Events.List(c.workCalID).
gEvents, err := c.Svc.Events.List(c.workCalID).
ShowDeleted(false).
SingleEvents(true).
// 2500 is the max possible from API
Expand Down Expand Up @@ -119,7 +119,12 @@ func (c *Client) GetAllGCalEvents(endTime time.Time) ([]*googlecalendar.Event, e

log.Printf("Finished getting all events in %s", time.Since(start))

return eventsFromGoogle.Items, nil
events := make([]*Event, 0)

Check failure on line 122 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / build

undefined: Event

Check failure on line 122 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: Event
for _, event := range gEvents.Items {
events = append(events, &Event{event})

Check failure on line 124 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / build

undefined: Event

Check failure on line 124 in calendar/gcal/publish.go

View workflow job for this annotation

GitHub Actions / goreleaser

undefined: Event
}

return events, nil
}

// PublishAllEvents unconditionally publishes new events to Google Calendar, without checking if they already exist
Expand Down
3 changes: 0 additions & 3 deletions calendar/maccalendar/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func getEvent(raw string) (calendar.Event, error) {
} else {
// time has "at"
// First part is date: <date> at <time>
// or
// First part is: "tomorrow at <time>"

timeParts[1] = strings.Replace(timeParts[1], " at ", " ", 1)
}

Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ go 1.20

require (
github.com/BurntSushi/toml v1.3.2
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/oauth2 v0.11.0
google.golang.org/api v0.134.0
)

require (
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.4 // indirect
Expand All @@ -20,6 +20,7 @@ require (
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.12.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down

0 comments on commit d98e383

Please sign in to comment.