From 56871c2b96fc27912b0b42f51beda239921629c0 Mon Sep 17 00:00:00 2001 From: odrling Date: Mon, 18 Nov 2024 11:49:03 +0100 Subject: [PATCH] =?UTF-8?q?don=E2=80=99t=20encode=20the=20whole=20config?= =?UTF-8?q?=20in=20a=20webhook,=20use=20json.Marshall=20for=20all=20webhoo?= =?UTF-8?q?ks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/webhooks.go | 80 ++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 35 deletions(-) diff --git a/server/webhooks.go b/server/webhooks.go index fb5bf855..991c9209 100644 --- a/server/webhooks.go +++ b/server/webhooks.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "html/template" "net/http" "strings" ) @@ -15,10 +14,11 @@ type Webhook struct { } type WebhookTemplateContext struct { - Config KaraberusConfig Kara KaraInfoDB + Server string Title string Description string + Resource string } func parseWebhooksConfig() []Webhook { @@ -41,7 +41,15 @@ func PostWebhooks(kara KaraInfoDB) { getLogger().Printf("error generating description for webhooks: %s", err) return } - tmplCtx := &WebhookTemplateContext{CONFIG, kara, title, desc} + + tmplCtx := WebhookTemplateContext{ + Kara: kara, + Server: CONFIG.Listen.Addr(), + Title: title, + Description: desc, + Resource: fmt.Sprintf("%s/karaoke/browse/%d", CONFIG.Listen.Addr(), kara.ID), + } + for _, webhook := range parseWebhooksConfig() { var err error switch webhook.Type { @@ -58,7 +66,7 @@ func PostWebhooks(kara KaraInfoDB) { } } -func postJsonWebhook(url string, tmplCtx *WebhookTemplateContext) error { +func postJsonWebhook(url string, tmplCtx WebhookTemplateContext) error { b, err := json.Marshal(tmplCtx) if err != nil { return err @@ -71,43 +79,45 @@ func postJsonWebhook(url string, tmplCtx *WebhookTemplateContext) error { return nil } -func postDiscordWebhook(url string, tmplCtx *WebhookTemplateContext) error { - tmpl, err := template.New("discord_template").Funcs(funcs).Parse(discordTemplate) - if err != nil { - return err +type DiscordEmbedAuthor struct { + Name string `json:"name"` + IconURL string `json:"icon_url"` +} + +type DiscordEmbed struct { + Author DiscordEmbedAuthor `json:"author,omitempty"` + Title string `json:"title"` + URL string `json:"url"` + Description string `json:"description"` + Color uint `json:"color"` +} + +type DiscordWebhook struct { + Embeds []DiscordEmbed `json:"embeds"` +} + +func postDiscordWebhook(url string, tmplCtx WebhookTemplateContext) error { + webhook_data := DiscordWebhook{ + Embeds: []DiscordEmbed{DiscordEmbed{ + Author: DiscordEmbedAuthor{ + Name: "New Karaoke!", + IconURL: fmt.Sprintf("%s/vite.svg", tmplCtx.Server), + }, + Title: tmplCtx.Title, + URL: tmplCtx.Resource, + Description: tmplCtx.Description, + Color: 10053324, + }}, } - var buf bytes.Buffer - if err := tmpl.Execute(&buf, tmplCtx); err != nil { + + body, err := json.Marshal(webhook_data) + if err != nil { return err } - resp, err := http.Post(url, "application/json", &buf) + resp, err := http.Post(url, "application/json", bytes.NewBuffer(body)) if err != nil { return err } defer Closer(resp.Body) return nil } - -var funcs = template.FuncMap{ - "jsstr": func(s string) template.JSStr { - return template.JSStr(strings.ReplaceAll(s, "\n", "\\n")) - }, - "join": func(sep string, elems []string) string { - return strings.Join(elems, sep) - }, -} - -const discordTemplate = `{ - "embeds": [ - { - "author": { - "name": "New Karaoke!", - "icon_url": "{{ .Config.Listen.BaseURL }}/vite.svg" - }, - "title": "{{ .Title | jsstr }}", - "url": "{{ .Config.Listen.BaseURL }}/karaoke/browse/{{ .Kara.ID }}", - "description": "{{ .Description | jsstr }}", - "color": 10053324 - } - ] -}`