Skip to content

Commit

Permalink
web: fully remove RequireGuildChannelsMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
jogramming committed Jul 13, 2020
1 parent d70786e commit 2d8f8ea
Show file tree
Hide file tree
Showing 15 changed files with 19 additions and 47 deletions.
2 changes: 0 additions & 2 deletions automod/automod_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/automod"), muxer)
web.CPMux.Handle(pat.New("/automod/*"), muxer)

muxer.Use(web.RequireGuildChannelsMiddleware)

getIndexHandler := web.ControllerHandler(p.handleGetAutomodIndex, "automod_index")

muxer.Handle(pat.Get("/"), getIndexHandler)
Expand Down
1 change: 0 additions & 1 deletion automod_legacy/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/automod_legacy"), autmodMux)

// Alll handlers here require guild channels present
autmodMux.Use(web.RequireGuildChannelsMiddleware)
autmodMux.Use(web.RequireBotMemberMW)
autmodMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles, discordgo.PermissionKickMembers, discordgo.PermissionBanMembers, discordgo.PermissionManageMessages))

Expand Down
2 changes: 0 additions & 2 deletions commands/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/commands/settings"), subMux)
web.CPMux.Handle(pat.New("/commands/settings/*"), subMux)

subMux.Use(web.RequireGuildChannelsMiddleware)

getHandler := web.ControllerHandler(HandleCommands, "cp_commands")
subMux.Handle(pat.Get(""), getHandler)
subMux.Handle(pat.Get("/"), getHandler)
Expand Down
1 change: 0 additions & 1 deletion customcommands/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (p *Plugin) InitWeb() {
}
return http.HandlerFunc(h)
})
subMux.Use(web.RequireGuildChannelsMiddleware)

subMux.Handle(pat.Get(""), getHandler)
subMux.Handle(pat.Get("/"), getHandler)
Expand Down
2 changes: 0 additions & 2 deletions logs/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ func (lp *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/logging"), logCPMux)
web.CPMux.Handle(pat.New("/logging/*"), logCPMux)

logCPMux.Use(web.RequireGuildChannelsMiddleware)

cpGetHandler := web.ControllerHandler(HandleLogsCP, "cp_logging")
logCPMux.Handle(pat.Get("/"), cpGetHandler)
logCPMux.Handle(pat.Get(""), cpGetHandler)
Expand Down
2 changes: 0 additions & 2 deletions moderation/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/moderation"), subMux)
web.CPMux.Handle(pat.New("/moderation/*"), subMux)

subMux.Use(web.RequireGuildChannelsMiddleware)

subMux.Use(web.RequireBotMemberMW) // need the bot's role
subMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles, discordgo.PermissionKickMembers, discordgo.PermissionBanMembers, discordgo.PermissionManageMessages, discordgo.PermissionEmbedLinks))

Expand Down
13 changes: 7 additions & 6 deletions notifications/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package notifications

import (
"fmt"
"html/template"
"net/http"

"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/configstore"
"github.com/jonas747/yagpdb/web"
"goji.io/pat"
"html/template"
"net/http"
)

