-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.go
195 lines (168 loc) · 4.7 KB
/
calendar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package calendar
import (
"fmt"
"log"
"net/http"
"sort"
"time"
"github.com/emersion/go-ical"
// "github.com/teambition/rrule-go"
)
type Calendar struct {
url string
tz *time.Location
*ical.Calendar
*log.Logger
}
type EventData struct {
UID string
Start time.Time
End time.Time
Summary string
Location string
Description string
}
func NewCalendar(url string, l *log.Logger) (Calendar, error) {
calendar := Calendar{url: url, tz: time.Local}
resp, err := http.Get(url)
if err != nil {
return calendar, err
}
defer resp.Body.Close()
parser := ical.NewDecoder(resp.Body)
cal, err := parser.Decode()
if err != nil {
return calendar, err
}
calendar.Calendar = cal
calendar.Logger = l
return calendar, nil
}
func (cal Calendar) GetEventsOn(date time.Time) ([]EventData, error) {
all_events := make(map[string]ical.Event)
selected_events := make([]ical.Event, 0)
todayStart := GetDateWithoutTime(date)
todayEnd := todayStart.Add(24 * time.Hour)
for _, event := range cal.Events() {
uid := event.Props.Get(ical.PropUID).Value
// check for doubles via uid
if _, ok := all_events[uid]; ok {
createdProp := all_events[uid].Props.Get(ical.PropCreated)
if createdProp == nil {
continue
}
created_existing, err := createdProp.DateTime(cal.tz)
if err != nil {
continue
}
createdNewProp := event.Props.Get(ical.PropCreated)
if createdNewProp == nil {
continue
}
created_new, err := createdNewProp.DateTime(cal.tz)
if err != nil {
continue
}
if created_existing.After(created_new) {
continue
}
}
all_events[event.Props.Get(ical.PropUID).Value] = event
}
for _, event := range all_events {
start, err := event.DateTimeStart(cal.tz)
if err != nil {
return []EventData{}, err
}
end, err := event.DateTimeEnd(cal.tz)
if err != nil {
return []EventData{}, err
}
// regular event
if (start.After(todayStart) || start.Local() == todayStart.Local()) && start.Before(todayEnd) || (start.Before(todayStart) && end.After(todayEnd)) {
selected_events = append(selected_events, event)
continue
}
// recurring event
reccurenceSet, err := event.RecurrenceSet(cal.tz)
if err != nil {
cal.Printf("could not get recurrence set: %s\n", err)
continue
}
if reccurenceSet == nil {
// no recurrence
continue
}
if GetDateWithoutTime(reccurenceSet.After(todayStart, true)).Local() == GetDateWithoutTime(date).Local() {
selected_events = append(selected_events, event)
}
}
// Convert ical.Events to EventData
eventDatas := make([]EventData, 0)
for _, event := range selected_events {
eventData, err := cal.ConvertToEventData(event, date)
if err != nil {
return nil, err
}
eventDatas = append(eventDatas, eventData)
}
// sort events
sort.SliceStable(eventDatas, func(i, j int) bool {
start1 := eventDatas[i].Start
start2 := eventDatas[j].Start
return start1.Before(start2)
})
return eventDatas, nil
}
func GetDateWithoutTime(date time.Time) time.Time {
return time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local)
}
func (cal Calendar) ConvertToEventData(icalEvent ical.Event, d time.Time) (EventData, error) {
eventData := EventData{}
// Handle UID
uidProp := icalEvent.Props.Get(ical.PropUID)
if uidProp != nil {
eventData.UID = uidProp.Value
} else {
return eventData, fmt.Errorf("UID is missing for event %s", icalEvent.Name)
}
// Handle DTSTART
startProp := icalEvent.Props.Get(ical.PropDateTimeStart)
if startProp == nil {
return eventData, fmt.Errorf("DTSTART is missing for event %s", icalEvent.Name)
}
eventStart, err := startProp.DateTime(cal.tz)
if err != nil {
return eventData, err
}
eventData.Start = time.Date(d.Year(), d.Month(), d.Day(), eventStart.Hour(), eventStart.Minute(), 0, 0, d.Location())
// Handle DTEND
endProp := icalEvent.Props.Get(ical.PropDateTimeEnd)
if endProp != nil {
eventEnd, err := endProp.DateTime(cal.tz)
if err != nil {
return eventData, err
}
// Calculate the difference in days and adjust eventData.End
daysDiff := int(eventEnd.Sub(eventStart).Hours() / 24)
eventData.End = time.Date(d.Year(), d.Month(), d.Day()+daysDiff, eventEnd.Hour(), eventEnd.Minute(), 0, 0, d.Location())
}
// Handle SUMMARY
summaryProp := icalEvent.Props.Get(ical.PropSummary)
if summaryProp != nil {
eventData.Summary = summaryProp.Value
}
// Handle LOCATION
locationProp := icalEvent.Props.Get(ical.PropLocation)
if locationProp != nil {
eventData.Location = locationProp.Value
}
// Handle DESCRIPTION
descriptionProp := icalEvent.Props.Get(ical.PropDescription)
if descriptionProp != nil {
eventData.Description = descriptionProp.Value
} else {
eventData.Description = ""
}
return eventData, nil
}