2022-04-23 21:27:43 +02:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"brainbaking.com/go-jamming/app/mf"
|
|
|
|
"brainbaking.com/go-jamming/common"
|
2022-04-24 16:10:59 +02:00
|
|
|
"bytes"
|
2022-04-23 21:27:43 +02:00
|
|
|
"fmt"
|
2022-04-24 16:10:59 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"text/template"
|
2022-04-23 21:27:43 +02:00
|
|
|
)
|
|
|
|
|
2022-04-24 16:10:59 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-23 21:27:43 +02:00
|
|
|
type Notifier interface {
|
|
|
|
NotifyReceived(wm mf.Mention, data *mf.IndiewebData)
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:10:59 +02:00
|
|
|
// BuildNotification returns a HTML (string template) representation of the Mention to notify the admin.
|
2022-04-23 21:27:43 +02:00
|
|
|
func BuildNotification(wm mf.Mention, data *mf.IndiewebData, cnf *common.Config) string {
|
|
|
|
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())
|
2022-04-24 16:10:59 +02:00
|
|
|
adminUrl := fmt.Sprintf("%sadmin/%s", cnf.BaseURL, cnf.Token)
|
2022-04-23 21:27:43 +02:00
|
|
|
|
2022-04-24 16:10:59 +02:00
|
|
|
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()
|
2022-04-23 21:27:43 +02:00
|
|
|
}
|