-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplugin.go
476 lines (401 loc) · 12.6 KB
/
plugin.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/robfig/cron"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
)
const (
userTokenKey = "_usertoken"
calendarTokenKey = "_calendartoken"
CalendarIconURL = "plugins/google-calendar/Google_Calendar_Logo.png"
BotUsername = "Calendar Plugin"
postPretext = "Event starting in 10 min"
welcomeMessage = "Welcome to Google Calendar Plugin"
)
type Plugin struct {
plugin.MattermostPlugin
// configurationLock synchronizes access to the configuration.
configurationLock sync.RWMutex
// configuration is the active plugin configuration. Consult getConfiguration and
// setConfiguration for usage.
configuration *configuration
BotUserID string
}
// UserInfo captures the UserID and authentication token of a user.
type UserInfo struct {
UserID string
Token *oauth2.Token
ChannelID string
}
// CalendarInfo captures the list of events and details of the last event update.
type CalendarInfo struct {
LastEventUpdate string
Events []EventInfo
CalendarWatchToken string
CalendarWatchExpiry int64
}
// EventInfo captures some of the attributes of a Calendar event.
type EventInfo struct {
Id string
HtmlLink string
StartTime string
EndTime string
Summary string
Status string
}
// OnActivate is triggered as soon as the plugin is enabled.
func (p *Plugin) OnActivate() error {
config := p.getConfiguration()
if err := config.IsValid(); err != nil {
return err
}
p.API.RegisterCommand(getCommand())
user, err := p.API.GetUserByUsername(config.Username)
if err != nil {
return fmt.Errorf("Unable to find user with configured username: %v", config.Username)
}
p.BotUserID = user.Id
return nil
}
func (p *Plugin) getOAuthConfig() *oauth2.Config {
pluginConfig := p.getConfiguration()
config := p.API.GetConfig()
return &oauth2.Config{
ClientID: pluginConfig.CalendarOAuthClientID,
ClientSecret: pluginConfig.CalendarOAuthClientSecret,
RedirectURL: fmt.Sprintf("%s/plugins/google-calendar/oauth/complete", *config.ServiceSettings.SiteURL),
Scopes: []string{"https://www.googleapis.com/auth/calendar.readonly", "https://www.googleapis.com/auth/calendar.events.readonly"},
Endpoint: google.Endpoint,
}
}
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
split := strings.Fields(args.Command)
command := split[0]
action := ""
if len(split) > 1 {
action = split[1]
}
if command != "/google-calendar" {
return &model.CommandResponse{}, nil
}
if action == "connect" {
config := p.API.GetConfig()
if config.ServiceSettings.SiteURL == nil {
return getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Encountered an error connecting to Google Calendar."), nil
}
resp := getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("[Click here to link your Google Calendar.](%s/plugins/google-calendar/oauth/connect)", *config.ServiceSettings.SiteURL))
return resp, nil
}
return &model.CommandResponse{}, nil
}
func (p *Plugin) createBotDMPost(userInfo *UserInfo) *model.AppError {
post := &model.Post{
UserId: p.BotUserID,
ChannelId: userInfo.ChannelID,
Message: welcomeMessage,
Type: "custom_git_welcome",
Props: map[string]interface{}{
"from_webhook": "true",
"override_username": BotUsername,
"override_icon_url": CalendarIconURL,
},
}
if _, err := p.API.CreatePost(post); err != nil {
mlog.Error("Error while creating bot welcome post" + err.Error())
return err
}
return nil
}
func (p *Plugin) getDirectChannel(userInfo *UserInfo) (string, error) {
channel, err := p.API.GetDirectChannel(userInfo.UserID, p.BotUserID)
if err != nil {
mlog.Error("Couldn't get bot's DM channel", mlog.String("user_id", userInfo.UserID))
return "", err
}
userInfo.ChannelID = channel.Id
return channel.Id, nil
}
func (p *Plugin) createAPostForEvent(userID string, e EventInfo) error {
event := generateSlackAttachment(e)
userInfo, err := p.getUserInfo(userID)
if err != nil {
mlog.Error("Error fetching user details" + err.Error())
return err
}
p.API.CreatePost(&model.Post{
ChannelId: userInfo.ChannelID,
Type: model.POST_SLACK_ATTACHMENT,
UserId: p.BotUserID,
Props: map[string]interface{}{
"from_webhook": "true",
"use_user_icon": "true",
"attachments": []*model.SlackAttachment{event},
},
})
return nil
}
// createCalendarService initialises and returns a Google Calendar service
func (p *Plugin) createCalendarService(u *UserInfo) (*calendar.Service, error) {
googleOauthConfig := p.getOAuthConfig()
tokenSource := googleOauthConfig.TokenSource(context.TODO(), u.Token)
newToken, err := tokenSource.Token()
if err != nil {
mlog.Error("Error fetching token from token source" + err.Error())
return nil, err
}
if newToken.AccessToken != u.Token.AccessToken {
u.Token = newToken
err := p.storeUserInfo(u)
if err != nil {
mlog.Error("Error storing the new access token " + err.Error())
return nil, err
}
}
client := oauth2.NewClient(context.TODO(), tokenSource)
calendarService, err := calendar.New(client)
if err != nil {
return nil, err
}
return calendarService, nil
}
func (p *Plugin) subscribeToCalendar(u *UserInfo) {
p.processEventsFromCalendar(u)
p.setupCalendarWatchService(u)
cron := cron.New()
cron.AddFunc("@every 1m", func() {
// checkEvents method gets called every minute to check if the
// start time of any event is after 10 min.
p.checkEvents(u.UserID)
})
cron.Start()
}
func (p *Plugin) setupCalendarWatchService(u *UserInfo) error {
calendarInfo, calendarInfoErr := p.getCalendarInfo(u.UserID)
if calendarInfoErr != nil {
return calendarInfoErr
}
calendarService, calendarServiceErr := p.createCalendarService(u)
if calendarServiceErr != nil {
return calendarServiceErr
}
config := p.API.GetConfig()
uuid := uuid.New().String()
eventsWatchCall := calendarService.Events.Watch("primary", &calendar.Channel{
Address: fmt.Sprintf("%s/plugins/google-calendar/watch?userID=%s", *config.ServiceSettings.SiteURL, u.UserID),
Id: uuid,
Type: "web_hook",
})
channel, err := eventsWatchCall.Do()
if err != nil {
return err
}
calendarInfo.CalendarWatchToken = uuid
calendarInfo.CalendarWatchExpiry = channel.Expiration
p.storeCalendarInfo(u.UserID, calendarInfo)
return nil
}
func (p *Plugin) setupWatchRenewal(userID string) error {
calendarInfo, calendarInfoErr := p.getCalendarInfo(userID)
if calendarInfoErr != nil {
return calendarInfoErr
}
userInfo, userInfoErr := p.getUserInfo(userID)
if userInfoErr != nil {
return userInfoErr
}
diff := calendarInfo.CalendarWatchExpiry - (time.Now().UnixNano()/int64(time.Millisecond) + 60000)
if diff <= 60000 {
time.AfterFunc(time.Millisecond*time.Duration(diff), func() {
p.setupCalendarWatchService(userInfo)
})
}
return nil
}
// checkIfTheEventAlreadyExists checks if event with given eventID already exists
func (p *Plugin) checkIfTheEventAlreadyExists(eventID, userID string) bool {
calendarInfo, _ := p.getCalendarInfo(userID)
for _, event := range calendarInfo.Events {
if event.Id == eventID {
return true
}
}
return false
}
func (p *Plugin) fetchEventsFromCalendar(u *UserInfo) (*calendar.Events, error) {
calendarService, _ := p.createCalendarService(u)
var calendarInfo *CalendarInfo
var calendarEvents *calendar.Events
var err error
if info, _ := p.getCalendarInfo(u.UserID); info != nil {
calendarInfo = info
}
if calendarInfo.LastEventUpdate != "" {
calendarEvents, err = calendarService.Events.List("primary").UpdatedMin(calendarInfo.LastEventUpdate).TimeMax(time.Now().Add(time.Hour * 1).Format(time.RFC3339)).Do()
} else {
calendarEvents, err = calendarService.Events.List("primary").TimeMin(time.Now().Format(time.RFC3339)).TimeMax(time.Now().Add(time.Hour * 1).Format(time.RFC3339)).Do()
}
if err != nil {
return nil, err
}
return calendarEvents, nil
}
func (p *Plugin) processEventsFromCalendar(u *UserInfo) error {
var calendarInfo *CalendarInfo
info, err := p.getCalendarInfo(u.UserID)
if info != nil {
calendarInfo = info
}
if err != nil {
return err
}
calendarEvents, err := p.fetchEventsFromCalendar(u)
if err != nil {
return err
}
if len(calendarEvents.Items) > 0 {
for _, event := range calendarEvents.Items {
calendarInfo.Events = append(calendarInfo.Events, EventInfo{
Id: event.Id,
HtmlLink: event.HtmlLink,
StartTime: formatTime(event.Start.DateTime),
EndTime: formatTime(event.End.DateTime),
Summary: event.Summary,
Status: event.Status,
})
}
calendarInfo.LastEventUpdate = time.Now().Format(time.RFC3339)
p.storeCalendarInfo(u.UserID, calendarInfo)
}
return nil
}
func (p *Plugin) updateCalendarEvents(u *UserInfo, calendarInfo *CalendarInfo) error {
calendarEvents, err := p.fetchEventsFromCalendar(u)
if err != nil {
return err
}
for _, event := range calendarEvents.Items {
if event.Status == "cancelled" {
calendarInfo, _ = p.removeAnEvent(u.UserID, event)
} else if p.checkIfTheEventAlreadyExists(event.Id, u.UserID) == true {
e := EventInfo{
Id: event.Id,
HtmlLink: event.HtmlLink,
StartTime: formatTime(event.Start.DateTime),
EndTime: formatTime(event.End.DateTime),
Summary: event.Summary,
Status: event.Status,
}
calendarInfo, _ = p.updateEvent(event.Id, u.UserID, e)
} else {
calendarInfo.Events = append(calendarInfo.Events, EventInfo{
Id: event.Id,
HtmlLink: event.HtmlLink,
StartTime: formatTime(event.Start.DateTime),
EndTime: formatTime(event.End.DateTime),
Summary: event.Summary,
Status: event.Status,
})
}
calendarInfo.LastEventUpdate = time.Now().Format(time.RFC3339)
p.storeCalendarInfo(u.UserID, calendarInfo)
}
return nil
}
// checkEvents checks if there is any event 10 min after the current time.
// If there is an event, if triggers a post for it.
func (p *Plugin) checkEvents(userID string) error {
calendarInfo, err := p.getCalendarInfo(userID)
if err != nil {
return err
}
for _, e := range calendarInfo.Events {
afterTenMinutes := time.Now().Add(time.Minute * 10).Format("3:04PM")
if afterTenMinutes == e.StartTime {
_ = p.createAPostForEvent(userID, e)
}
}
p.setupWatchRenewal(userID)
return nil
}
func (p *Plugin) storeUserInfo(userInfo *UserInfo) error {
jsonInfo, err := json.Marshal(userInfo)
if err != nil {
return err
}
if err := p.API.KVSet(userInfo.UserID+userTokenKey, jsonInfo); err != nil {
return err
}
return nil
}
func (p *Plugin) getUserInfo(userID string) (*UserInfo, error) {
var userInfo UserInfo
if info, err := p.API.KVGet(userID + userTokenKey); err != nil || info == nil {
return nil, err
} else if err := json.Unmarshal(info, &userInfo); err != nil {
return nil, err
}
return &userInfo, nil
}
func (p *Plugin) storeCalendarInfo(userID string, calendarInfo *CalendarInfo) error {
jsonInfo, err := json.Marshal(calendarInfo)
if err != nil {
return err
}
if err := p.API.KVSet(userID+calendarTokenKey, jsonInfo); err != nil {
return err
}
return nil
}
func (p *Plugin) getCalendarInfo(userID string) (*CalendarInfo, error) {
var calendarInfo CalendarInfo
if info, err := p.API.KVGet(userID + calendarTokenKey); err != nil || info == nil {
return nil, err
} else if err := json.Unmarshal(info, &calendarInfo); err != nil {
return nil, err
}
return &calendarInfo, nil
}
func (p *Plugin) updateEvent(eventID, userID string, updatedEvent EventInfo) (*CalendarInfo, error) {
calendarInfo, err := p.getCalendarInfo(userID)
if err != nil {
return nil, err
}
for index := range calendarInfo.Events {
event := &calendarInfo.Events[index]
if event.Id == eventID {
event.StartTime = updatedEvent.StartTime
event.EndTime = updatedEvent.EndTime
event.Status = updatedEvent.Status
event.HtmlLink = updatedEvent.HtmlLink
event.Id = updatedEvent.Id
event.Summary = updatedEvent.Summary
break
}
}
return calendarInfo, nil
}
func (p *Plugin) removeAnEvent(userID string, e *calendar.Event) (*CalendarInfo, error) {
calendarInfo, err := p.getCalendarInfo(userID)
if err != nil {
return nil, errors.New("Failed to get calendar information")
}
for index, event := range calendarInfo.Events {
if event.Id == e.Id {
calendarInfo.Events = append(calendarInfo.Events[:index], calendarInfo.Events[index+1:]...)
break
}
}
return calendarInfo, nil
}