-
Notifications
You must be signed in to change notification settings - Fork 67
/
command.go
228 lines (186 loc) · 6.62 KB
/
command.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 (
"fmt"
"strings"
"github.com/mattermost/mattermost-plugin-zoom/server/zoom"
"github.com/mattermost/mattermost-plugin-api/experimental/command"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/plugin"
"github.com/pkg/errors"
)
const helpText = `* |/zoom start| - Start a zoom meeting`
const oAuthHelpText = `* |/zoom connect| - Connect to zoom
* |/zoom disconnect| - Disconnect from zoom`
const alreadyConnectedString = "Already connected"
func (p *Plugin) getCommand() (*model.Command, error) {
iconData, err := command.GetIconData(p.API, "assets/profile.svg")
if err != nil {
return nil, errors.Wrap(err, "failed to get icon data")
}
return &model.Command{
Trigger: "zoom",
AutoComplete: true,
AutoCompleteDesc: "Available commands: start, disconnect, help",
AutoCompleteHint: "[command]",
AutocompleteData: p.getAutocompleteData(),
AutocompleteIconData: iconData,
}, nil
}
func (p *Plugin) postCommandResponse(args *model.CommandArgs, text string) {
post := &model.Post{
UserId: p.botUserID,
ChannelId: args.ChannelId,
Message: text,
}
_ = p.API.SendEphemeralPost(args.UserId, post)
}
func (p *Plugin) executeCommand(c *plugin.Context, args *model.CommandArgs) (string, error) {
split := strings.Fields(args.Command)
command := split[0]
action := ""
if command != "/zoom" {
return fmt.Sprintf("Command '%s' is not /zoom. Please try again.", command), nil
}
if len(split) > 1 {
action = split[1]
} else {
return "Please specify an action for /zoom command.", nil
}
userID := args.UserId
user, appErr := p.API.GetUser(userID)
if appErr != nil {
return fmt.Sprintf("We could not retrieve user (userId: %v)", args.UserId), nil
}
switch action {
case "connect":
return p.runConnectCommand(user, args)
case "start":
return p.runStartCommand(args, user)
case "disconnect":
return p.runDisconnectCommand(user)
case "help", "":
return p.runHelpCommand(user)
default:
return fmt.Sprintf("Unknown action %v", action), nil
}
}
func (p *Plugin) canConnect(user *model.User) bool {
return p.configuration.EnableOAuth && // we are not on JWT
(!p.configuration.AccountLevelApp || // we are on user managed app
user.IsSystemAdmin()) // admins can connect Account level apps
}
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
msg, err := p.executeCommand(c, args)
if err != nil {
p.API.LogWarn("failed to execute command", "error", err.Error())
}
if msg != "" {
p.postCommandResponse(args, msg)
}
return &model.CommandResponse{}, nil
}
// runStartCommand runs command to start a Zoom meeting.
func (p *Plugin) runStartCommand(args *model.CommandArgs, user *model.User) (string, error) {
if _, appErr := p.API.GetChannelMember(args.ChannelId, user.Id); appErr != nil {
return fmt.Sprintf("We could not get channel members (channelId: %v)", args.ChannelId), nil
}
recentMeeting, recentMeetingLink, creatorName, provider, appErr := p.checkPreviousMessages(args.ChannelId)
if appErr != nil {
return "Error checking previous messages", nil
}
if recentMeeting {
p.postConfirm(recentMeetingLink, args.ChannelId, "", user.Id, creatorName, provider)
return "", nil
}
zoomUser, authErr := p.authenticateAndFetchZoomUser(user)
if authErr != nil {
// the user state will be needed later while connecting the user to Zoom via OAuth
if appErr := p.storeOAuthUserState(user.Id, args.ChannelId, false); appErr != nil {
p.API.LogWarn("failed to store user state")
}
return authErr.Message, authErr.Err
}
if err := p.postMeeting(user, zoomUser.Pmi, args.ChannelId, ""); err != nil {
return "Failed to post message. Please try again.", nil
}
p.trackMeetingStart(args.UserId, telemetryStartSourceCommand)
return "", nil
}
func (p *Plugin) runConnectCommand(user *model.User, extra *model.CommandArgs) (string, error) {
if !p.canConnect(user) {
return "Unknown action `connect`", nil
}
oauthMsg := fmt.Sprintf(
zoom.OAuthPrompt,
*p.API.GetConfig().ServiceSettings.SiteURL)
// OAuth Account Level
if p.configuration.AccountLevelApp {
token, err := p.getSuperuserToken()
if err == nil && token != nil {
return alreadyConnectedString, nil
}
appErr := p.storeOAuthUserState(user.Id, extra.ChannelId, true)
if appErr != nil {
return "", errors.Wrap(appErr, "cannot store state")
}
return oauthMsg, nil
}
// OAuth User Level
_, err := p.fetchOAuthUserInfo(zoomUserByMMID, user.Id)
if err == nil {
return alreadyConnectedString, nil
}
appErr := p.storeOAuthUserState(user.Id, extra.ChannelId, true)
if appErr != nil {
return "", errors.Wrap(appErr, "cannot store state")
}
return oauthMsg, nil
}
// runDisconnectCommand runs command to disconnect from Zoom. Will fail if user cannot connect.
func (p *Plugin) runDisconnectCommand(user *model.User) (string, error) {
if !p.canConnect(user) {
return "Unknown action `disconnect`", nil
}
if p.configuration.AccountLevelApp {
err := p.removeSuperUserToken()
if err != nil {
return "Error disconnecting, " + err.Error(), nil
}
return "Successfully disconnected from Zoom.", nil
}
err := p.disconnectOAuthUser(user.Id)
if err != nil {
return "Could not disconnect OAuth from zoom, " + err.Error(), nil
}
p.trackDisconnect(user.Id)
return "User disconnected from Zoom.", nil
}
// runHelpCommand runs command to display help text.
func (p *Plugin) runHelpCommand(user *model.User) (string, error) {
text := "###### Mattermost Zoom Plugin - Slash Command Help\n"
text += strings.ReplaceAll(helpText, "|", "`")
if p.canConnect(user) {
text += "\n" + strings.ReplaceAll(oAuthHelpText, "|", "`")
}
return text, nil
}
// getAutocompleteData retrieves auto-complete data for the "/zoom" command
func (p *Plugin) getAutocompleteData() *model.AutocompleteData {
available := "start, help"
if p.configuration.EnableOAuth && !p.configuration.AccountLevelApp {
available = "start, connect, disconnect, help"
}
zoom := model.NewAutocompleteData("zoom", "[command]", fmt.Sprintf("Available commands: %s", available))
start := model.NewAutocompleteData("start", "", "Starts a Zoom meeting")
zoom.AddCommand(start)
// no point in showing the 'disconnect' option if OAuth is not enabled
if p.configuration.EnableOAuth && !p.configuration.AccountLevelApp {
connect := model.NewAutocompleteData("connect", "", "Connect to Zoom")
disconnect := model.NewAutocompleteData("disconnect", "", "Disonnects from Zoom")
zoom.AddCommand(connect)
zoom.AddCommand(disconnect)
}
help := model.NewAutocompleteData("help", "", "Display usage")
zoom.AddCommand(help)
return zoom
}