forked from wgroeneveld/go-jamming
updated notification mail to use HTML template. Ready for tag v2.0.0
This commit is contained in:
parent
e361567eed
commit
0652aa73d4
|
@ -40,8 +40,8 @@
|
|||
<tbody>
|
||||
{{ range $mentions }}
|
||||
<tr>
|
||||
<td>{{ .Source }}</td>
|
||||
<td>{{ .Target }}</td>
|
||||
<td><a href="{{ .Source }}" target="_blank">{{ .Source }}</a></td>
|
||||
<td><a href="{{ .Target }}" target="_blank">{{ .Target }}</a></td>
|
||||
<td>{{ .Content }}</td>
|
||||
<td><a href="{{ .ApproveURL }}">✅ Yes!</a></td>
|
||||
<td><a href="{{ .RejectURL }}">❌ Nop!</a></td>
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Webmention in moderation from {{ .SourceDomain }}</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
||||
margin: 1em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>🥞 Webmention in moderation from {{ .SourceDomain }}</h1>
|
||||
|
||||
<p>Hi Admin, a webmention was received:</p>
|
||||
|
||||
<p>
|
||||
<em>Source:</em> <a href="{{ .Source }}">{{ .Source }}</a><br/>
|
||||
<em>Target:</em> <a href="{{ .Target }}">{{ .Target }}</a><br/>
|
||||
<em>Content:</em> {{ .Content }}
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<a href="{{ .ApproveURL }}">✅ Accept!</a><br/>
|
||||
<a href="{{ .RejectURL }}">❌ Reject!</a>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
Cheerio, your Go-Jammin' Thing! (<a href="{{ .AdminURL }}">Go to Admin Dashboard</a>)
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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, `<em>Source:</em> <a href="https://brainbaking.com/valid-indieweb-source.html">https://brainbaking.com/valid-indieweb-source.html</a><br/>`)
|
||||
assert.Contains(t, result, `<em>Target:</em> <a href="https://brainbaking.com/valid-indieweb-target.html">https://brainbaking.com/valid-indieweb-target.html</a><br/>`)
|
||||
assert.Contains(t, result, `<a href="https://jam.brainbaking.com/admin/approve/mytoken/19d462ddff3c3322c662dac3461324bb:brainbaking.com`)
|
||||
}
|
||||
|
|
|
@ -269,7 +269,7 @@ func TestReceiveFromNotInWhitelistSavesInModerationAndNotifies(t *testing.T) {
|
|||
receiver.Receive(wm)
|
||||
assert.Empty(t, repo.GetAll("brainbaking.com").Data)
|
||||
assert.Equal(t, 1, len(repo.GetAllToModerate("brainbaking.com").Data))
|
||||
assert.Contains(t, notifierMock.Output, "Accept?")
|
||||
assert.Contains(t, notifierMock.Output, "✅ Accept!")
|
||||
}
|
||||
|
||||
func TestReceiveFromBlacklistedDomainDoesNothing(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue