Skip to content

Commit

Permalink
make email templates configurable
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Sep 15, 2022
1 parent 8ae6fb0 commit a26bbf7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion services/notifications/pkg/command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
}
Expand Down
1 change: 1 addition & 0 deletions services/notifications/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 18 additions & 4 deletions services/notifications/pkg/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions services/notifications/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ 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,
events: events,
signals: make(chan os.Signal, 1),
gwClient: gwClient,
machineAuthAPIKey: machineAuthAPIKey,
emailTemplatePath: emailTemplatePath,
}
}

Expand All @@ -42,6 +44,7 @@ type eventsNotifier struct {
signals chan os.Signal
gwClient gateway.GatewayAPIClient
machineAuthAPIKey string
emailTemplatePath string
}

func (s eventsNotifier) Run() error {
Expand Down Expand Up @@ -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().
Expand Down

0 comments on commit a26bbf7

Please sign in to comment.