-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcal.go
92 lines (78 loc) · 2.03 KB
/
gcal.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
package pkg
import (
"bytes"
"context"
"fmt"
"io"
"time"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
type GCalStore struct {
calendarId string
mailsReader io.Reader
opts []option.ClientOption
}
func NewGCalStore(calendarId string, mails io.Reader, opts ...option.ClientOption) *GCalStore {
return &GCalStore{calendarId, mails, opts}
}
func (s *GCalStore) Events(when EventType) ([]Event, error) {
ctx := context.Background()
srv, err := calendar.NewService(ctx, s.opts...)
if err != nil {
return nil, fmt.Errorf("gcal: service error: %w", err)
}
var (
min, max time.Time
)
switch when {
case Today:
min = time.Now()
max = min.AddDate(0, 0, 1).Truncate(24 * time.Hour)
default:
min = time.Now().AddDate(0, 0, 1).Truncate(24 * time.Hour)
max = min.AddDate(0, 0, 1)
}
es, err := srv.Events.List(s.calendarId).
TimeMin(min.Format(time.RFC3339)).
TimeMax(max.Format(time.RFC3339)).
ShowDeleted(false).
SingleEvents(true).
OrderBy("startTime").
Do()
if err != nil {
return nil, fmt.Errorf("gcal: events error: %w", err)
}
mailsSource, err := io.ReadAll(s.mailsReader)
if err != nil {
return nil, fmt.Errorf("gcal: mails file reading error: %w", err)
}
var result []Event
for _, e := range es.Items {
mails := make([]string, len(e.Attendees))
for i, a := range e.Attendees {
mails[i] = a.Email
}
// TODO: Quick Dirty fix for rereading reader
names, err := MailsToNames(mails, bytes.NewReader(mailsSource))
if err != nil {
return result, fmt.Errorf("gcal: mails converting error: %w", err)
}
start, err := time.Parse(time.RFC3339, e.Start.DateTime)
if err != nil {
return result, fmt.Errorf("gcal: start time parse error: %w", err)
}
end, err := time.Parse(time.RFC3339, e.End.DateTime)
if err != nil {
return result, fmt.Errorf("gcal: end time parse error: %w", err)
}
result = append(result, Event{
e.Summary,
e.Location,
start.Format("02.01.2006 15:04"),
end.Format("02.01.2006 15:04"),
names,
})
}
return result, nil
}