forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
92 lines (73 loc) · 2.85 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
package streaming
import (
"context"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/web"
"goji.io"
"goji.io/pat"
"html/template"
"net/http"
)
type ConextKey int
const (
ConextKeyConfig ConextKey = iota
)
func (p *Plugin) InitWeb() {
tmplPath := "templates/plugins/streaming.html"
if common.Testing {
tmplPath = "../../streaming/assets/streaming.html"
}
web.Templates = template.Must(web.Templates.ParseFiles(tmplPath))
streamingMux := goji.SubMux()
web.CPMux.Handle(pat.New("/streaming/*"), streamingMux)
web.CPMux.Handle(pat.New("/streaming"), streamingMux)
// Alll handlers here require guild channels present
streamingMux.Use(web.RequireGuildChannelsMiddleware)
streamingMux.Use(web.RequireFullGuildMW)
streamingMux.Use(web.RequireBotMemberMW)
streamingMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles))
streamingMux.Use(baseData)
// Get just renders the template, so let the renderhandler do all the work
streamingMux.Handle(pat.Get(""), web.RenderHandler(nil, "cp_streaming"))
streamingMux.Handle(pat.Get("/"), web.RenderHandler(nil, "cp_streaming"))
streamingMux.Handle(pat.Post(""), web.FormParserMW(web.RenderHandler(HandlePostStreaming, "cp_streaming"), Config{}))
streamingMux.Handle(pat.Post("/"), web.FormParserMW(web.RenderHandler(HandlePostStreaming, "cp_streaming"), Config{}))
}
// Adds the current config to the context
func baseData(inner http.Handler) http.Handler {
mw := func(w http.ResponseWriter, r *http.Request) {
guild, tmpl := web.GetBaseCPContextData(r.Context())
config, err := GetConfig(guild.ID)
if web.CheckErr(tmpl, err, "Failed retrieving streaming config :'(", web.CtxLogger(r.Context()).Error) {
web.LogIgnoreErr(web.Templates.ExecuteTemplate(w, "cp_streaming", tmpl))
return
}
tmpl["StreamingConfig"] = config
inner.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ConextKeyConfig, config)))
}
return http.HandlerFunc(mw)
}
func HandlePostStreaming(w http.ResponseWriter, r *http.Request) interface{} {
ctx := r.Context()
guild, tmpl := web.GetBaseCPContextData(ctx)
tmpl["VisibleURL"] = "/manage/" + discordgo.StrID(guild.ID) + "/streaming/"
ok := ctx.Value(common.ContextKeyFormOk).(bool)
newConf := ctx.Value(common.ContextKeyParsedForm).(*Config)
tmpl["StreamingConfig"] = newConf
if !ok {
return tmpl
}
err := newConf.Save(guild.ID)
if web.CheckErr(tmpl, err, "Failed saving config :'(", web.CtxLogger(ctx).Error) {
return tmpl
}
err = pubsub.Publish("update_streaming", guild.ID, nil)
if err != nil {
web.CtxLogger(ctx).WithError(err).Error("Failed sending update streaming event")
}
user := ctx.Value(common.ContextKeyUser).(*discordgo.User)
common.AddCPLogEntry(user, guild.ID, "Updated streaming config.")
return tmpl.AddAlerts(web.SucessAlert("Saved settings"))
}