-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configEditor_global.go
245 lines (220 loc) · 6.66 KB
/
configEditor_global.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
package main
import (
"encoding/json"
"net/http"
"regexp"
"time"
log "github.com/sirupsen/logrus"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
const frontendNotifyTypeReload = "configReload"
var frontendNotifyHooks = newHooker()
//nolint:funlen // Just contains a collection of objects
func registerEditorGlobalMethods() {
for _, rd := range []plugins.HTTPRouteRegistrationArgs{
{
Description: "Returns the documentation for available actions",
HandlerFunc: configEditorGlobalGetActions,
Method: http.MethodGet,
Module: moduleConfigEditor,
Name: "Get available actions",
Path: "/actions",
ResponseType: plugins.HTTPRouteResponseTypeJSON,
},
{
Description: "Returns all available modules for auth",
HandlerFunc: configEditorGlobalGetModules,
Method: http.MethodGet,
Module: moduleConfigEditor,
Name: "Get available modules",
Path: "/modules",
ResponseType: plugins.HTTPRouteResponseTypeJSON,
},
{
Description: "Returns information about a Twitch user to properly display bot editors",
HandlerFunc: configEditorGlobalGetUser,
Method: http.MethodGet,
Module: moduleConfigEditor,
Name: "Get user information",
Path: "/user",
QueryParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "The user to query the information for",
Name: "user",
Required: true,
Type: "string",
},
},
RequiresEditorsAuth: true,
ResponseType: plugins.HTTPRouteResponseTypeJSON,
},
{
Description: "Subscribe for configuration changes",
HandlerFunc: configEditorGlobalSubscribe,
Method: http.MethodGet,
Module: moduleConfigEditor,
Name: "Websocket: Subscribe config changes",
Path: "/notify-config",
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
},
{
Description: "Validate a cron expression and return the next executions",
HandlerFunc: configEditorGlobalValidateCron,
Method: http.MethodPut,
Module: moduleConfigEditor,
Name: "Validate cron expression",
Path: "/validate-cron",
QueryParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "The cron expression to test",
Name: "cron",
Required: true,
Type: "string",
},
{
Description: "Check cron with last execution of auto-message",
Name: "uuid",
Required: false,
Type: "string",
},
},
ResponseType: plugins.HTTPRouteResponseTypeJSON,
},
{
Description: "Validate a regular expression against the RE2 regex parser",
HandlerFunc: configEditorGlobalValidateRegex,
Method: http.MethodPut,
Module: moduleConfigEditor,
Name: "Validate regular expression",
Path: "/validate-regex",
QueryParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "The regular expression to test",
Name: "regexp",
Required: true,
Type: "string",
},
},
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
},
{
Description: "Validate a template expression against the built in template function library",
HandlerFunc: configEditorGlobalValidateTemplate,
Method: http.MethodPut,
Module: moduleConfigEditor,
Name: "Validate template expression",
Path: "/validate-template",
QueryParams: []plugins.HTTPRouteParamDocumentation{
{
Description: "The template expression to test",
Name: "template",
Required: true,
Type: "string",
},
},
ResponseType: plugins.HTTPRouteResponseTypeTextPlain,
},
} {
if err := registerRoute(rd); err != nil {
log.WithError(err).Fatal("Unable to register config editor route")
}
}
}
func configEditorGlobalGetActions(w http.ResponseWriter, _ *http.Request) {
availableActorDocsLock.Lock()
defer availableActorDocsLock.Unlock()
if err := json.NewEncoder(w).Encode(availableActorDocs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func configEditorGlobalGetModules(w http.ResponseWriter, _ *http.Request) {
if err := json.NewEncoder(w).Encode(knownModules); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func configEditorGlobalGetUser(w http.ResponseWriter, r *http.Request) {
usr, err := twitchClient.GetUserInformation(r.Context(), r.FormValue("user"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(usr); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func configEditorGlobalSubscribe(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.WithError(err).Error("Unable to initialize websocket")
return
}
defer conn.Close() //nolint:errcheck
var (
frontendNotify = make(chan string, 1)
pingTimer = time.NewTicker(websocketPingInterval)
unsubscribe = frontendNotifyHooks.Register(func(payload any) { frontendNotify <- payload.(string) })
)
defer unsubscribe()
type socketMsg struct {
MsgType string `json:"msg_type"`
}
for {
select {
case msgType := <-frontendNotify:
if err := conn.WriteJSON(socketMsg{
MsgType: msgType,
}); err != nil {
log.WithError(err).Debug("Unable to send websocket notification")
return
}
case <-pingTimer.C:
if err := conn.WriteJSON(socketMsg{
MsgType: "ping",
}); err != nil {
log.WithError(err).Debug("Unable to send websocket ping")
return
}
}
}
}
func configEditorGlobalValidateCron(w http.ResponseWriter, r *http.Request) {
sched, err := cronParser.Parse(r.FormValue("cron"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var (
lt = time.Now()
out []time.Time
)
if id := r.FormValue("uuid"); id != "" {
for _, a := range config.AutoMessages {
if a.ID() != id {
continue
}
lt = a.lastMessageSent
break
}
}
for i := 0; i < 3; i++ {
lt = sched.Next(lt)
out = append(out, lt)
}
if err := json.NewEncoder(w).Encode(out); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func configEditorGlobalValidateRegex(w http.ResponseWriter, r *http.Request) {
if _, err := regexp.Compile(r.FormValue("regexp")); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}
func configEditorGlobalValidateTemplate(w http.ResponseWriter, r *http.Request) {
if err := validateTemplate(r.FormValue("template")); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusNoContent)
}