make email templates configurable

Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
Christian Richter
2022-09-22 09:38:11 +02:00
parent 817ac00393
commit 22369b33f5
4 changed files with 25 additions and 7 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ func Server(cfg *config.Config) *cli.Command {
logger.Fatal().Err(err).Str("addr", cfg.Notifications.RevaGateway).Msg("could not get reva client")
}
svc := service.NewEventsNotifier(evts, channel, logger, gwclient, cfg.Commons.MachineAuthAPIKey)
svc := service.NewEventsNotifier(evts, channel, logger, gwclient, cfg.Commons.MachineAuthAPIKey, cfg.Notifications.EmailTemplatePath)
return svc.Run()
},
}
@@ -26,6 +26,7 @@ type Notifications struct {
Events Events `yaml:"events"`
RevaGateway string `yaml:"reva_gateway" env:"REVA_GATEWAY;NOTIFICATIONS_REVA_GATEWAY" desc:"CS3 gateway used to look up user metadata"`
MachineAuthAPIKey string `yaml:"machine_auth_api_key" env:"OCIS_MACHINE_AUTH_API_KEY;NOTIFICATIONS_MACHINE_AUTH_API_KEY" desc:"Machine auth API key used to validate internal requests necessary to access resources from other services."`
EmailTemplatePath string `yaml:"email_template_path" env:"OCIS_EMAIL_TEMPLATE_PATH;NOTIFICATIONS_EMAIL_TEMPLATE_PATH" desc:"Path to the E-Mail templates for the notifications to override the embedded ones."`
}
// SMTP combines the smtp configuration options.
+18 -4
View File
@@ -4,16 +4,30 @@ import (
"bytes"
"embed"
"html/template"
"path/filepath"
)
// go:embed templates/*.tmpl
// RenderEmailTemplate renders the email template for a new share
func RenderEmailTemplate(templateName string, templateVariables map[string]string) (string, error) {
func RenderEmailTemplate(templateName string, templateVariables map[string]string, emailTemplatePath string) (string, error) {
var fs embed.FS
tpl, err := template.ParseFS(fs, templateName)
if err != nil {
return "", err
var err error
var tpl *template.Template
templateHasBeenFound := false
if emailTemplatePath != "" {
// try to lookup the files in the filesystem
tpl, err = template.ParseFiles(filepath.Join(emailTemplatePath, templateName))
if err == nil {
templateHasBeenFound = true
}
}
if !templateHasBeenFound {
// template has not been found in the fs, or path has not been specified => use embed templates
tpl, err = template.ParseFS(fs, templateName)
if err != nil {
return "", err
}
}
writer := bytes.NewBufferString("")
err = tpl.Execute(writer, templateVariables)
@@ -24,7 +24,8 @@ type Service interface {
Run() error
}
func NewEventsNotifier(events <-chan interface{}, channel channels.Channel, logger log.Logger, gwClient gateway.GatewayAPIClient, machineAuthAPIKey string) Service {
// NewEventsNotifier provides a new eventsNotifier
func NewEventsNotifier(events <-chan interface{}, channel channels.Channel, logger log.Logger, gwClient gateway.GatewayAPIClient, machineAuthAPIKey, emailTemplatePath string) Service {
return eventsNotifier{
logger: logger,
channel: channel,
@@ -32,6 +33,7 @@ func NewEventsNotifier(events <-chan interface{}, channel channels.Channel, logg
signals: make(chan os.Signal, 1),
gwClient: gwClient,
machineAuthAPIKey: machineAuthAPIKey,
emailTemplatePath: emailTemplatePath,
}
}
@@ -42,6 +44,7 @@ type eventsNotifier struct {
signals chan os.Signal
gwClient gateway.GatewayAPIClient
machineAuthAPIKey string
emailTemplatePath string
}
func (s eventsNotifier) Run() error {
@@ -146,7 +149,7 @@ func (s eventsNotifier) handleShareCreated(e events.ShareCreated) {
msg, err := email.RenderEmailTemplate("shareCreated.email.tmpl", map[string]string{
"ShareSharer": userResponse.User.DisplayName,
"ShareFolder": md.Info.Name,
})
}, s.emailTemplatePath)
if err != nil {
s.logger.Error().