Skip to content

Commit

Permalink
don’t encode the whole config in a webhook, use json.Marshall for all…
Browse files Browse the repository at this point in the history
… webhooks
  • Loading branch information
odrling committed Nov 27, 2024
1 parent e63f016 commit 56871c2
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions server/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"
)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
}
]
}`

0 comments on commit 56871c2

Please sign in to comment.