From 818df6ff6a0ad92598bf276d713ffcb6e982c419 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Wed, 18 Dec 2024 15:50:07 +0100 Subject: [PATCH] transformation: add transformer to keep original event link Closes #56 --- example.sync.yaml | 1 + internal/adapter/google/event.go | 1 + internal/adapter/outlook_http/client.go | 1 + internal/models/event.go | 1 + internal/sync/transformer.go | 2 + internal/transformation/addOriginalLink.go | 19 ++++ .../transformation/addOriginalLink_test.go | 92 +++++++++++++++++++ 7 files changed, 117 insertions(+) create mode 100644 internal/transformation/addOriginalLink.go create mode 100644 internal/transformation/addOriginalLink_test.go diff --git a/example.sync.yaml b/example.sync.yaml index 4bd3d3e..2048d76 100644 --- a/example.sync.yaml +++ b/example.sync.yaml @@ -52,6 +52,7 @@ transformations: config: NewTitle: "[Synchronisierter Termin]" - name: KeepMeetingLink + - name: AddOriginalLink # Do not use KeepAttendees when the Outlook Adapter is used as a sink. There is no way to suppress mail invitations - name: KeepAttendees config: diff --git a/internal/adapter/google/event.go b/internal/adapter/google/event.go index f3ffca5..b5563bc 100644 --- a/internal/adapter/google/event.go +++ b/internal/adapter/google/event.go @@ -52,6 +52,7 @@ func calendarEventToEvent(e *calendar.Event, adapterSourceID string) models.Even Reminders: reminders, MeetingLink: e.HangoutLink, Accepted: hasEventAccepted, + HTMLLink: e.HtmlLink, } } diff --git a/internal/adapter/outlook_http/client.go b/internal/adapter/outlook_http/client.go index 589b04c..f4cdbfa 100644 --- a/internal/adapter/outlook_http/client.go +++ b/internal/adapter/outlook_http/client.go @@ -287,6 +287,7 @@ func (o OutlookClient) outlookEventToEvent(oe Event, adapterSourceID string) (e Reminders: reminders, MeetingLink: oe.OnlineMeetingUrl, Accepted: hasEventAccepted, + HTMLLink: oe.HtmlLink, } if oe.IsAllDay { diff --git a/internal/models/event.go b/internal/models/event.go index 7713c2f..181180c 100644 --- a/internal/models/event.go +++ b/internal/models/event.go @@ -28,6 +28,7 @@ type Event struct { Reminders Reminders MeetingLink string Accepted bool + HTMLLink string } type Reminders []Reminder diff --git a/internal/sync/transformer.go b/internal/sync/transformer.go index a9864fd..b514f16 100644 --- a/internal/sync/transformer.go +++ b/internal/sync/transformer.go @@ -35,6 +35,7 @@ var ( "PrefixTitle": &transformation.PrefixTitle{Prefix: ""}, "KeepTitle": &transformation.KeepTitle{}, "KeepMeetingLink": &transformation.KeepMeetingLink{}, + "AddOriginalLink": &transformation.AddOriginalLink{}, "KeepDescription": &transformation.KeepDescription{}, "KeepLocation": &transformation.KeepLocation{}, "KeepAttendees": &transformation.KeepAttendees{UseEmailAsDisplayName: false}, @@ -49,6 +50,7 @@ var ( "KeepReminders", "KeepDescription", "KeepMeetingLink", + "AddOriginalLink", "KeepTitle", "PrefixTitle", "ReplaceTitle", diff --git a/internal/transformation/addOriginalLink.go b/internal/transformation/addOriginalLink.go new file mode 100644 index 0000000..db45885 --- /dev/null +++ b/internal/transformation/addOriginalLink.go @@ -0,0 +1,19 @@ +package transformation + +import ( + "fmt" + "github.com/inovex/CalendarSync/internal/models" +) + +type AddOriginalLink struct{} + +func (t *AddOriginalLink) Name() string { + return "AddOriginalLink" +} + +func (t *AddOriginalLink) Transform(source models.Event, sink models.Event) (models.Event, error) { + if len(source.HTMLLink) > 0 { + sink.Description = fmt.Sprintf("original event link: %s\n\n############\n%s", source.HTMLLink, sink.Description) + } + return sink, nil +} diff --git a/internal/transformation/addOriginalLink_test.go b/internal/transformation/addOriginalLink_test.go new file mode 100644 index 0000000..a61bd33 --- /dev/null +++ b/internal/transformation/addOriginalLink_test.go @@ -0,0 +1,92 @@ +package transformation + +import ( + "testing" + + "github.com/inovex/CalendarSync/internal/models" + "github.com/stretchr/testify/assert" +) + +// verify keep attendees +func TestAddOriginalLink_Transform(t *testing.T) { + source := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "foo", + Description: "bar", + HTMLLink: "https://foo.com/bar?calendarId=testId", + } + sink := models.NewSyncEvent(source) + + addOriginalLink := AddOriginalLink{} + + event, err := addOriginalLink.Transform(source, sink) + + assert.Nil(t, err) + expectedEvent := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "CalendarSync Event", + Description: "original event link: https://foo.com/bar?calendarId=testId\n\n############\n", + } + + assert.Equal(t, expectedEvent, event) +} + +func TestAddOriginalLink_Transform_WithKeepDescription(t *testing.T) { + source := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "foo", + Description: "bar", + HTMLLink: "https://foo.com/bar?calendarId=testId", + } + sink := models.NewSyncEvent(source) + + keepDescription := KeepDescription{} + addOriginalLink := AddOriginalLink{} + + sink, err := keepDescription.Transform(source, sink) + event, err := addOriginalLink.Transform(source, sink) + + assert.Nil(t, err) + expectedEvent := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "CalendarSync Event", + Description: "original event link: https://foo.com/bar?calendarId=testId\n\n############\nbar", + } + + assert.Equal(t, expectedEvent, event) +} + +func TestAddOriginalLink_Transform_WithKeepDescriptionAndKeepMeetingLink(t *testing.T) { + source := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "foo", + Description: "bar", + MeetingLink: "https://meetinglink.de", + HTMLLink: "https://foo.com/bar?calendarId=testId", + } + sink := models.NewSyncEvent(source) + + keepDescription := KeepDescription{} + keepMeetingLink := KeepMeetingLink{} + addOriginalLink := AddOriginalLink{} + + sink, _ = keepDescription.Transform(source, sink) + sink, _ = keepMeetingLink.Transform(source, sink) + event, err := addOriginalLink.Transform(source, sink) + + assert.Nil(t, err) + expectedEvent := models.Event{ + ICalUID: "testId", + ID: "testUid", + Title: "CalendarSync Event", + Description: "original event link: https://foo.com/bar?calendarId=testId\n\n############\n" + + "original meeting link: https://meetinglink.de\n\n############\nbar", + } + + assert.Equal(t, expectedEvent, event) +}