-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface_types.go
200 lines (173 loc) · 4.16 KB
/
interface_types.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
package main
import (
"log"
"strings"
"time"
"github.com/apognu/gocal"
"google.golang.org/api/calendar/v3"
)
type Calendar struct {
Id string
Name string
Description string
Tag string
}
func NewCalendarFromGcal(e *calendar.CalendarListEntry, tag string) *Calendar {
return &Calendar{
Id: e.Id,
Name: e.Summary,
Description: e.Description,
Tag: tag,
}
}
func NewCalendarFromIcs(filename string, tag string) *Calendar {
return &Calendar{
Id: filename,
Name: "ICS: " + filename,
Description: "",
Tag: tag,
}
}
type Attachment struct {
Title string
FileUrl string
}
type ResponseStatus int
const (
ResponseUnknown = iota
ResponseAccepted
ResponseTentative
ResponseDeclined
)
type Attendee struct {
ResponseStatus ResponseStatus
Email string
Name string
}
type Event struct {
UID string
Name string
Description string
Attachments []*Attachment
Attendees []*Attendee
URL string
OrganizerMail string
Start *time.Time
End *time.Time
}
func convertAttendeeResponse(response string) ResponseStatus {
switch strings.ToLower(response) {
case "accepted":
return ResponseAccepted
case "declined":
return ResponseDeclined
case "tentative":
return ResponseTentative
case "needs-action":
fallthrough
case "needsaction":
fallthrough
default:
return ResponseUnknown
}
}
func NewEventFromGcal(e *calendar.Event) *Event {
// parse attachments
attachments := make([]*Attachment, 0)
for _, a := range e.Attachments {
attachments = append(attachments, &Attachment{
Title: a.Title,
FileUrl: a.FileUrl,
})
}
// parse attendees
attendees := make([]*Attendee, 0, len(e.Attendees))
for _, a := range e.Attendees {
attendees = append(attendees, &Attendee{
ResponseStatus: convertAttendeeResponse(a.ResponseStatus),
Email: a.Email,
Name: a.DisplayName,
})
}
// parse times
var start, end time.Time
if e.Start.Date != "" {
var err error
start, err = time.Parse("2006-01-02", e.Start.Date)
if err != nil {
log.Fatalf("could not parse time %s", e.Start.Date)
}
end, err = time.Parse("2006-01-02", e.End.Date)
if err != nil {
log.Fatalf("could not parse time %s", e.End.Date)
}
} else {
ts, err := time.Parse(time.RFC3339, e.Start.DateTime)
if err != nil {
log.Fatalf("could not parse time %s", e.Start.DateTime)
}
start = ts.In(time.Local)
if e.End == nil {
end = start
} else {
te, err := time.Parse(time.RFC3339, e.End.DateTime)
if err != nil {
log.Fatalf("could not parse time %s", e.Start.DateTime)
}
end = te.In(time.Local)
}
}
return &Event{
UID: e.ICalUID,
Name: e.Summary,
Description: e.Description,
Attachments: attachments,
Attendees: attendees,
URL: e.HtmlLink,
OrganizerMail: e.Organizer.Email,
Start: &start,
End: &end,
}
}
func NewEventFromIcs(e *gocal.Event) *Event {
attachments := make([]*Attachment, 0, len(e.Attachments))
for _, a := range e.Attachments {
attachments = append(attachments, &Attachment{
Title: a.Filename,
FileUrl: a.Value,
})
}
attendees := make([]*Attendee, 0, len(e.Attendees))
for _, a := range e.Attendees {
attendees = append(attendees, &Attendee{
ResponseStatus: convertAttendeeResponse(a.Status),
Name: a.Cn,
Email: a.Value,
})
}
var orgmail string
if e.Organizer != nil {
orgmail = strings.TrimPrefix(e.Organizer.Value, "mailto:")
}
description := strings.ReplaceAll(e.Description, "\\n", "\n")
var start, end time.Time
// Convert timestamps to local time if they aren't on a day boundary.
if !(e.Start.Hour() == 0 && e.Start.Minute() == 0 && e.End.Hour() == 23 && e.End.Minute() == 59) {
start = e.Start.In(time.Local)
end = e.End.In(time.Local)
} else {
start = *e.Start
end = *e.End
}
return &Event{
UID: e.Uid,
Name: e.Summary,
Description: description,
Attachments: attachments,
Attendees: attendees,
URL: e.URL,
OrganizerMail: orgmail,
Start: &start,
End: &end,
}
}