forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_web.go
170 lines (137 loc) · 5.16 KB
/
plugin_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package serverstats
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/serverstats/models"
"github.com/jonas747/yagpdb/web"
"github.com/volatiletech/null"
"github.com/volatiletech/sqlboiler/boil"
"goji.io"
"goji.io/pat"
"html/template"
"net/http"
"strconv"
"time"
)
type FormData struct {
Public bool
IgnoreChannels []int64 `valid:"channel,false"`
}
func (p *Plugin) InitWeb() {
tmplPath := "templates/plugins/serverstats.html"
if common.Testing {
tmplPath = "../../serverstats/assets/serverstats.html"
}
web.Templates = template.Must(web.Templates.ParseFiles(tmplPath))
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)
statsCPMux.Handle(pat.Get("/"), cpGetHandler)
statsCPMux.Handle(pat.Post("/settings"), web.ControllerPostHandler(HandleSaveStatsSettings, cpGetHandler, FormData{}, "Updated serverstats settings"))
statsCPMux.Handle(pat.Get("/full"), web.APIHandler(publicHandlerJson(HandleStatsJson, false)))
// Public
web.ServerPublicMux.Handle(pat.Get("/stats"), web.RequireGuildChannelsMiddleware(web.ControllerHandler(publicHandler(HandleStatsHtml, true), "cp_serverstats")))
web.ServerPublicMux.Handle(pat.Get("/stats/full"), web.RequireGuildChannelsMiddleware(web.APIHandler(publicHandlerJson(HandleStatsJson, true))))
}
type publicHandlerFunc func(w http.ResponseWriter, r *http.Request, publicAccess bool) (web.TemplateData, error)
func publicHandler(inner publicHandlerFunc, public bool) web.ControllerHandlerFunc {
mw := func(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
return inner(w, r.WithContext(web.SetContextTemplateData(r.Context(), map[string]interface{}{"Public": public})), public)
}
return mw
}
// Somewhat dirty - should clean up this mess sometime
func HandleStatsHtml(w http.ResponseWriter, r *http.Request, isPublicAccess bool) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
config, err := GetConfig(r.Context(), activeGuild.ID)
if err != nil {
return templateData, common.ErrWithCaller(err)
}
templateData["Config"] = config
templateData["ExtraHead"] = template.HTML(`
<link rel="stylesheet" href="/static/vendor/morris/morris.css" />
<link rel="stylesheet" href="/static/vendor/chartist/chartist.min.css" />
`)
return templateData, nil
}
func HandleSaveStatsSettings(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ag, templateData := web.GetBaseCPContextData(r.Context())
formData := r.Context().Value(common.ContextKeyParsedForm).(*FormData)
stringedChannels := ""
alreadyAdded := make([]int64, 0, len(formData.IgnoreChannels))
OUTER:
for i, v := range formData.IgnoreChannels {
// only add each once
for _, ad := range alreadyAdded {
if ad == v {
continue OUTER
}
}
// make sure the channel exists
channelExists := false
for _, ec := range ag.Channels {
if ec.ID == v {
channelExists = true
break
}
}
if !channelExists {
continue
}
if i != 0 {
stringedChannels += ","
}
alreadyAdded = append(alreadyAdded, v)
stringedChannels += strconv.FormatInt(v, 10)
}
model := &models.ServerStatsConfig{
GuildID: ag.ID,
Public: null.BoolFrom(formData.Public),
IgnoreChannels: null.StringFrom(stringedChannels),
CreatedAt: null.TimeFrom(time.Now()),
}
err := model.UpsertG(r.Context(), true, []string{"guild_id"}, boil.Whitelist("public", "ignore_channels"), boil.Infer())
if err == nil {
go pubsub.Publish("server_stats_invalidate_cache", ag.ID, nil)
}
return templateData, err
}
type publicHandlerFuncJson func(w http.ResponseWriter, r *http.Request, publicAccess bool) interface{}
func publicHandlerJson(inner publicHandlerFuncJson, public bool) web.CustomHandlerFunc {
mw := func(w http.ResponseWriter, r *http.Request) interface{} {
return inner(w, r.WithContext(web.SetContextTemplateData(r.Context(), map[string]interface{}{"Public": public})), public)
}
return mw
}
func HandleStatsJson(w http.ResponseWriter, r *http.Request, isPublicAccess bool) interface{} {
activeGuild, _ := web.GetBaseCPContextData(r.Context())
conf, err := GetConfig(r.Context(), activeGuild.ID)
if err != nil {
web.CtxLogger(r.Context()).WithError(err).Error("Failed retrieving stats config")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if !conf.Public && isPublicAccess {
return nil
}
stats, err := RetrieveFullStats(activeGuild.ID)
if err != nil {
web.CtxLogger(r.Context()).WithError(err).Error("Failed retrieving stats")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
// Update the names to human readable ones, leave the ids in the name fields for the ones not available
for _, cs := range stats.ChannelsHour {
for _, channel := range activeGuild.Channels {
if discordgo.StrID(channel.ID) == cs.Name {
cs.Name = channel.Name
break
}
}
}
return stats
}