-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.go
308 lines (234 loc) · 6.05 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"errors"
"net/http"
"sort"
"sync"
"time"
"github.com/apognu/gocal"
"github.com/dolanor/caldav-go/caldav"
)
// calendar allows fetching events.
type calendar interface {
events() (calendarEvents, error)
}
type queryableCalendar interface {
eventsBetween(from, to time.Time) (calendarEvents, error)
}
// calendarEvent represents a single calendar item.
type calendarEvent struct {
from, to time.Time
text string
}
// cachedCalendar wraps a calendar caching its events.
type cachedCalendar struct {
cal calendar
period time.Duration
cache calendarEvents
lastUpdated time.Time
cleanTimer *time.Timer
mutex sync.RWMutex
}
// newCachedCalendar wrapping the given calendar, caching its events for the given period.
func newCachedCalendar(cal calendar, period time.Duration) *cachedCalendar {
return &cachedCalendar{cal: cal, period: period}
}
func (cal *cachedCalendar) events() (calendarEvents, error) {
cal.mutex.RLock()
if cal.cache == nil {
cal.mutex.RUnlock()
cal.mutex.Lock()
defer cal.mutex.Unlock()
var err error
cal.cache, err = cal.cal.events()
if err != nil {
return cal.cache, err
}
cal.lastUpdated = time.Now()
if cal.cleanTimer != nil {
cal.cleanTimer.Stop()
}
cal.cleanTimer = time.AfterFunc(cal.period, cal.clean)
return cal.cache, err
}
defer cal.mutex.RUnlock()
return cal.cache, nil
}
func (cal *cachedCalendar) clean() {
cal.mutex.Lock()
defer cal.mutex.Unlock()
cal.cache = nil
cal.cleanTimer = nil
}
// calDavCalendar implements calendar, fetches events from a caldav server.
type calDavCalendar struct {
mutex sync.Mutex
client *caldav.Client
}
func newCalDavCalendar(url string) (*calDavCalendar, error) {
server, err := caldav.NewServer(url)
if err != nil {
return nil, err
}
client := caldav.NewDefaultClient(server)
err = client.ValidateServer(url)
return &calDavCalendar{client: client}, err
}
func (cal *calDavCalendar) events() (calendarEvents, error) {
cal.mutex.Lock()
evs, err := cal.client.GetEvents("")
cal.mutex.Unlock()
if err != nil {
return []*calendarEvent{}, err
}
events := []*calendarEvent{}
for _, ev := range evs {
event := calendarEvent{
from: ev.DateStart.NativeTime(),
to: ev.DateEnd.NativeTime(),
text: ev.Summary,
}
events = append(events, &event)
}
sort.Sort(calendarEvents(events))
return events, nil
}
// iCalCalendar implements calendar, fetches events from a remote ical file.
type iCalCalendar struct {
url string
}
func newICalCalendar(url string) (*iCalCalendar, error) {
return &iCalCalendar{url}, nil
}
func (cal *iCalCalendar) events() (calendarEvents, error) {
// TODO: user agent
resp, err := http.Get(cal.url)
defer resp.Body.Close()
if err != nil {
return nil, err
}
c := gocal.NewParser(resp.Body)
c.Parse()
events := []*calendarEvent{}
for _, ev := range c.Events {
var start, end time.Time
if ev.Start != nil {
start = *ev.Start
}
if ev.End != nil {
end = *ev.End
}
event := calendarEvent{
from: start,
to: end,
text: ev.Summary,
}
events = append(events, &event)
}
sort.Sort(calendarEvents(events))
return events, nil
}
// combinedCalendar wraps multipe calendars.
type combinedCalendar []calendar
var errNoCalendars = errors.New("no calendars")
// eventsBetween gives the calendarEvents from the underlying calendars which start
// between the gives dates.
func (cals combinedCalendar) eventsBetween(from time.Time, until time.Time) (calendarEvents, error) {
var events []*calendarEvent
if len(cals) == 0 {
return events, errNoCalendars
}
for _, cal := range cals {
evs, err := cal.events()
evs = evs.between(from, until)
if err != nil {
// TODO: Multierror
return events, err
}
events = append(events, []*calendarEvent(evs)...)
}
sort.Sort(calendarEvents(events))
return events, nil
}
type eventDay struct {
dayStr string
day time.Time
events []calendarEvent
}
type calendarType string
var (
calendarTypeCalDav = calendarType("caldav")
calendarTypeICal = calendarType("ical")
)
// calendarEvents implements sort.Interface
type calendarEvents []*calendarEvent
func (c calendarEvents) between(from time.Time, until time.Time) calendarEvents {
var events []*calendarEvent
for _, ev := range c {
if from != (time.Time{}) && ev.from.Before(from) {
continue
}
if until != (time.Time{}) && until.Before(ev.from) {
continue
}
events = append(events, ev)
}
return events
}
// formatsToDays converts the events into days, to ease printing a calendar.
func (evs calendarEvents) formatToDays() []*eventDay {
days := []*eventDay{}
for _, ev := range evs {
cur := ev.from
fromStr := ev.from.Format("2006-01-02")
toStr := ev.to.Format("2006-01-02")
for {
curStr := cur.Format("2006-01-02")
var thisDay *eventDay
for i, day := range days {
if day.dayStr == curStr {
thisDay = days[i]
break
}
}
if thisDay == nil {
thisDay = &eventDay{dayStr: curStr, day: cur}
days = append(days, thisDay)
}
evCp := *ev
if fromStr != toStr {
if fromStr != curStr {
evCp.from = time.Date(evCp.from.Year(), evCp.from.Month(), evCp.from.Day(), 0, 0, 0, 0, evCp.from.Location())
}
if toStr != curStr {
evCp.to = time.Date(evCp.to.Year(), evCp.to.Month(), evCp.to.Day(), 0, 0, 0, 0, evCp.to.Location())
}
}
thisDay.events = append(thisDay.events, evCp)
if cur.Format("2006-01-02") == ev.to.Format("2006-01-02") {
break
}
cur = cur.AddDate(0, 0, 1)
}
}
return days
}
func (c calendarEvents) Len() int {
return len(c)
}
func (c calendarEvents) Less(i, j int) bool {
return c[i].from.Unix() < c[j].from.Unix()
}
func (c calendarEvents) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
type mockCalendar calendarEvents
func newMockCalendar(events []*calendarEvent) mockCalendar {
return mockCalendar(calendarEvents(events))
}
func (evs mockCalendar) events() (calendarEvents, error) {
return calendarEvents(evs), nil
}
func (evs mockCalendar) eventsBetween(from, to time.Time) (calendarEvents, error) {
return calendarEvents(evs).between(from, to), nil
}