-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.go
145 lines (107 loc) · 4.06 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
package autorole
import (
"fmt"
"html"
"html/template"
"net/http"
"emperror.dev/errors"
"github.com/jonas747/discordgo"
"github.com/mrbentarikau/pagst/common"
"github.com/mrbentarikau/pagst/common/cplogs"
"github.com/mrbentarikau/pagst/common/pubsub"
"github.com/mrbentarikau/pagst/web"
"github.com/mediocregopher/radix/v3"
"goji.io"
"goji.io/pat"
)
type Form struct {
GeneralConfig `valid:"traverse"`
}
var _ web.SimpleConfigSaver = (*Form)(nil)
var (
panelLogKeyUpdatedSettings = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "autorole_settings_updated", FormatString: "Updated autorole settings"})
panelLogKeyStartedFullScan = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "autorole_full_scan", FormatString: "Started full retroactive autorole scan"})
)
func (f Form) Save(guildID int64) error {
pubsub.Publish("autorole_stop_processing", guildID, nil)
err := common.SetRedisJson(KeyGeneral(guildID), f.GeneralConfig)
if err != nil {
return err
}
return nil
}
func (f Form) Name() string {
return "Autorole"
}
func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../autorole/assets/autorole.html", "templates/plugins/autorole.html")
web.AddSidebarItem(web.SidebarCategoryTools, &web.SidebarItem{
Name: "Autorole",
URL: "autorole",
Icon: "fas fa-user-plus",
})
muxer := goji.SubMux()
web.CPMux.Handle(pat.New("/autorole"), muxer)
web.CPMux.Handle(pat.New("/autorole/*"), muxer)
muxer.Use(web.RequireBotMemberMW) // need the bot's role
muxer.Use(web.RequirePermMW(discordgo.PermissionManageRoles))
getHandler := web.RenderHandler(handleGetAutoroleMainPage, "cp_autorole")
muxer.Handle(pat.Get(""), getHandler)
muxer.Handle(pat.Get("/"), getHandler)
muxer.Handle(pat.Post("/fullscan"), web.ControllerPostHandler(handlePostFullScan, getHandler, nil))
muxer.Handle(pat.Post(""), web.SimpleConfigSaverHandler(Form{}, getHandler, panelLogKeyUpdatedSettings))
muxer.Handle(pat.Post("/"), web.SimpleConfigSaverHandler(Form{}, getHandler, panelLogKeyUpdatedSettings))
}
func handleGetAutoroleMainPage(w http.ResponseWriter, r *http.Request) interface{} {
ctx := r.Context()
activeGuild, tmpl := web.GetBaseCPContextData(ctx)
general, err := GetGeneralConfig(activeGuild.ID)
web.CheckErr(tmpl, err, "Failed retrieving general config (contact support)", web.CtxLogger(r.Context()).Error)
tmpl["Autorole"] = general
var proc int
common.RedisPool.Do(radix.Cmd(&proc, "GET", KeyProcessing(activeGuild.ID)))
tmpl["Processing"] = proc
tmpl["ProcessingETA"] = int(proc / 60)
fullScanActive := WorkingOnFullScan(activeGuild.ID)
tmpl["FullScanActive"] = fullScanActive
return tmpl
}
func handlePostFullScan(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, tmpl := web.GetBaseCPContextData(ctx)
err := botRestPostFullScan(activeGuild.ID)
if err != nil {
if err == ErrAlreadyProcessingFullGuild {
return tmpl.AddAlerts(web.ErrorAlert("Already processing, please wait.")), nil
}
return tmpl, errors.WithMessage(err, "botrest")
}
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyStartedFullScan))
return tmpl, nil
}
var _ web.PluginWithServerHomeWidget = (*Plugin)(nil)
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ag, templateData := web.GetBaseCPContextData(r.Context())
templateData["WidgetTitle"] = "Autorole"
templateData["SettingsPath"] = "/autorole"
general, err := GetGeneralConfig(ag.ID)
if err != nil {
return templateData, err
}
enabledDisabled := ""
autoroleRole := "none"
if role := ag.Role(general.Role); role != nil {
templateData["WidgetEnabled"] = true
enabledDisabled = web.EnabledDisabledSpanStatus(true)
autoroleRole = html.EscapeString(role.Name)
} else {
templateData["WidgetDisabled"] = true
enabledDisabled = web.EnabledDisabledSpanStatus(false)
}
format := `<ul>
<li>Autorole status: %s</li>
<li>Autorole role: <code>%s</code></li>
</ul>`
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format, enabledDisabled, autoroleRole))
return templateData, nil
}