-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal_service.go
143 lines (119 loc) · 2.66 KB
/
cal_service.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
package main
import (
"context"
"google.golang.org/api/calendar/v3"
"os"
"time"
"google.golang.org/api/option"
)
type CalendarService interface {
Init(ctx context.Context) error
GetRecentEvents(ctx context.Context, since time.Time) ([]CalendarEvent, error)
}
type EventStatus int
const (
StatusCreated EventStatus = iota
StatusUpdated
StatusCanceled
StatusUnknown
)
type CalendarEvent struct {
Title string
Start string
End string
Creator string
Status EventStatus
}
func NewCalendarService(cfg Config) CalendarService {
return &calendarClient{
cfg: cfg,
}
}
type calendarClient struct {
cfg Config
svc *calendar.Service
}
func (c *calendarClient) Init(ctx context.Context) error {
b, err := os.ReadFile(c.cfg.GoogleServiceAccountFile)
if err != nil {
return err
}
svc, err := calendar.NewService(ctx, option.WithCredentialsJSON(b))
if err != nil {
return err
}
c.svc = svc
return nil
}
func (c *calendarClient) GetRecentEvents(ctx context.Context, since time.Time) ([]CalendarEvent, error) {
events, err := c.svc.Events.
List(c.cfg.CalendarId).
ShowDeleted(true).
SingleEvents(true).
UpdatedMin(since.Format(time.RFC3339)).
MaxResults(10).
OrderBy("updated").
Context(ctx).
Do()
if err != nil {
return nil, err
}
resp := make([]CalendarEvent, 0, len(events.Items))
for _, e := range events.Items {
resp = append(resp, CalendarEvent{
Title: e.Summary,
Start: parseEventStart(e),
End: parseEventEnd(e),
Creator: getEventCreator(e),
Status: parseEventStatus(e),
})
}
return resp, nil
}
const (
googleStatusConfirmed = "confirmed"
googleStatusCancelled = "cancelled"
)
func getEventCreator(event *calendar.Event) string {
if event.Creator == nil {
return ""
}
return event.Creator.Email
}
func parseEventStatus(event *calendar.Event) EventStatus {
switch event.Status {
case googleStatusConfirmed:
if isEventUpdated(event) {
return StatusUpdated
} else {
return StatusCreated
}
case googleStatusCancelled:
return StatusCanceled
default:
return StatusUnknown
}
}
func isEventUpdated(event *calendar.Event) bool {
created, err := time.Parse(time.RFC3339, event.Created)
if err != nil {
return false
}
updated, err := time.Parse(time.RFC3339, event.Updated)
if err != nil {
return false
}
return updated.Sub(created) >= time.Second
}
func parseEventStart(event *calendar.Event) string {
return parseEventTime(event.Start)
}
func parseEventEnd(event *calendar.Event) string {
return parseEventTime(event.End)
}
func parseEventTime(eventDateTime *calendar.EventDateTime) string {
if eventDateTime.Date != "" {
return eventDateTime.Date
}
return eventDateTime.DateTime
}