-
Notifications
You must be signed in to change notification settings - Fork 921
/
web.go
396 lines (315 loc) · 11.8 KB
/
web.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
package rolecommands
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/rolecommands/models"
"github.com/jonas747/yagpdb/web"
"github.com/pkg/errors"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/boil"
"github.com/volatiletech/sqlboiler/queries/qm"
"goji.io"
"goji.io/pat"
"html/template"
"net/http"
"sort"
"strconv"
"strings"
)
type FormCommand struct {
ID int64
Name string `valid:",1,100,trimspace"`
Role int64 `valid:"role,false"`
Group int64
RequireRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
}
type FormGroup struct {
ID int64
Name string `valid:",1,100,trimspace"`
RequireRoles []int64 `valid:"role,true"`
IgnoreRoles []int64 `valid:"role,true"`
Mode int
MultipleMax int
MultipleMin int
SingleAutoToggleOff bool
SingleRequireOne bool
}
func (p *Plugin) InitWeb() {
tmplPathSettings := "templates/plugins/rolecommands.html"
if common.Testing {
tmplPathSettings = "../../rolecommands/assets/rolecommands.html"
}
web.Templates = template.Must(web.Templates.ParseFiles(tmplPathSettings))
// Setup SubMuxer
subMux := goji.SubMux()
web.CPMux.Handle(pat.New("/rolecommands/*"), subMux)
web.CPMux.Handle(pat.New("/rolecommands"), subMux)
subMux.Use(web.RequireFullGuildMW)
subMux.Use(web.RequireBotMemberMW)
subMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles))
// Setup routes
getIndexHandler := web.ControllerHandler(HandleGetIndex, "cp_rolecommands")
getGroupHandler := web.ControllerHandler(func(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
groupIDRaw := pat.Param(r, "groupID")
parsed, _ := strconv.ParseInt(groupIDRaw, 10, 64)
return HandleGetGroup(parsed, w, r)
}, "cp_rolecommands")
subMux.Handle(pat.Get("/"), getIndexHandler)
subMux.Handle(pat.Get("/group/:groupID"), getGroupHandler)
// either serve the group page or the index page, kinda convoluted but eh
getIndexpPostHandler := web.ControllerHandler(func(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
if r.FormValue("GroupID") != "" {
parsed, _ := strconv.ParseInt(r.FormValue("GroupID"), 10, 64)
return HandleGetGroup(parsed, w, r)
}
if r.FormValue("Group") != "" {
parsed, _ := strconv.ParseInt(r.FormValue("Group"), 10, 64)
return HandleGetGroup(parsed, w, r)
}
_, tmpl = web.GetBaseCPContextData(r.Context())
if idInterface, ok := tmpl["GroupID"]; ok {
return HandleGetGroup(idInterface.(int64), w, r)
}
return HandleGetIndex(w, r)
}, "cp_rolecommands")
subMux.Handle(pat.Post("/new_cmd"), web.ControllerPostHandler(HandleNewCommand, getIndexpPostHandler, FormCommand{}, "Added a new role command"))
subMux.Handle(pat.Post("/update_cmd"), web.ControllerPostHandler(HandleUpdateCommand, getIndexpPostHandler, FormCommand{}, "Updated a role command"))
subMux.Handle(pat.Post("/remove_cmd"), web.ControllerPostHandler(HandleRemoveCommand, getIndexpPostHandler, nil, "Removed a role command"))
subMux.Handle(pat.Post("/move_cmd"), web.ControllerPostHandler(HandleMoveCommand, getIndexpPostHandler, nil, "Moved a role command"))
subMux.Handle(pat.Post("/new_group"), web.ControllerPostHandler(HandleNewGroup, getIndexpPostHandler, FormGroup{}, "Added a new role command group"))
subMux.Handle(pat.Post("/update_group"), web.ControllerPostHandler(HandleUpdateGroup, getIndexpPostHandler, FormGroup{}, "Updated a role command group"))
subMux.Handle(pat.Post("/remove_group"), web.ControllerPostHandler(HandleRemoveGroup, getIndexpPostHandler, nil, "Removed a role command group"))
}
func HandleGetIndex(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
ungroupedCommands, err := models.RoleCommands(qm.Where("guild_id = ?", g.ID), qm.Where("role_group_id is null")).AllG(r.Context())
if err != nil {
return tmpl, err
}
sort.Slice(ungroupedCommands, RoleCommandsLessFunc(ungroupedCommands))
tmpl["LoneCommands"] = ungroupedCommands
groups, err := models.RoleGroups(qm.Where(models.RoleGroupColumns.GuildID+" = ?", g.ID), qm.OrderBy("id asc")).AllG(r.Context())
if err != nil {
return tmpl, err
}
tmpl["Groups"] = groups
return tmpl, nil
}
func HandleGetGroup(groupID int64, w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
groups, err := models.RoleGroups(qm.Where(models.RoleGroupColumns.GuildID+" = ?", g.ID), qm.OrderBy("id asc")).AllG(r.Context())
if err != nil {
return tmpl, err
}
tmpl["Groups"] = groups
var currentGroup *models.RoleGroup
for _, v := range groups {
if v.ID == groupID {
tmpl["CurrentGroup"] = v
currentGroup = v
break
}
}
if currentGroup != nil {
commands, err := currentGroup.RoleCommands().AllG(r.Context())
if err != nil {
return tmpl, err
}
sort.Slice(commands, RoleCommandsLessFunc(commands))
tmpl["Commands"] = commands
} else {
// Fallback
return HandleGetIndex(w, r)
}
return tmpl, nil
}
func HandleNewCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
form := r.Context().Value(common.ContextKeyParsedForm).(*FormCommand)
form.Name = strings.TrimSpace(form.Name)
if c, _ := models.RoleCommands(qm.Where(models.RoleCommandColumns.GuildID+"=?", g.ID)).CountG(r.Context()); c >= 1000 {
tmpl.AddAlerts(web.ErrorAlert("Max 1000 role commands allowed"))
return tmpl, nil
}
if c, _ := models.RoleCommands(qm.Where(models.RoleCommandColumns.GuildID+"=?", g.ID), qm.Where(models.RoleCommandColumns.Name+" ILIKE ?", form.Name)).CountG(r.Context()); c > 0 {
tmpl.AddAlerts(web.ErrorAlert("Already a role command with that name"))
return tmpl, nil
}
model := &models.RoleCommand{
Name: form.Name,
GuildID: g.ID,
Role: form.Role,
RequireRoles: form.RequireRoles,
IgnoreRoles: form.IgnoreRoles,
}
if form.Group != -1 {
group, err := models.RoleGroups(qm.Where(models.RoleGroupColumns.GuildID+"=?", g.ID), qm.Where(models.RoleGroupColumns.ID+"=?", form.Group)).OneG(r.Context())
if err != nil {
return tmpl, err
}
model.RoleGroupID = null.Int64From(group.ID)
}
err := model.InsertG(r.Context(), boil.Infer())
return tmpl, err
}
func HandleUpdateCommand(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
formCmd := r.Context().Value(common.ContextKeyParsedForm).(*FormCommand)
cmd, err := models.FindRoleCommandG(r.Context(), formCmd.ID)
if err != nil {
return
}
if cmd.GuildID != g.ID {
return tmpl.AddAlerts(web.ErrorAlert("That's not your command")), nil
}
cmd.Name = formCmd.Name
cmd.Role = formCmd.Role
cmd.IgnoreRoles = formCmd.IgnoreRoles
cmd.RequireRoles = formCmd.RequireRoles
if formCmd.Group != -1 {
group, err := models.FindRoleGroupG(r.Context(), formCmd.Group)
if err != nil {
return tmpl, err
}
if group.GuildID != g.ID {
return tmpl.AddAlerts(web.ErrorAlert("That's not your group")), nil
}
err = cmd.SetRoleGroupG(r.Context(), false, group)
if err != nil {
return tmpl, err
}
} else {
cmd.RoleGroupID.Valid = false
if _, err = cmd.UpdateG(r.Context(), boil.Whitelist(models.RoleCommandColumns.RoleGroupID)); err != nil {
cmd.RoleGroupID.Valid = true
return tmpl, err
}
}
_, err = cmd.UpdateG(r.Context(), boil.Whitelist(models.RoleCommandColumns.Name, models.RoleCommandColumns.Role, models.RoleCommandColumns.IgnoreRoles, models.RoleCommandColumns.RequireRoles))
return
}
func HandleMoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
commands, err := models.RoleCommands(qm.Where("guild_id=?", g.ID)).AllG(r.Context())
if err != nil {
return tmpl, err
}
tID, err := strconv.ParseInt(r.FormValue("ID"), 10, 32)
if err != nil {
return tmpl, err
}
var targetCmd *models.RoleCommand
for _, v := range commands {
if v.ID == tID {
targetCmd = v
break
}
}
if targetCmd == nil {
return tmpl, errors.New("RoleCommand not found")
}
commandsInGroup := make([]*models.RoleCommand, 0, len(commands))
// Sort all relevant commands
for _, v := range commands {
if (!targetCmd.RoleGroupID.Valid && !v.RoleGroupID.Valid) || (targetCmd.RoleGroupID.Valid && v.RoleGroupID.Valid && targetCmd.RoleGroupID.Int64 == v.RoleGroupID.Int64) {
commandsInGroup = append(commandsInGroup, v)
}
}
sort.Slice(commandsInGroup, RoleCommandsLessFunc(commandsInGroup))
isUp := r.FormValue("dir") == "1"
// Move the position
for i := 0; i < len(commandsInGroup); i++ {
v := commandsInGroup[i]
v.Position = int64(i)
if v.ID == tID {
if isUp {
if i == 0 {
// Can't move further up
continue
}
v.Position--
commandsInGroup[i-1].Position = int64(i)
} else {
if i == len(commandsInGroup)-1 {
// Can't move further down
continue
}
v.Position++
commandsInGroup[i+1].Position = int64(i)
i++
}
}
}
for _, v := range commandsInGroup {
_, lErr := v.UpdateG(r.Context(), boil.Whitelist(models.RoleCommandColumns.Position))
if lErr != nil {
err = lErr
}
}
return tmpl, err
}
func HandleRemoveCommand(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
idParsed, _ := strconv.ParseInt(r.FormValue("ID"), 10, 64)
_, err := models.RoleCommands(qm.Where("guild_id=?", g.ID), qm.Where("id=?", idParsed)).DeleteAll(r.Context(), common.PQ)
if err != nil {
return nil, err
}
return tmpl, nil
}
func HandleNewGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
form := r.Context().Value(common.ContextKeyParsedForm).(*FormGroup)
form.Name = strings.TrimSpace(form.Name)
if c, _ := models.RoleGroups(qm.Where("guild_id=?", g.ID)).CountG(r.Context()); c >= 1000 {
tmpl.AddAlerts(web.ErrorAlert("Max 1000 role groups allowed"))
return tmpl, nil
}
if c, _ := models.RoleGroups(qm.Where("guild_id=?", g.ID), qm.Where("name ILIKE ?", form.Name)).CountG(r.Context()); c > 0 {
tmpl.AddAlerts(web.ErrorAlert("Already a role group with that name"))
return tmpl, nil
}
model := &models.RoleGroup{
Name: form.Name,
GuildID: g.ID,
RequireRoles: form.RequireRoles,
IgnoreRoles: form.IgnoreRoles,
Mode: int64(form.Mode),
MultipleMax: int64(form.MultipleMax),
MultipleMin: int64(form.MultipleMin),
SingleRequireOne: form.SingleRequireOne,
SingleAutoToggleOff: form.SingleAutoToggleOff,
}
err := model.InsertG(r.Context(), boil.Infer())
if err != nil {
return tmpl, err
}
tmpl["GroupID"] = model.ID
return tmpl, nil
}
func HandleUpdateGroup(w http.ResponseWriter, r *http.Request) (tmpl web.TemplateData, err error) {
g, tmpl := web.GetBaseCPContextData(r.Context())
formGroup := r.Context().Value(common.ContextKeyParsedForm).(*FormGroup)
group, err := models.RoleGroups(qm.Where("guild_id=?", g.ID), qm.Where("id=?", formGroup.ID)).OneG(r.Context())
if err != nil {
return
}
group.Name = formGroup.Name
group.IgnoreRoles = formGroup.IgnoreRoles
group.RequireRoles = formGroup.RequireRoles
group.SingleRequireOne = formGroup.SingleRequireOne
group.SingleAutoToggleOff = formGroup.SingleAutoToggleOff
group.MultipleMax = int64(formGroup.MultipleMax)
group.MultipleMin = int64(formGroup.MultipleMin)
group.Mode = int64(formGroup.Mode)
tmpl["GroupID"] = group.ID
_, err = group.UpdateG(r.Context(), boil.Infer())
return
}
func HandleRemoveGroup(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
g, _ := web.GetBaseCPContextData(r.Context())
idParsed, _ := strconv.ParseInt(r.FormValue("ID"), 10, 64)
_, err := models.RoleGroups(qm.Where("guild_id=?", g.ID), qm.Where("id=?", idParsed)).DeleteAll(r.Context(), common.PQ)
return nil, err
}