forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
150 lines (117 loc) · 4.36 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
package streaming
import (
"context"
"fmt"
"html"
"html/template"
"net/http"
"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"
)
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.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"))
}
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())
templateData["WidgetTitle"] = "Streaming"
templateData["SettingsPath"] = "/streaming"
config, err := GetConfig(ag.ID)
if err != nil {
return templateData, err
}
format := `<ul>
<li>Streaming status: %s</li>
<li>Streaming role: <code>%s</code>%s</li>
<li>Streaming message: <code>#%s</code>%s</li>
</ul>`
status := web.EnabledDisabledSpanStatus(config.Enabled)
if config.Enabled {
templateData["WidgetEnabled"] = true
} else {
templateData["WidgetDisabled"] = true
}
roleStr := "none / unknown"
indicatorRole := ""
if role := ag.Role(config.GiveRole); role != nil {
roleStr = html.EscapeString(role.Name)
indicatorRole = web.Indicator(true)
} else {
indicatorRole = web.Indicator(false)
}
indicatorMessage := ""
channelStr := "none / unknown"
if channel := ag.Channel(config.AnnounceChannel); channel != nil {
indicatorMessage = web.Indicator(true)
channelStr = html.EscapeString(channel.Name)
} else {
indicatorMessage = web.Indicator(false)
}
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format, status, roleStr, indicatorRole, channelStr, indicatorMessage))
return templateData, nil
}
func (p *Plugin) ServerHomeWidgetApplyMiddlewares(inner http.Handler) http.Handler {
return web.RequireGuildChannelsMiddleware(inner)
}