Skip to content

Commit 2144158

Browse files
committed
feat: add TimeExclude filter
1 parent 9b57e42 commit 2144158

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed

internal/filter/timeExclude.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package filter
2+
3+
import (
4+
"github.com/inovex/CalendarSync/internal/models"
5+
)
6+
7+
type TimeExcludeEvents struct {
8+
HourStart int
9+
HourEnd int
10+
}
11+
12+
func (a TimeExcludeEvents) Name() string {
13+
return "TimeExclude"
14+
}
15+
16+
func (a TimeExcludeEvents) Filter(event models.Event) bool {
17+
// if it is an all-day event, it should not be filtered here,
18+
// the AllDayEvents filter should be used instead
19+
if event.AllDay {
20+
return true
21+
}
22+
23+
// if start time and end time are inside the timeframe: exclude
24+
// example: event from 12:15-12:45, timeframe is 12-13
25+
// starttime and endtime are inside the timeframe
26+
if event.StartTime.Hour() >= a.HourStart && event.EndTime.Hour() <= a.HourEnd {
27+
return false
28+
}
29+
30+
// if the endtime is inside the timeframe, but the starttime is outside,
31+
// or if the starttime is inside the timeframe, but the endtime is outside,
32+
// or in any other case: keep the event
33+
return true
34+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package filter_test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/inovex/CalendarSync/internal/filter"
8+
"github.com/inovex/CalendarSync/internal/models"
9+
)
10+
11+
// Events which match the start and end hour should be kept
12+
func TestTimeExcludeEventsFilter(t *testing.T) {
13+
const timeFormat string = "2006-01-02T15:04"
14+
15+
t1, err := time.Parse(timeFormat, "2024-01-01T08:00")
16+
if err != nil {
17+
t.Error(err)
18+
}
19+
20+
t2, err := time.Parse(timeFormat, "2024-01-01T12:15")
21+
if err != nil {
22+
t.Error(err)
23+
}
24+
25+
t3, err := time.Parse(timeFormat, "2024-01-01T12:00")
26+
if err != nil {
27+
t.Error(err)
28+
}
29+
30+
t4, err := time.Parse(timeFormat, "2024-01-01T13:00")
31+
if err != nil {
32+
t.Error(err)
33+
}
34+
35+
sourceEvents := []models.Event{
36+
// Should be kept (all-day event)
37+
{
38+
ICalUID: "testId",
39+
ID: "testUid",
40+
Title: "all-day",
41+
Description: "bar",
42+
AllDay: true,
43+
StartTime: t1,
44+
EndTime: t1.Add(time.Hour * 1),
45+
},
46+
// Should be kept, as Start AND Endtime are outside the excluded times
47+
{
48+
ICalUID: "testId",
49+
ID: "testUid",
50+
Title: "outside frame",
51+
Description: "bar",
52+
AllDay: false,
53+
StartTime: t1,
54+
EndTime: t1.Add(time.Hour * 1),
55+
},
56+
// Should be filtered, as the Start and EndTime are inside the excluded times
57+
{
58+
ICalUID: "testId2",
59+
ID: "testUid2",
60+
Title: "inside frame",
61+
Description: "bar",
62+
AllDay: false,
63+
StartTime: t2,
64+
EndTime: t2.Add(time.Minute * 30),
65+
},
66+
// Should be filtered, as the end time is part of the timeframe
67+
{
68+
ICalUID: "testId4",
69+
ID: "inside frame 2",
70+
Title: "foo",
71+
Description: "bar",
72+
StartTime: t2,
73+
EndTime: t2.Add(time.Minute * 45),
74+
},
75+
// Should be filtered, as start and end times are at exclusion borders
76+
{
77+
ICalUID: "testId5",
78+
ID: "testUid4",
79+
Title: "inside frame 3",
80+
Description: "bar",
81+
StartTime: t3,
82+
EndTime: t3.Add(time.Hour * 1),
83+
},
84+
// Should be kept, as the start time is at the end of the exclusion border
85+
{
86+
ICalUID: "testId5",
87+
ID: "testUid4",
88+
Title: "outside frame 2",
89+
Description: "bar",
90+
StartTime: t4,
91+
EndTime: t4.Add(time.Hour * 1),
92+
},
93+
}
94+
95+
expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[1], sourceEvents[5]}
96+
97+
eventFilter := filter.TimeExcludeEvents{
98+
// Events from 12 to 13 should be filtered
99+
HourStart: 12,
100+
HourEnd: 13,
101+
}
102+
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents)
103+
}

internal/filter/timeFrame_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"github.com/inovex/CalendarSync/internal/models"
99
)
1010

11-
const timeFormat string = "2006-01-02T15:04"
12-
1311
// Events which match the start and end hour should be kept
1412
func TestTimeFrameEventsFilter(t *testing.T) {
13+
const timeFormat string = "2006-01-02T15:04"
14+
1515
t1, err := time.Parse(timeFormat, "2024-01-01T13:00")
1616
if err != nil {
1717
t.Error(err)

internal/sync/filter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ func FilterEvent(event models.Event, filters ...Filter) (result bool) {
3131
var (
3232
filterConfigMapping = map[string]Filter{
3333
"TimeFrame": &filter.TimeFrameEvents{},
34+
"TimeExclude": &filter.TimeExcludeEvents{},
3435
"DeclinedEvents": &filter.DeclinedEvents{},
3536
"AllDayEvents": &filter.AllDayEvents{},
3637
"RegexTitle": &filter.RegexTitle{},
3738
}
3839

3940
filterOrder = []string{
4041
"TimeFrame",
42+
"TimeExclude",
4143
"DeclinedEvents",
4244
"AllDayEvents",
4345
"RegexTitle",

0 commit comments

Comments
 (0)