generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 31
/
api.go
228 lines (195 loc) · 6.27 KB
/
api.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/mattermost/mattermost-server/model"
"golang.org/x/oauth2"
"google.golang.org/api/calendar/v3"
"github.com/mattermost/mattermost-server/plugin"
)
// ServeHTTP allows the plugin to implement the http.Handler interface. Requests destined for the
// /plugins/{id} path will be routed to the plugin.
//
// The Mattermost-User-Id header will be present if (and only if) the request is by an
// authenticated user.
//
// This demo implementation sends back whether or not the plugin hooks are currently enabled. It
// is used by the web app to recover from a network reconnection and synchronize the state of the
// plugin's hooks.
func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch path := r.URL.Path; path {
case "/oauth/connect":
p.connectCalendar(w, r)
case "/oauth/complete":
p.completeCalendar(w, r)
case "/delete":
p.deleteEvent(w, r)
case "/handleresponse":
p.handleEventResponse(w, r)
case "/watch":
p.watchCalendar(w, r)
default:
http.NotFound(w, r)
}
}
func (p *Plugin) connectCalendar(w http.ResponseWriter, r *http.Request) {
authedUserId := r.Header.Get("Mattermost-User-ID")
if authedUserId == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
state := fmt.Sprintf("%v_%v", model.NewId()[10], authedUserId)
if err := p.API.KVSet(state, []byte(state)); err != nil {
http.Error(w, "Failed to save state", http.StatusBadRequest)
return
}
calendarConfig := p.CalendarConfig()
url := calendarConfig.AuthCodeURL(state, oauth2.AccessTypeOffline, oauth2.ApprovalForce)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
func (p *Plugin) completeCalendar(w http.ResponseWriter, r *http.Request) {
html := `
<!DOCTYPE html>
<html>
<head>
<script>
window.close();
</script>
</head>
<body>
<p>Completed connecting to Google Calendar. Please close this window.</p>
</body>
</html>
`
authedUserId := r.Header.Get("Mattermost-User-ID")
state := r.FormValue("state")
code := r.FormValue("code")
userId := strings.Split(state, "_")[1]
config := p.CalendarConfig()
if authedUserId == "" || userId != authedUserId {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}
if storedState, err := p.API.KVGet(state); err != nil {
http.Error(w, "Missing stored state", http.StatusBadRequest)
return
} else if string(storedState) != state {
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
if err := p.API.KVDelete(state); err != nil {
http.Error(w, "Error deleting state", http.StatusBadRequest)
return
}
token, err := config.Exchange(context.Background(), string(code))
if err != nil {
http.Error(w, "Error setting up Config Exchange", http.StatusBadRequest)
return
}
tokenJSON, err := json.Marshal(token)
if err != nil {
http.Error(w, "Invalid token marshal in completeCalendar", http.StatusBadRequest)
return
}
p.API.KVSet(userId+"calendarToken", tokenJSON)
p.CalendarSync(userId)
if err = p.setupCalendarWatch(userId); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
p.startCronJob(authedUserId)
// Post intro post
message := "#### Welcome to the Mattermost Google Calendar Plugin!\n" +
"You've successfully connected your Mattermost account to your Google Calendar.\n" +
"Please type **/calendar help** to understand how to user this plugin. "
p.CreateBotDMPost(userId, message)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, html)
}
func (p *Plugin) deleteEvent(w http.ResponseWriter, r *http.Request) {
html := `
<!DOCTYPE html>
<html>
<head>
<script>
window.close();
</script>
</head>
</html>
`
userID := r.Header.Get("Mattermost-User-ID")
eventID := r.URL.Query().Get("evtid")
calendarID := p.getPrimaryCalendarID(userID)
srv, err := p.getCalendarService(userID)
if err != nil {
p.CreateBotDMPost(userID, fmt.Sprintf("Unable to delete event. Error: %s", err))
return
}
eventToBeDeleted, _ := srv.Events.Get(calendarID, eventID).Do()
err = srv.Events.Delete(calendarID, eventID).Do()
if err != nil {
p.CreateBotDMPost(userID, fmt.Sprintf("Unable to delete event. Error: %s", err.Error()))
return
}
p.CreateBotDMPost(userID, fmt.Sprintf("Success! Event _%s_ has been deleted.", eventToBeDeleted.Summary))
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, html)
}
func (p *Plugin) handleEventResponse(w http.ResponseWriter, r *http.Request) {
html := `
<!DOCTYPE html>
<html>
<head>
<script>
window.close();
</script>
</head>
</html>
`
userID := r.Header.Get("Mattermost-User-ID")
response := r.URL.Query().Get("response")
eventID := r.URL.Query().Get("evtid")
calendarID := p.getPrimaryCalendarID(userID)
srv, _ := p.getCalendarService(userID)
eventToBeUpdated, err := srv.Events.Get(calendarID, eventID).Do()
if err != nil {
p.CreateBotDMPost(userID, fmt.Sprintf("Error! Failed to update the response of _%s_ event.", eventToBeUpdated.Summary))
return
}
for idx, attendee := range eventToBeUpdated.Attendees {
if attendee.Self {
eventToBeUpdated.Attendees[idx].ResponseStatus = response
}
}
event, err := srv.Events.Update(calendarID, eventID, eventToBeUpdated).Do()
if err != nil {
p.CreateBotDMPost(userID, fmt.Sprintf("Error! Failed to update the response of _%s_ event.", event.Summary))
} else {
p.CreateBotDMPost(userID, fmt.Sprintf("Success! Event _%s_ response has been updated.", event.Summary))
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
fmt.Fprint(w, html)
}
func (p *Plugin) watchCalendar(w http.ResponseWriter, r *http.Request) {
userID := r.URL.Query().Get("userId")
channelID := r.Header.Get("X-Goog-Channel-ID")
resourceID := r.Header.Get("X-Goog-Resource-ID")
state := r.Header.Get("X-Goog-Resource-State")
watchToken, _ := p.API.KVGet(userID + "watchToken")
channelByte, _ := p.API.KVGet(userID + "watchChannel")
var channel calendar.Channel
json.Unmarshal(channelByte, &channel)
if string(watchToken) == channelID && state == "exists" {
p.CalendarSync(userID)
} else {
srv, _ := p.getCalendarService(userID)
srv.Channels.Stop(&calendar.Channel{
Id: channelID,
ResourceId: resourceID,
})
}
}