Skip to content

Commit

Permalink
initial revision of new cplogs
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Jul 24, 2020
1 parent 44e23ce commit b59c66d
Show file tree
Hide file tree
Showing 30 changed files with 568 additions and 132 deletions.
12 changes: 6 additions & 6 deletions admin/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/jonas747/yagpdb/web"
"goji.io"
"goji.io/pat"
)
)

// InitWeb implements web.Plugin
func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../admin/assets/bot_admin_panel.html", "templates/plugins/bot_admin_panel.html")
Expand All @@ -43,11 +43,11 @@ func (p *Plugin) InitWeb() {
mux.Handle(pat.Get("/host/:host/pid/:pid/allocs"), p.ProxyGetInternalAPI("/debug/pprof/allocs"))

// Control routes
mux.Handle(pat.Post("/host/:host/pid/:pid/shutdown"), web.ControllerPostHandler(p.handleShutdown, panelHandler, nil, ""))
mux.Handle(pat.Post("/host/:host/pid/:pid/shutdown"), web.ControllerPostHandler(p.handleShutdown, panelHandler, nil))

// Orhcestrator controls
mux.Handle(pat.Post("/host/:host/pid/:pid/updateversion"), web.ControllerPostHandler(p.handleUpgrade, panelHandler, nil, ""))
mux.Handle(pat.Post("/host/:host/pid/:pid/migratenodes"), web.ControllerPostHandler(p.handleMigrateNodes, panelHandler, nil, ""))
mux.Handle(pat.Post("/host/:host/pid/:pid/updateversion"), web.ControllerPostHandler(p.handleUpgrade, panelHandler, nil))
mux.Handle(pat.Post("/host/:host/pid/:pid/migratenodes"), web.ControllerPostHandler(p.handleMigrateNodes, panelHandler, nil))
mux.Handle(pat.Get("/host/:host/pid/:pid/deployedversion"), http.HandlerFunc(p.handleLaunchNodeVersion))

// Node routes
Expand All @@ -56,7 +56,7 @@ func (p *Plugin) InitWeb() {

getConfigHandler := web.ControllerHandler(p.handleGetConfig, "bot_admin_config")
mux.Handle(pat.Get("/config"), getConfigHandler)
mux.Handle(pat.Post("/config/edit/:key"), web.ControllerPostHandler(p.handleEditConfig, getConfigHandler, nil, ""))
mux.Handle(pat.Post("/config/edit/:key"), web.ControllerPostHandler(p.handleEditConfig, getConfigHandler, nil))
}

type Host struct {
Expand Down
52 changes: 40 additions & 12 deletions automod/automod_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/jonas747/yagpdb/automod/models"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/cplogs"
"github.com/jonas747/yagpdb/common/featureflags"
"github.com/jonas747/yagpdb/web"
"github.com/volatiletech/sqlboiler/boil"
Expand All @@ -33,6 +34,20 @@ const (

var _ web.Plugin = (*Plugin)(nil)

var (
panelLogKeyNewList = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_new_list", FormatString: "Updated automod: Created a new ChannelOverride"})
panelLogKeyUpdatedList = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_updated_list", FormatString: "Updated automod: Updated a ChannelOverride"})
panelLogKeyRemovedList = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_removed_list", FormatString: "Updated automod: Removed a ChannelOverride"})

panelLogKeyNewRuleset = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_new_ruleset", FormatString: "Updated automod: Created a new ruleset"})
panelLogKeyUpdatedRuleset = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_updated_ruleset", FormatString: "Updated automod: Updated a ruleset"})
panelLogKeyRemovedRuleset = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_removed_ruleset", FormatString: "Updated automod: Removed a ruleset"})

panelLogKeyNewRule = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_new_rule", FormatString: "Updated automod: Created a new rule"})
panelLogKeyUpdatedRule = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_updated_rule", FormatString: "Updated automod: Updated a rule"})
panelLogKeyRemovedRule = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automodv2_removed_rule", FormatString: "Updated automod: Removed a rule"})
)

func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../automod/assets/automod.html", "templates/plugins/automod.html")
web.AddSidebarItem(web.SidebarCategoryTools, &web.SidebarItem{
Expand All @@ -52,12 +67,12 @@ func (p *Plugin) InitWeb() {
muxer.Handle(pat.Get(""), getIndexHandler)
muxer.Handle(pat.Get("/logs"), web.ControllerHandler(p.handleGetLogs, "automod_index"))

muxer.Handle(pat.Post("/new_ruleset"), web.ControllerPostHandler(p.handlePostAutomodCreateRuleset, getIndexHandler, CreateRulesetData{}, "Created a new automod ruleset"))
muxer.Handle(pat.Post("/new_ruleset"), web.ControllerPostHandler(p.handlePostAutomodCreateRuleset, getIndexHandler, CreateRulesetData{}))

// List handlers
muxer.Handle(pat.Post("/new_list"), web.ControllerPostHandler(p.handlePostAutomodCreateList, getIndexHandler, CreateListData{}, "Created a new automod list"))
muxer.Handle(pat.Post("/list/:listID/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateList, getIndexHandler, UpdateListData{}, "Updated a automod list"))
muxer.Handle(pat.Post("/list/:listID/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteList, getIndexHandler, nil, "Deleted a automod list"))
muxer.Handle(pat.Post("/new_list"), web.ControllerPostHandler(p.handlePostAutomodCreateList, getIndexHandler, CreateListData{}))
muxer.Handle(pat.Post("/list/:listID/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateList, getIndexHandler, UpdateListData{}))
muxer.Handle(pat.Post("/list/:listID/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteList, getIndexHandler, nil))

// Ruleset specific handlers
rulesetMuxer := goji.SubMux()
Expand All @@ -70,12 +85,12 @@ func (p *Plugin) InitWeb() {
rulesetMuxer.Handle(pat.Get(""), getRulesetHandler)
rulesetMuxer.Handle(pat.Get("/"), getRulesetHandler)

rulesetMuxer.Handle(pat.Post("/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateRuleset, getRulesetHandler, UpdateRulesetData{}, "Updated a ruleset"))
rulesetMuxer.Handle(pat.Post("/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteRuleset, getIndexHandler, nil, "Deleted a ruleset"))
rulesetMuxer.Handle(pat.Post("/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateRuleset, getRulesetHandler, UpdateRulesetData{}))
rulesetMuxer.Handle(pat.Post("/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteRuleset, getIndexHandler, nil))

rulesetMuxer.Handle(pat.Post("/new_rule"), web.ControllerPostHandler(p.handlePostAutomodCreateRule, getRulesetHandler, CreateRuleData{}, "Created a new automod rule"))
rulesetMuxer.Handle(pat.Post("/rule/:ruleID/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteRule, getRulesetHandler, nil, "Deleted a automod rule"))
rulesetMuxer.Handle(pat.Post("/rule/:ruleID/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateRule, getRulesetHandler, UpdateRuleData{}, "Updated a automod rule"))
rulesetMuxer.Handle(pat.Post("/new_rule"), web.ControllerPostHandler(p.handlePostAutomodCreateRule, getRulesetHandler, CreateRuleData{}))
rulesetMuxer.Handle(pat.Post("/rule/:ruleID/delete"), web.ControllerPostHandler(p.handlePostAutomodDeleteRule, getRulesetHandler, nil))
rulesetMuxer.Handle(pat.Post("/rule/:ruleID/update"), web.ControllerPostHandler(p.handlePostAutomodUpdateRule, getRulesetHandler, UpdateRuleData{}))
}

func (p *Plugin) handleGetAutomodIndex(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
Expand Down Expand Up @@ -158,6 +173,9 @@ func (p *Plugin) handlePostAutomodCreateRuleset(w http.ResponseWriter, r *http.R
}

err = rs.InsertG(r.Context(), boil.Infer())
if err == nil {
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyNewRuleset))
}
return tmpl, err
}

Expand Down Expand Up @@ -188,6 +206,7 @@ func (p *Plugin) handlePostAutomodCreateList(w http.ResponseWriter, r *http.Requ
err = list.InsertG(r.Context(), boil.Infer())
if err == nil {
bot.EvictGSCache(g.ID, CacheKeyLists)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyNewList))
}
return tmpl, err
}
Expand All @@ -210,6 +229,7 @@ func (p *Plugin) handlePostAutomodUpdateList(w http.ResponseWriter, r *http.Requ
_, err = list.UpdateG(r.Context(), boil.Whitelist("content"))
if err == nil {
bot.EvictGSCache(g.ID, CacheKeyLists)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyUpdatedList))
}
return tmpl, err
}
Expand All @@ -226,6 +246,7 @@ func (p *Plugin) handlePostAutomodDeleteList(w http.ResponseWriter, r *http.Requ
_, err = list.DeleteG(r.Context())
if err == nil {
bot.EvictGSCache(g.ID, CacheKeyLists)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyRemovedList))
}
return tmpl, err
}
Expand Down Expand Up @@ -297,6 +318,7 @@ func (p *Plugin) handlePostAutomodCreateRule(w http.ResponseWriter, r *http.Requ
err = rule.InsertG(r.Context(), boil.Infer())
if err == nil {
ruleset.R.RulesetAutomodRules = append(ruleset.R.RulesetAutomodRules, rule)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyNewRule))
}

return tmpl, err
Expand Down Expand Up @@ -370,6 +392,7 @@ func (p *Plugin) handlePostAutomodUpdateRuleset(w http.ResponseWriter, r *http.R

bot.EvictGSCache(g.ID, CacheKeyRulesets)
featureflags.MarkGuildDirty(g.ID)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyUpdatedRuleset))

// Reload the conditions now
ruleset.R.RulesetAutomodRulesetConditions = properConditions
Expand All @@ -382,15 +405,18 @@ func (p *Plugin) handlePostAutomodDeleteRuleset(w http.ResponseWriter, r *http.R
g, tmpl := web.GetBaseCPContextData(r.Context())

ruleset := r.Context().Value(CtxKeyCurrentRuleset).(*models.AutomodRuleset)
_, err := ruleset.DeleteG(r.Context())
rows, err := ruleset.DeleteG(r.Context())
if err != nil {
return tmpl, err
}

delete(tmpl, "CurrentRuleset")

bot.EvictGSCache(g.ID, CacheKeyRulesets)
featureflags.MarkGuildDirty(g.ID)
if rows > 0 {
bot.EvictGSCache(g.ID, CacheKeyRulesets)
featureflags.MarkGuildDirty(g.ID)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyRemovedRuleset))
}

return tmpl, err
}
Expand Down Expand Up @@ -499,6 +525,7 @@ func (p *Plugin) handlePostAutomodUpdateRule(w http.ResponseWriter, r *http.Requ

bot.EvictGSCache(g.ID, CacheKeyRulesets)
featureflags.MarkGuildDirty(g.ID)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyUpdatedRule))

return tmpl, err
}
Expand Down Expand Up @@ -699,6 +726,7 @@ func (p *Plugin) handlePostAutomodDeleteRule(w http.ResponseWriter, r *http.Requ
ruleset.R.RulesetAutomodRules = append(ruleset.R.RulesetAutomodRules[:k], ruleset.R.RulesetAutomodRules[k+1:]...)
bot.EvictGSCache(g.ID, CacheKeyRulesets)
featureflags.MarkGuildDirty(g.ID)
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyRemovedRule))
}

return nil, err
Expand Down
7 changes: 5 additions & 2 deletions automod_legacy/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common/cplogs"
"github.com/jonas747/yagpdb/common/featureflags"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/web"
Expand All @@ -23,6 +24,8 @@ type GeneralForm struct {
Enabled bool
}

var panelLogKeyUpdatedSettings = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "automod_legacy_settings_updated", FormatString: "Updated legacy automod settings"})

func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../automod_legacy/assets/automod_legacy.html", "templates/plugins/automod_legacy.html")

Expand All @@ -46,8 +49,8 @@ func (p *Plugin) InitWeb() {
autmodMux.Handle(pat.Get(""), getHandler)

// Post handlers
autmodMux.Handle(pat.Post("/"), ExtraPostMW(web.SimpleConfigSaverHandler(Config{}, getHandler)))
autmodMux.Handle(pat.Post(""), ExtraPostMW(web.SimpleConfigSaverHandler(Config{}, getHandler)))
autmodMux.Handle(pat.Post("/"), ExtraPostMW(web.SimpleConfigSaverHandler(Config{}, getHandler, panelLogKeyUpdatedSettings)))
autmodMux.Handle(pat.Post(""), ExtraPostMW(web.SimpleConfigSaverHandler(Config{}, getHandler, panelLogKeyUpdatedSettings)))
}

func HandleAutomod(w http.ResponseWriter, r *http.Request) interface{} {
Expand Down
14 changes: 11 additions & 3 deletions autorole/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"emperror.dev/errors"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/cplogs"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/web"
"github.com/mediocregopher/radix/v3"
Expand All @@ -22,6 +23,11 @@ type Form struct {

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)

Expand Down Expand Up @@ -59,10 +65,10 @@ func (p *Plugin) InitWeb() {
muxer.Handle(pat.Get(""), getHandler)
muxer.Handle(pat.Get("/"), getHandler)

muxer.Handle(pat.Post("/fullscan"), web.ControllerPostHandler(handlePostFullScan, getHandler, nil, "Triggered a full autorole scan"))
muxer.Handle(pat.Post("/fullscan"), web.ControllerPostHandler(handlePostFullScan, getHandler, nil))

muxer.Handle(pat.Post(""), web.SimpleConfigSaverHandler(Form{}, getHandler))
muxer.Handle(pat.Post("/"), web.SimpleConfigSaverHandler(Form{}, getHandler))
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{} {
Expand Down Expand Up @@ -98,6 +104,8 @@ func handlePostFullScan(w http.ResponseWriter, r *http.Request) (web.TemplateDat
return tmpl, errors.WithMessage(err, "botrest")
}

go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyStartedFullScan))

return tmpl, nil
}

Expand Down
15 changes: 10 additions & 5 deletions cmd/yagpdb/templates/cp_logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ <h2>Control panel logs</h2>
<div class="col-lg-12">
<section class="card">
<div class="card-body">
<p>Logs who changed settings, and in most (95%) cases what. (This isn't where kicks and bans are logged, set those up under moderation settings).</p>
<p>Control panel logs</p>
<div class="bs-callout bs-callout-info">
<p>Note: If you see someone with the name jonas747 and the id <code>105487308693757952</code>, then don't be scared because that is me (the bot owner). The bot is very large and if something is causing me issues I might have to change update something to fix those issues, join the <a href="https://discord.gg/4udtcA5">support server</a> if you have more questions.</p>
<p>Note: If you see someone with the name jonas747 and the id <code>105487308693757952</code>, then
don't be scared because that is me (the bot owner). The bot is very large and if something is
causing me issues I might have to change update something to fix those issues, join the <a
href="https://discord.gg/4udtcA5">support server</a> if you have more questions.</p>
</div>
<table class="table table-responsive-lg table-bordered table-striped table-sm mb-0">
<thead>
<tr>
<th>Time</th>
<th>User</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{{range .entries}}
<tr>
<td>{{.TimestampString}}</td>
<td>{{.Action}}</td>
<td>{{formatTime .CreatedAt.UTC}}</td>
<td>{{.AuthorUsername}} (<code>{{.AuthorID}}</code>)</td>
<td>{{.Action.String}}</td>
</tr>
{{end}}
</tbody>
Expand All @@ -45,4 +50,4 @@ <h2>Control panel logs</h2>
<!-- /.row -->
{{template "cp_footer" .}}

{{end}}
{{end}}
Loading

0 comments on commit b59c66d

Please sign in to comment.