-
Notifications
You must be signed in to change notification settings - Fork 921
/
plugin_web.go
117 lines (89 loc) · 3.43 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
package notifications
import (
_ "embed"
"fmt"
"html/template"
"net/http"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/configstore"
"github.com/botlabs-gg/yagpdb/v2/common/cplogs"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/web"
"goji.io/pat"
)
//go:embed assets/notifications_general.html
var PageHTML string
var panelLogKey = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "notifications_settings", FormatString: "Updated server notification settings"})
func (p *Plugin) InitWeb() {
web.AddHTMLTemplate("notifications/assets/notifications_general.html", PageHTML)
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{})
web.CPMux.Handle(pat.Get("/notifications/general"), getHandler)
web.CPMux.Handle(pat.Get("/notifications/general/"), getHandler)
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{} {
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
}
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKey))
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"
}