From 0652aa73d45d44ce9a11fb25cb487b2a8436d8f1 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sun, 24 Apr 2022 16:10:59 +0200 Subject: [PATCH] updated notification mail to use HTML template. Ready for tag v2.0.0 --- app/admin/dashboard.html | 4 +-- app/notifier/notification.html | 34 +++++++++++++++++++++ app/notifier/notifier.go | 47 ++++++++++++++++++++++++----- app/notifier/notifier_test.go | 14 ++------- app/webmention/recv/receive_test.go | 2 +- 5 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 app/notifier/notification.html diff --git a/app/admin/dashboard.html b/app/admin/dashboard.html index 1d66ea4..94afb9f 100644 --- a/app/admin/dashboard.html +++ b/app/admin/dashboard.html @@ -40,8 +40,8 @@ {{ range $mentions }} - {{ .Source }} - {{ .Target }} + {{ .Source }} + {{ .Target }} {{ .Content }} ✅ Yes! ❌ Nop! diff --git a/app/notifier/notification.html b/app/notifier/notification.html new file mode 100644 index 0000000..a449303 --- /dev/null +++ b/app/notifier/notification.html @@ -0,0 +1,34 @@ + + + + + Webmention in moderation from {{ .SourceDomain }} + + + + +

🥞 Webmention in moderation from {{ .SourceDomain }}

+ +

Hi Admin, a webmention was received:

+ +

+ Source: {{ .Source }}
+ Target: {{ .Target }}
+ Content: {{ .Content }} +

+ +

+ ✅ Accept!
+ ❌ Reject! +

+ +

+ Cheerio, your Go-Jammin' Thing! (Go to Admin Dashboard) +

+ + \ No newline at end of file diff --git a/app/notifier/notifier.go b/app/notifier/notifier.go index 575a46d..e741116 100644 --- a/app/notifier/notifier.go +++ b/app/notifier/notifier.go @@ -3,22 +3,55 @@ package notifier import ( "brainbaking.com/go-jamming/app/mf" "brainbaking.com/go-jamming/common" + "bytes" "fmt" + "github.com/rs/zerolog/log" + "text/template" ) +import _ "embed" + +//go:embed notification.html +var notificationTmplBytes []byte +var notificationTmpl *template.Template + +func init() { + var err error + notificationTmpl, err = template.New("notification").Parse(string(notificationTmplBytes)) + if err != nil { + log.Fatal().Err(err).Str("name", "notification").Msg("Template invalid") + } +} + +type notificationData struct { + SourceDomain string + Source string + Content string + Target string + AdminURL string + ApproveURL string + RejectURL string +} + type Notifier interface { NotifyReceived(wm mf.Mention, data *mf.IndiewebData) } -// BuildNotification returns a string representation of the Mention to notify the admin. +// BuildNotification returns a HTML (string template) representation of the Mention to notify the admin. func BuildNotification(wm mf.Mention, data *mf.IndiewebData, cnf *common.Config) string { - enter := "\n" acceptUrl := fmt.Sprintf("%sadmin/approve/%s/%s", cnf.BaseURL, cnf.Token, wm.Key()) rejectUrl := fmt.Sprintf("%sadmin/reject/%s/%s", cnf.BaseURL, cnf.Token, wm.Key()) + adminUrl := fmt.Sprintf("%sadmin/%s", cnf.BaseURL, cnf.Token) - return fmt.Sprintf("Hi admin, %s%s,A webmention was received: %sSource %s, Target %s%sContent: %s%s%sAccept? %s%sReject? %s%sCheerio, your go-jammin' thing.", - enter, enter, enter, - wm.Source, wm.Target, enter, - data.Content, enter, enter, - acceptUrl, enter, rejectUrl, enter) + var buff bytes.Buffer + notificationTmpl.Execute(&buff, notificationData{ + Source: wm.Source, + Target: wm.Target, + Content: data.Content, + SourceDomain: wm.SourceDomain(), + ApproveURL: acceptUrl, + RejectURL: rejectUrl, + AdminURL: adminUrl, + }) + return buff.String() } diff --git a/app/notifier/notifier_test.go b/app/notifier/notifier_test.go index 299e905..2cde63a 100644 --- a/app/notifier/notifier_test.go +++ b/app/notifier/notifier_test.go @@ -22,16 +22,8 @@ func TestBuildNotification(t *testing.T) { Whitelist: []string{}, } - expected := `Hi admin, - -,A webmention was received: -Source https://brainbaking.com/valid-indieweb-source.html, Target https://brainbaking.com/valid-indieweb-target.html -Content: somecontent - -Accept? https://jam.brainbaking.com/admin/approve/mytoken/19d462ddff3c3322c662dac3461324bb:brainbaking.com -Reject? https://jam.brainbaking.com/admin/reject/mytoken/19d462ddff3c3322c662dac3461324bb:brainbaking.com -Cheerio, your go-jammin' thing.` - result := BuildNotification(wm, &mf.IndiewebData{Content: "somecontent"}, cnf) - assert.Equal(t, result, expected) + assert.Contains(t, result, `Source: https://brainbaking.com/valid-indieweb-source.html
`) + assert.Contains(t, result, `Target: https://brainbaking.com/valid-indieweb-target.html
`) + assert.Contains(t, result, `