-
Notifications
You must be signed in to change notification settings - Fork 910
/
plugin_web.go
107 lines (83 loc) · 3.27 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
package notifications
import (
"fmt"
"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() {
web.LoadHTMLTemplate("../../notifications/assets/notifications_general.html", "templates/plugins/notifications_general.html")
web.AddSidebarItem(web.SidebarCategoryFeeds, &web.SidebarItem{
Name: "General",
URL: "notifications/general",
Icon: "fas fa-bell",
})
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.Post("/notifications/general"), web.RequireGuildChannelsMiddleware(postHandler))
web.CPMux.Handle(pat.Post("/notifications/general/"), web.RequireGuildChannelsMiddleware(postHandler))
}
func HandleNotificationsGet(w http.ResponseWriter, r *http.Request) interface{} {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
formConfig, ok := ctx.Value(common.ContextKeyParsedForm).(*Config)
if ok {
templateData["NotifyConfig"] = formConfig
} else {
conf, err := GetConfig(activeGuild.ID)
if err != nil {
web.CtxLogger(r.Context()).WithError(err).Error("failed retrieving config")
}
templateData["NotifyConfig"] = conf
}
return templateData
}
func HandleNotificationsPost(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/notifications/general/"
newConfig := ctx.Value(common.ContextKeyParsedForm).(*Config)
newConfig.GuildID = activeGuild.ID
err := configstore.SQL.SetGuildConfig(ctx, newConfig)
if err != nil {
return templateData, nil
}
return templateData, nil
}
var _ web.PluginWithServerHomeWidget = (*Plugin)(nil)
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ag, templateData := web.GetBaseCPContextData(r.Context())
templateData["WidgetTitle"] = "General notifications"
templateData["SettingsPath"] = "/notifications/general"
config, err := GetConfig(ag.ID)
if err != nil {
return templateData, err
}
format := `<ul>
<li>Join Server message: %s</li>
<li>Join DM message: %s</li>
<li>Leave message: %s</li>
<li>Topic change message: %s</li>
</ul>`
if config.JoinServerEnabled || config.JoinDMEnabled || config.LeaveEnabled || config.TopicEnabled {
templateData["WidgetEnabled"] = true
} else {
templateData["WidgetDisabled"] = true
}
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format,
web.EnabledDisabledSpanStatus(config.JoinServerEnabled), web.EnabledDisabledSpanStatus(config.JoinDMEnabled),
web.EnabledDisabledSpanStatus(config.LeaveEnabled), web.EnabledDisabledSpanStatus(config.TopicEnabled)))
return templateData, nil
}
func enabledDisabled(b bool) string {
if b {
return "enabled"
}
return "disabled"
}