-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnector.go
450 lines (375 loc) · 10.1 KB
/
connector.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package googlecal
//revive:disable:cyclomatic
import (
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
global "github.com/tktip/google-calendar/pkg/googlecal"
"golang.org/x/net/context"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
var (
configs CalendarConfig
)
func init() {
b, err := ioutil.ReadFile(os.Getenv("CREDENTIALS"))
if err != nil {
log.Fatalf("Unable to read client secret file: %v", err)
}
configs, err = ParseConfig(b)
if err != nil {
log.Fatalf("Unable to parse client secret file as config: %v", err)
}
}
//CalendarConnector - base struct of dsl.
type CalendarConnector struct {
domain DomainName
context context.Context
//Use pointers to allow patch semantics
guestsCanModify *bool
autoAccept *bool
guestsCanInvite *bool
guestsCanSeeGuests *bool
privateEvent *bool
informGuestsAboutUpdates *bool
}
//NewCalendarConnector - create calendar connector
func NewCalendarConnector(ctx context.Context, domain string) *CalendarConnector {
eC := CalendarConnector{
domain: DomainName(domain),
context: ctx,
}
return &eC
}
func (e *CalendarConnector) getCalendarService() (*calendar.Service, error) {
config := configs[e.domain]
if config == nil {
return nil, ErrorUnknownDomain
}
if e.context == nil {
panic("no context provided")
}
srv, err := calendar.NewService(e.context,
option.WithTokenSource(
config.TokenSource(e.context),
),
)
return srv, err
}
//GuestsCanModify - whether guests can alter event
func (e *CalendarConnector) GuestsCanModify(b *bool) *CalendarConnector {
e.guestsCanModify = b
return e
}
//GuestsAutoAccept - whether event is auto accepted by guests
func (e *CalendarConnector) GuestsAutoAccept(b *bool) *CalendarConnector {
e.autoAccept = b
return e
}
//EventIsprivate - whether event is private or not
func (e *CalendarConnector) EventIsprivate(b *bool) *CalendarConnector {
e.privateEvent = b
return e
}
//GuestsMayInviteOthers - whether guests may invite others
func (e *CalendarConnector) GuestsMayInviteOthers(b *bool) *CalendarConnector {
e.guestsCanInvite = b
return e
}
//GuestsMaySeeOtherGuests - whether guests may see other guests
func (e *CalendarConnector) GuestsMaySeeOtherGuests(b *bool) *CalendarConnector {
e.guestsCanSeeGuests = b
return e
}
//InformGuestsAboutUpdates - whether mail should be sent on event change
func (e *CalendarConnector) InformGuestsAboutUpdates(b *bool) *CalendarConnector {
e.informGuestsAboutUpdates = b
return e
}
func isValidEventID(ID string) bool {
if len(ID) < 5 || len(ID) > 1024 {
return false
}
matched, err := regexp.MatchString("^[a-z0-9]*$", ID)
if err != nil {
panic(err.Error())
}
return matched
}
//isNewEventValid - checks if event contains mandatory fields
func isNewEventValid(event global.Event) error {
if event.Start == nil || *event.Start == "" || event.End == nil || *event.End == "" {
return ErrorMissingDates
} else if event.Title == nil || *event.Title == "" {
return ErrorMissingTitle
}
return nil
}
//common functionality for event create, update & patch
func (e *CalendarConnector) copyGoogleEventUpdate(event global.Event, update *calendar.Event) {
if update == nil {
return
}
if e.guestsCanModify != nil {
update.GuestsCanModify = *e.guestsCanModify
}
if e.privateEvent != nil {
visibility := "default"
if *e.privateEvent {
visibility = "private"
}
update.Visibility = visibility
}
if e.guestsCanInvite != nil {
update.GuestsCanInviteOthers = e.guestsCanInvite
}
if e.guestsCanSeeGuests != nil {
update.GuestsCanSeeOtherGuests = e.guestsCanSeeGuests
}
if event.Title != nil && *event.Title != "" {
update.Summary = *event.Title
}
if event.Location != nil {
update.Location = *event.Location
}
if event.Description != nil {
update.Description = *event.Description
}
if event.Start != nil {
update.Start = &calendar.EventDateTime{
DateTime: *event.Start,
TimeZone: "Europe/Oslo",
}
}
if event.End != nil {
update.End = &calendar.EventDateTime{
DateTime: *event.End,
TimeZone: "Europe/Oslo",
}
}
respStatus := "needsAction"
if e.autoAccept != nil && *e.autoAccept {
respStatus = "accepted"
}
fmt.Println("RespStatus: " + respStatus)
if event.Participants != nil {
participants := []*calendar.EventAttendee{}
for _, participant := range *event.Participants {
participants = append(participants, &calendar.EventAttendee{
Email: participant,
ResponseStatus: respStatus,
})
}
update.Attendees = participants
}
if event.Organizer != nil {
update.Organizer = event.Organizer
}
}
//CreateEvent creates and uploads an event in Google Calendar
//based on contents of a global.Event struct
func (e *CalendarConnector) CreateEvent(event global.Event) (eventID string, err error) {
err = isNewEventValid(event)
if err != nil {
return "", err
}
if event.ID != nil && !isValidEventID(*event.ID) {
return "", ErrorBadID
}
srv, err := e.getCalendarService()
if err != nil {
return "", err
}
gEvent := calendar.Event{}
if event.ID != nil {
gEvent.Id = *event.ID
}
e.copyGoogleEventUpdate(event, &gEvent)
insert := srv.Events.Insert("primary", &gEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
insert = insert.SendUpdates("all")
}
_event, err := insert.Do()
if err != nil {
return "", err
}
return _event.Id, nil
}
//DeleteEvent deletes event with ID
func (e *CalendarConnector) DeleteEvent(eventID string) error {
if eventID == "" {
return ErrorMissingEventID
}
config := configs[e.domain]
if config == nil {
return ErrorUnknownDomain
}
srv, err := e.getCalendarService()
if err != nil {
return err
}
delete := srv.Events.Delete("primary", eventID)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
delete = delete.SendUpdates("all")
}
return delete.Do()
}
//PatchEvent updates an existing event using patch semantics
//Note: Will not replace entire event, just specified fields.
func (e *CalendarConnector) PatchEvent(event global.Event) error {
if event.ID == nil || *event.ID == "" {
return ErrorMissingEventID
}
srv, err := e.getCalendarService()
if err != nil {
return err
}
gEvent := calendar.Event{}
e.copyGoogleEventUpdate(event, &gEvent)
patch := srv.Events.Patch("primary", *event.ID, &gEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
patch = patch.SendUpdates("all")
}
_, err = patch.Do()
return err
}
//UpdateEvent updates an existing event (overwrite)
//Note: Will overwrite any existing fields, as entire event object is replaced.
func (e *CalendarConnector) UpdateEvent(event global.Event) error {
if event.ID == nil || *event.ID == "" {
return ErrorMissingEventID
}
err := isNewEventValid(event)
if err != nil {
return err
}
srv, err := e.getCalendarService()
if err != nil {
return err
}
gEvent := calendar.Event{}
e.copyGoogleEventUpdate(event, &gEvent)
update := srv.Events.Update("primary", *event.ID, &gEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
update = update.SendUpdates("all")
}
_, err = update.Do()
return err
}
//RemoveParticipants removes specified participants from an event
func (e *CalendarConnector) RemoveParticipants(eventID string, toRemove []string) error {
if eventID == "" {
return ErrorMissingEventID
}
srv, err := e.getCalendarService()
if err != nil {
return err
}
emailsToIgnore := map[string]bool{}
for _, v := range toRemove {
emailsToIgnore[v] = true
}
existingEvent, err := e.GetCalendarEvent(eventID)
if err != nil {
return err
}
participants := []*calendar.EventAttendee{}
for _, v := range existingEvent.Attendees {
if !emailsToIgnore[v.Email] {
participants = append(participants, v)
}
}
patchEvent := calendar.Event{
Attendees: participants,
}
if len(participants) == 0 { //overwrite on empty, to delete participant list
existingEvent.Attendees = participants
update := srv.Events.Update("primary", eventID, existingEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
update = update.SendUpdates("all")
}
_, err = update.Do()
} else {
patch := srv.Events.Patch("primary", eventID, &patchEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
patch = patch.SendUpdates("all")
}
_, err = patch.Do()
}
return err
}
//AddParticipants adds users to an existing events participant list
func (e *CalendarConnector) AddParticipants(eventID string, toAdd []string) error {
if eventID == "" {
return ErrorMissingEventID
}
srv, err := e.getCalendarService()
if err != nil {
return err
}
existingEvent, err := e.GetCalendarEvent(eventID)
if err != nil {
return err
}
existingUsersMap := map[string]bool{}
for _, v := range existingEvent.Attendees {
existingUsersMap[v.Email] = true
}
for _, user := range toAdd {
if !existingUsersMap[user] {
existingEvent.Attendees = append(
existingEvent.Attendees,
&calendar.EventAttendee{Email: user},
)
}
}
patchEvent := &calendar.Event{
Attendees: existingEvent.Attendees,
}
patch := srv.Events.Patch("primary", eventID, patchEvent)
if e.informGuestsAboutUpdates != nil && *e.informGuestsAboutUpdates {
patch = patch.SendUpdates("all")
}
_, err = patch.Do()
return err
}
//GetCalendarEvent returns event by id. Returns calendar.Event type event.
func (e *CalendarConnector) GetCalendarEvent(ID string) (*calendar.Event, error) {
config := configs[e.domain]
if config == nil {
return nil, ErrorUnknownDomain
}
srv, err := e.getCalendarService()
if err != nil {
return nil, err
}
get := srv.Events.Get("primary", ID)
return get.Do()
}
//GetEvents returns event by id. Returns calendar.Event type event.
func (e *CalendarConnector) GetEvents(min string, max string, showDeleted bool) (
*calendar.Events,
error,
) {
config := configs[e.domain]
if config == nil {
return nil, ErrorUnknownDomain
}
srv, err := e.getCalendarService()
if err != nil {
return nil, err
}
list := srv.Events.List("primary")
list.SingleEvents(true)
list.ShowDeleted(showDeleted)
if min != "" {
list.TimeMin(min)
}
if max != "" {
list.TimeMax(max)
}
return list.Do()
}