-
Notifications
You must be signed in to change notification settings - Fork 938
/
tickets_web.go
121 lines (97 loc) · 4.08 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
package tickets
import (
"database/sql"
"fmt"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/tickets/models"
"github.com/jonas747/yagpdb/web"
"github.com/volatiletech/sqlboiler/boil"
"goji.io/pat"
"html/template"
"net/http"
)
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"`
}
func (p *Plugin) InitWeb() {
web.LoadHTMLTemplate("../../tickets/assets/tickets_control_panel.html", "templates/plugins/tickets_control_panel.html")
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{}, "Updated ticket settings")
web.CPMux.Handle(pat.Get("/tickets/settings"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Get("/tickets/settings/"), web.RequireGuildChannelsMiddleware(getHandler))
web.CPMux.Handle(pat.Post("/tickets/settings"), web.RequireGuildChannelsMiddleware(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())
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 := false
if settings != nil {
enabled = true
}
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
}