This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-executor.go
197 lines (175 loc) · 5.38 KB
/
command-executor.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
package main
import (
"fmt"
"log"
"strings"
)
// Slack command executor.
type CommandExecutor struct {
settings *Settings
repo *Repository
strava *Strava
poster *ActivitiesPoster
jobsExecutor *JobsExecutor
slack *Slack
addedBots chan *BotDetails
commands []Command
activityTemplateEngine *TemplateEngine
activitiesPoster *ActivitiesPoster
teamsUnlockInfo *TeamsUnlockInfo
}
// Create new command executor.
func NewCommandExecutor(settings *Settings, repo *Repository, strava *Strava, poster *ActivitiesPoster, jobsExecutor *JobsExecutor, slack *Slack, activityTemplateEngine *TemplateEngine, activitiesPoster *ActivitiesPoster, teamsUnlockInfo *TeamsUnlockInfo) *CommandExecutor {
commands := []Command{
&HelpCommand{},
&ClubCommand{},
&UnitCommand{},
//&UnlockCommand{},
&DonateCommand{},
&MessageCommand{},
&RecentCommand{},
//&AdminUnlockCommand{},
&AdminInfoCommand{},
&AdminUsersCommand{},
}
return &CommandExecutor{
settings: settings,
repo: repo,
strava: strava,
poster: poster,
jobsExecutor: jobsExecutor,
slack: slack,
addedBots: make(chan *BotDetails, 100),
commands: commands,
activityTemplateEngine: activityTemplateEngine,
activitiesPoster: activitiesPoster,
teamsUnlockInfo: teamsUnlockInfo,
}
}
// Run command executor.
func (e *CommandExecutor) Run() {
bots := e.loadExistingBots()
for {
select {
case bot := <-e.addedBots:
bots[bot.TeamId] = bot
case message := <-e.slack.IncomingMessages():
if bot, ok := bots[message.TeamId]; ok {
response, attachmentName, attachmentContent := e.processCommand(bot.BotId, message)
if response != "" || attachmentName != "" {
e.slack.PostMessage(&OutgoingSlackMessage{message.ChannelId, bot.BotAccessToken, response, attachmentName, attachmentContent})
}
}
}
}
}
// Load existing bot details from the database.
func (e *CommandExecutor) loadExistingBots() map[string]*BotDetails {
bots := make(map[string]*BotDetails)
existingBots, err := e.repo.BotDetails.List()
if err != nil {
log.Printf("Can't load bot details from the database: %v\n", err)
}
for _, bot := range existingBots {
bots[bot.TeamId] = bot
}
return bots
}
// Add new bot details.
func (e *CommandExecutor) AddBot(bot *BotDetails) {
e.addedBots <- bot
}
// Process bot command.
func (e *CommandExecutor) processCommand(botId string, message *IncomingSlackMessage) (response, attachmentName string, attachmentContent []byte) {
messageText := strings.TrimSpace(message.Text)
mention := fmt.Sprintf("<@%s>", botId)
commandText := ""
if messageText == mention {
commandText = "help"
} else {
mention += " "
if strings.HasPrefix(messageText, mention) {
commandText = strings.TrimSpace(strings.TrimPrefix(messageText, mention))
}
}
if commandText == "" {
return "", "", nil
}
if strings.HasPrefix(commandText, "uploaded a file: ") {
return "", "", nil
}
command, params := e.getCommand(commandText)
if command == nil {
return "Unknown command", "", nil
}
commandWithAttachment, ok := command.(CommandWithAttachment)
if ok {
response, attachmentName, attachmentContent, err := commandWithAttachment.ExecuteWithAttachment(params, message, e)
if err != nil {
return "Something is wrong", "", nil
}
return response, attachmentName, attachmentContent
}
response, err := command.Execute(params, message, e)
if err != nil {
log.Printf("Error when executing %s: %v\n", commandText, err)
return "Something is wrong", "", nil
}
return response, "", nil
}
func (e *CommandExecutor) getCommand(commandText string) (Command, []string) {
for _, command := range e.commands {
commandName := strings.ToLower(command.Name())
if strings.ToLower(commandText) == commandName || strings.HasPrefix(strings.ToLower(commandText), fmt.Sprintf("%s ", commandName)) {
return command, parseParams(commandText[len(commandName):])
}
}
return nil, nil
}
func (e *CommandExecutor) unlockUser(stravaUserId int, unlockCode string, teamId string) error {
userDetails, err := e.repo.UserDetails.Get(stravaUserId)
if err != nil {
return err
}
if userDetails != nil && userDetails.UnlockCode != "" {
return nil
}
if userDetails == nil {
userDetails := &UserDetails{StravaUserId: stravaUserId, UnlockCode: unlockCode}
err = e.repo.UserDetails.Create(userDetails)
if err != nil {
return err
}
} else {
err = e.repo.UserDetails.Update(userDetails, map[string]interface{}{"UnlockCode": unlockCode})
if err != nil {
return err
}
}
e.teamsUnlockInfo.AddUnlockUser(teamId)
return nil
}
func (e *CommandExecutor) checkFromAdmin(message *IncomingSlackMessage) (string, error) {
accessDetails, err := e.repo.AccessDetails.GetForUser(message.TeamId, message.UserId)
if err != nil {
return "", err
}
if accessDetails == nil {
return COMMAND_STRAVA_NOT_CONNECTED, nil
}
if !e.settings.IsAdmin(accessDetails.StravaUserId) {
return COMMAND_SHOULD_BE_ADMIN, nil
}
return "", nil
}
func parseParams(paramsStr string) []string {
parts := strings.Split(paramsStr, " ")
params := make([]string, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if len(part) > 0 {
params = append(params, part)
}
}
return params
}