-
Notifications
You must be signed in to change notification settings - Fork 927
/
tickets_web.go
133 lines (104 loc) · 4.37 KB
/
tickets_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
package tickets
import (
"database/sql"
_ "embed"
"fmt"
"html/template"
"net/http"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/cplogs"
"github.com/botlabs-gg/yagpdb/v2/tickets/models"
"github.com/botlabs-gg/yagpdb/v2/web"
"github.com/volatiletech/sqlboiler/boil"
"goji.io/pat"
)
//go:embed assets/tickets_control_panel.html
var PageHTML string
type FormData struct {
GuildID int64
Enabled bool
TicketsChannelCategory int64 `valid:"channel,true"`
TicketsTranscriptsChannel int64 `valid:"channel,true"`
TicketsTranscriptsChannelAdminOnly int64 `valid:"channel,true"`
StatusChannel int64 `valid:"channel,true"`
TicketsUseTXTTranscripts bool
DownloadAttachments bool
ModRoles []int64 `valid:"role"`
AdminRoles []int64 `valid:"role"`
TicketOpenMSG string `valid:"template,10000"`
}
var panelLogKey = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "tickets_updated_settings", FormatString: "Updated ticket settings"})
func (p *Plugin) InitWeb() {
web.AddHTMLTemplate("tickets_control_panel.html", PageHTML)
web.AddSidebarItem(web.SidebarCategoryTools, &web.SidebarItem{
Name: "Ticket System",
URL: "tickets/settings",
Icon: "fas fa-ticket-alt",
})
getHandler := web.ControllerHandler(p.handleGetSettings, "cp_tickets_settings")
postHandler := web.ControllerPostHandler(p.handlePostSettings, getHandler, FormData{})
web.CPMux.Handle(pat.Get("/tickets/settings"), getHandler)
web.CPMux.Handle(pat.Get("/tickets/settings/"), getHandler)
web.CPMux.Handle(pat.Post("/tickets/settings"), postHandler)
}
func (p *Plugin) handleGetSettings(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
settings, err := models.FindTicketConfigG(ctx, activeGuild.ID)
if err != nil {
if err != sql.ErrNoRows {
return templateData, err
}
// return standard config
settings = &models.TicketConfig{}
}
templateData["DefaultTicketMessage"] = DefaultTicketMsg
templateData["PluginSettings"] = settings
return templateData, nil
}
func (p *Plugin) handlePostSettings(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
formConfig := ctx.Value(common.ContextKeyParsedForm).(*FormData)
model := &models.TicketConfig{
GuildID: activeGuild.ID,
Enabled: formConfig.Enabled,
TicketsChannelCategory: formConfig.TicketsChannelCategory,
TicketsTranscriptsChannel: formConfig.TicketsTranscriptsChannel,
TicketsTranscriptsChannelAdminOnly: formConfig.TicketsTranscriptsChannelAdminOnly,
StatusChannel: formConfig.StatusChannel,
TicketsUseTXTTranscripts: formConfig.TicketsUseTXTTranscripts,
DownloadAttachments: formConfig.DownloadAttachments,
ModRoles: formConfig.ModRoles,
AdminRoles: formConfig.AdminRoles,
TicketOpenMSG: formConfig.TicketOpenMSG,
}
err := model.UpsertG(ctx, true, []string{"guild_id"}, boil.Infer(), boil.Infer())
if err == nil {
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKey))
}
commands.PubsubSendUpdateSlashCommandsPermissions(activeGuild.ID)
return templateData, err
}
var _ web.PluginWithServerHomeWidget = (*Plugin)(nil)
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
settings, err := models.FindTicketConfigG(r.Context(), activeGuild.ID)
if err != nil && err != sql.ErrNoRows {
return templateData, err
}
enabled := settings != nil && settings.Enabled
templateData["WidgetTitle"] = "Tickets"
templateData["SettingsPath"] = "/tickets/settings"
if enabled {
templateData["WidgetEnabled"] = true
} else {
templateData["WidgetDisabled"] = true
}
const format = `<ul>
<li>Tickets enabled: %s</li>
</ul>`
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format, web.EnabledDisabledSpanStatus(enabled)))
return templateData, nil
}