forked from wgroeneveld/go-jamming
parent
2e504eaa65
commit
6cc83620ba
@ -0,0 +1,67 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"encoding/base64"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/mail"
|
||||
"net/smtp"
|
||||
)
|
||||
|
||||
type MailNotifier struct {
|
||||
Conf *common.Config
|
||||
}
|
||||
|
||||
// sendMail is a utility function that sends a mail without authentication to localhost. Tested using postfix.
|
||||
// cheers https://github.com/gadelkareem/go-helpers/blob/master/helpers.go
|
||||
func sendMail(from, subject, body, toName, toAddress string) error {
|
||||
c, err := smtp.Dial("127.0.0.1:25")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer c.Close()
|
||||
if err = c.Mail(from); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
to := (&mail.Address{toName, toAddress}).String()
|
||||
if err = c.Rcpt(to); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w, err := c.Data()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msg := "To: " + to + "\r\n" +
|
||||
"From: " + from + "\r\n" +
|
||||
"Subject: " + subject + "\r\n" +
|
||||
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
|
||||
"Content-Transfer-Encoding: base64\r\n" +
|
||||
"\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
|
||||
|
||||
_, err = w.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = w.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.Quit()
|
||||
}
|
||||
|
||||
func (mn *MailNotifier) NotifyReceived(wm mf.Mention, indieweb *mf.IndiewebData) {
|
||||
err := sendMail(
|
||||
"admin@brainbaking.com",
|
||||
"Webmention in moderation from "+wm.SourceDomain(),
|
||||
BuildNotification(wm, indieweb, mn.Conf),
|
||||
"Go-Jamming User",
|
||||
"wouter@brainbaking.com")
|
||||
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Unable to send notification mail, check localhost postfix settings?")
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Notifier interface {
|
||||
NotifyReceived(wm mf.Mention, data *mf.IndiewebData)
|
||||
}
|
||||
|
||||
// BuildNotification returns a string 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())
|
||||
|
||||
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)
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildNotification(t *testing.T) {
|
||||
wm := mf.Mention{
|
||||
Source: "https://brainbaking.com/valid-indieweb-source.html",
|
||||
Target: "https://brainbaking.com/valid-indieweb-target.html",
|
||||
}
|
||||
cnf := &common.Config{
|
||||
AllowedWebmentionSources: []string{
|
||||
"brainbaking.com",
|
||||
},
|
||||
BaseURL: "https://jam.brainbaking.com/",
|
||||
Token: "mytoken",
|
||||
Blacklist: []string{},
|
||||
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)
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/app/notifier"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
)
|
||||
|
||||
type StringNotifier struct {
|
||||
Output string
|
||||
Conf *common.Config
|
||||
}
|
||||
|
||||
func (sn *StringNotifier) NotifyReceived(wm mf.Mention, indieweb *mf.IndiewebData) {
|
||||
sn.Output = notifier.BuildNotification(wm, indieweb, sn.Conf)
|
||||
}
|
Loading…
Reference in new issue