func (p *Plugin) InitWeb() {
Expand All @@ -22,11 +23,11 @@ func (p *Plugin) InitWeb() {
getHandler := web.RenderHandler(HandleNotificationsGet, "cp_notifications_general")
postHandler := web.ControllerPostHandler(HandleNotificationsPost, getHandler, Config{}, "Updated general notifications config.")

web.CPMux.Handle(pat.Get("/notifications/general"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Get("/notifications/general/"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Get("/notifications/general"), getHandler)
web.CPMux.Handle(pat.Get("/notifications/general/"), getHandler)

web.CPMux.Handle(pat.Post("/notifications/general"), web.RequireGuildChannelsMiddleware(postHandler))
web.CPMux.Handle(pat.Post("/notifications/general/"), web.RequireGuildChannelsMiddleware(postHandler))
web.CPMux.Handle(pat.Post("/notifications/general"), postHandler)
web.CPMux.Handle(pat.Post("/notifications/general/"), postHandler)
}

func HandleNotificationsGet(w http.ResponseWriter, r *http.Request) interface{} {
Expand Down
1 change: 0 additions & 1 deletion reddit/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/reddit"), redditMux)

// Alll handlers here require guild channels present
redditMux.Use(web.RequireGuildChannelsMiddleware)
redditMux.Use(web.RequireBotMemberMW)
redditMux.Use(web.RequirePermMW(discordgo.PermissionManageWebhooks))
redditMux.Use(baseData)
Expand Down
7 changes: 3 additions & 4 deletions serverstats/plugin_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func (p *Plugin) InitWeb() {
statsCPMux := goji.SubMux()
web.CPMux.Handle(pat.New("/stats"), statsCPMux)
web.CPMux.Handle(pat.New("/stats/*"), statsCPMux)
statsCPMux.Use(web.RequireGuildChannelsMiddleware)

cpGetHandler := web.ControllerHandler(publicHandler(HandleStatsHtml, false), "cp_serverstats")
statsCPMux.Handle(pat.Get(""), cpGetHandler)
Expand All @@ -48,9 +47,9 @@ func (p *Plugin) InitWeb() {
statsCPMux.Handle(pat.Get("/charts"), web.APIHandler(publicHandlerJson(HandleStatsCharts, false)))

// Public
web.ServerPublicMux.Handle(pat.Get("/stats"), web.RequireGuildChannelsMiddleware(web.ControllerHandler(publicHandler(HandleStatsHtml, true), "cp_serverstats")))
web.ServerPublicMux.Handle(pat.Get("/stats/daily_json"), web.RequireGuildChannelsMiddleware(web.APIHandler(publicHandlerJson(HandleStatsJson, true))))
web.ServerPublicMux.Handle(pat.Get("/stats/charts"), web.RequireGuildChannelsMiddleware(web.APIHandler(publicHandlerJson(HandleStatsCharts, true))))
web.ServerPublicMux.Handle(pat.Get("/stats"), web.ControllerHandler(publicHandler(HandleStatsHtml, true), "cp_serverstats"))
web.ServerPublicMux.Handle(pat.Get("/stats/daily_json"), web.APIHandler(publicHandlerJson(HandleStatsJson, true)))
web.ServerPublicMux.Handle(pat.Get("/stats/charts"), web.APIHandler(publicHandlerJson(HandleStatsCharts, true)))
}

type publicHandlerFunc func(w http.ResponseWriter, r *http.Request, publicAccess bool) (web.TemplateData, error)
Expand Down
6 changes: 0 additions & 6 deletions streaming/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/streaming"), streamingMux)

// Alll handlers here require guild channels present
streamingMux.Use(web.RequireGuildChannelsMiddleware)
streamingMux.Use(web.RequireBotMemberMW)
streamingMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles))
streamingMux.Use(baseData)
Expand Down Expand Up @@ -100,7 +99,6 @@ func HandlePostStreaming(w http.ResponseWriter, r *http.Request) interface{} {
}

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

func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ag, templateData := web.GetBaseCPContextData(r.Context())
Expand Down Expand Up @@ -150,7 +148,3 @@ func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (w

return templateData, nil
}

func (p *Plugin) ServerHomeWidgetApplyMiddlewares(inner http.Handler) http.Handler {
return web.RequireGuildChannelsMiddleware(inner)
}
11 changes: 6 additions & 5 deletions tickets/tickets_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package tickets
import (
"database/sql"
"fmt"
"html/template"
"net/http"

"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/tickets/models"
"github.com/jonas747/yagpdb/web"
"github.com/volatiletech/sqlboiler/boil"
"goji.io/pat"
"html/template"
"net/http"
)

type FormData struct {
Expand Down Expand Up @@ -38,10 +39,10 @@ func (p *Plugin) InitWeb() {
getHandler := web.ControllerHandler(p.handleGetSettings, "cp_tickets_settings")
postHandler := web.ControllerPostHandler(p.handlePostSettings, getHandler, FormData{}, "Updated ticket settings")

web.CPMux.Handle(pat.Get("/tickets/settings"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Get("/tickets/settings/"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Get("/tickets/settings"), getHandler)
web.CPMux.Handle(pat.Get("/tickets/settings/"), getHandler)

web.CPMux.Handle(pat.Post("/tickets/settings"), web.RequireGuildChannelsMiddleware(postHandler))
web.CPMux.Handle(pat.Post("/tickets/settings"), postHandler)
}

func (p *Plugin) handleGetSettings(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
Expand Down
3 changes: 0 additions & 3 deletions twitter/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/twitter/*"), mux)
web.CPMux.Handle(pat.New("/twitter"), mux)

// Alll handlers here require guild channels present
mux.Use(web.RequireGuildChannelsMiddleware)

mainGetHandler := web.ControllerHandler(p.HandleTwitter, "cp_twitter")

mux.Handle(pat.Get("/"), mainGetHandler)
Expand Down
6 changes: 3 additions & 3 deletions verification/verification_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func (p *Plugin) InitWeb() {
getHandler := web.ControllerHandler(p.handleGetSettings, "cp_verification_settings")
postHandler := web.ControllerPostHandler(p.handlePostSettings, getHandler, FormData{}, "Updated verification settings")

web.CPMux.Handle(pat.Get("/verification"), web.RequireBotMemberMW(web.RequireGuildChannelsMiddleware(getHandler)))
web.CPMux.Handle(pat.Get("/verification/"), web.RequireBotMemberMW(web.RequireGuildChannelsMiddleware(getHandler)))
web.CPMux.Handle(pat.Get("/verification"), web.RequireBotMemberMW(getHandler))
web.CPMux.Handle(pat.Get("/verification/"), web.RequireBotMemberMW(getHandler))

web.CPMux.Handle(pat.Post("/verification"), web.RequireGuildChannelsMiddleware(postHandler))
web.CPMux.Handle(pat.Post("/verification"), postHandler)

getVerifyPageHandler := web.ControllerHandler(p.handleGetVerifyPage, "verification_verify_page")
postVerifyPageHandler := web.ControllerPostHandler(p.handlePostVerifyPage, getVerifyPageHandler, nil, "")
Expand Down
8 changes: 0 additions & 8 deletions web/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,6 @@ func RequireServerAdminMiddleware(inner http.Handler) http.Handler {
return http.HandlerFunc(mw)
}

// RequireGuildChannelsMiddleware ensures that the channels are available for the guild were on during this request, and yes this has to be done seperately cause discord
func RequireGuildChannelsMiddleware(inner http.Handler) http.Handler {
mw := func(w http.ResponseWriter, r *http.Request) {
inner.ServeHTTP(w, r)
}
return http.HandlerFunc(mw)
}

// RequireBotMemberMW ensures that the bot member for the curreng guild is available, mostly used for checking the bot's roles
func RequireBotMemberMW(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 0 additions & 1 deletion youtube/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func (p *Plugin) InitWeb() {
web.CPMux.Handle(pat.New("/youtube"), ytMux)

// Alll handlers here require guild channels present
ytMux.Use(web.RequireGuildChannelsMiddleware)
ytMux.Use(web.RequireBotMemberMW)
ytMux.Use(web.RequirePermMW(discordgo.PermissionMentionEveryone))

Expand Down

0 comments on commit 2d8f8ea

Please sign in to comment.