Implemented create and delete sync logic

This commit is contained in:
Andreas Schneider 2018-04-01 20:20:02 +02:00
parent 0209309bac
commit f71ec34f48
2 changed files with 62 additions and 13 deletions

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"log"
"time"
)
@ -12,28 +11,78 @@ func main() {
e := NewEWSCalendar(s.EWS.URL, s.EWS.Username, s.EWS.Password)
id, err := e.getCalendarFolderID()
if err != nil {
log.Println(err)
return
log.Fatalln(err)
}
items, err := e.getCalendarItems(id, time.Now().AddDate(0, -1, 0), time.Now().AddDate(0, 2, 0))
items, err := e.getCalendarItems(id,
time.Now().AddDate(0, -1, 0), // One month past
time.Now().AddDate(0, 2, 0)) // Two months ahead
if err != nil {
log.Println(err)
return
log.Fatalln(err)
}
relevantEWSItems := make(map[string]CalendarItem)
for _, item := range items {
fmt.Printf("%#v\n", item)
// Ignore private items.
if item.Sensitivity != "Private" {
// None-private items though ... remember them by hash.
// The hash will equal the CalDAV UID (and its filename).
relevantEWSItems[item.Hash()] = item
}
}
c := NewCalDAV(s.CalDAV)
calDavItems, err := c.GetEvents()
if err != nil {
log.Println(err)
return
log.Fatalln(err)
}
// Build a map for easier lookup by UID
calDavItemMap := make(map[string]CalDAVItem)
for _, item := range calDavItems {
fmt.Printf("%s\n UID: %s\n Summary: %s\n Start: %s\n End: %s\n",
item.HRef, item.UID(), item.Summary(), item.Start(), item.End())
calDavItemMap[item.UID()] = item
}
// First step: find items that were changed or that were removed
// and update the CalDAV side accordingly.
for uid, calDavItem := range calDavItemMap {
if ewsItem, ok := relevantEWSItems[uid]; ok {
// Good, so we still know the item at least.
if !ewsItem.Start.Equal(calDavItem.Start()) ||
!ewsItem.End.Equal(calDavItem.End()) {
// So times have changed. Update.
// TODO
}
} else {
// Oops, seems the item vanished.
err := c.DeleteItem(calDavItem)
if err == nil {
log.Printf("Deleted item %s (%s)\n", calDavItem.UID(), calDavItem.Summary())
} else {
// Not fatal, but worth a note.
log.Println(err)
}
}
}
// Find items we don't know so far and create them.
for uid, ewsItem := range relevantEWSItems {
if _, ok := calDavItemMap[uid]; !ok {
title := s.Anonymize.Title
if title == "" {
// No anonymization? Fine.
title = ewsItem.Subject
}
ical := CreateICal(ewsItem.Hash(), title, ewsItem.Start, ewsItem.End)
calDavItem := CalDAVItem{HRef: uid + ".ics", ICal: ical}
err := c.UploadItem(calDavItem)
if err == nil {
log.Printf("Created item %s (%s)\n", uid, ewsItem.Subject)
} else {
log.Println(err)
}
}
}
}

View File

@ -174,7 +174,7 @@ func NewCalDAV(settings ServerSettings) *CalDAV {
type CalDAVItem struct {
HRef string
ICal
*ICal
}
func (c *CalDAV) GetEvents() ([]CalDAVItem, error) {
@ -199,7 +199,7 @@ func (c *CalDAV) GetEvents() ([]CalDAVItem, error) {
result := make([]CalDAVItem, 0, len(ms.Response))
for _, p := range ms.Response {
item := CalDAVItem{p.HRef, p.PropStat.Prop.CalendarData}
item := CalDAVItem{p.HRef, &p.PropStat.Prop.CalendarData}
result = append(result, item)
}