reverse template parsing logic

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2023-03-08 12:39:18 +01:00
parent 4a21a7be5f
commit b3bd2dc7c4
2 changed files with 31 additions and 43 deletions
+18 -30
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"strings"
"text/template"
"time"
@@ -24,6 +25,7 @@ var (
// TODO: from config
_pathToLocales = "/home/jkoberg/ocis/services/userlog/pkg/service/locales"
_domain = "default"
)
// OC10Notification is the oc10 style representation of an event
@@ -265,47 +267,33 @@ func (c *Converter) getUser(ctx context.Context, userID *user.UserId) (*user.Use
}
func composeMessage(nt NotificationTemplate, locale string, vars map[string]interface{}) (string, string, string, string, error) {
subj, msg, err := parseTemplate(nt, locale)
subjectraw, messageraw := loadTemplates(nt, locale)
subject, err := executeTemplate(subjectraw, vars)
if err != nil {
return "", "", "", "", err
}
subject, err := executeTemplate(subj, vars)
if err != nil {
return "", "", "", "", err
}
subjectraw, err := executeTemplate(subj, _placeholders)
if err != nil {
return "", "", "", "", err
}
message, err := executeTemplate(msg, vars)
if err != nil {
return "", "", "", "", err
}
messageraw, err := executeTemplate(msg, _placeholders)
message, err := executeTemplate(messageraw, vars)
return subject, subjectraw, message, messageraw, err
}
func parseTemplate(nt NotificationTemplate, locale string) (*template.Template, *template.Template, error) {
// Create Locale with library path and language code and load domain '.../default.po'
func loadTemplates(nt NotificationTemplate, locale string) (string, string) {
// Create Locale with library path and language code and load default domain
l := gotext.NewLocale(_pathToLocales, locale)
l.AddDomain("default")
subject, err := template.New("").Parse(l.Get(nt.Subject))
if err != nil {
return nil, nil, err
}
message, err := template.New("").Parse(l.Get(nt.Message))
return subject, message, err
l.AddDomain(_domain)
return l.Get(nt.Subject), l.Get(nt.Message)
}
func executeTemplate(tpl *template.Template, vars map[string]interface{}) (string, error) {
func executeTemplate(raw string, vars map[string]interface{}) (string, error) {
for o, n := range _placeholders {
raw = strings.ReplaceAll(raw, o, n)
}
tpl, err := template.New("").Parse(raw)
if err != nil {
return "", err
}
var writer bytes.Buffer
if err := tpl.Execute(&writer, vars); err != nil {
return "", err
+13 -13
View File
@@ -7,50 +7,50 @@ func Template(s string) string { return s }
var (
SpaceShared = NotificationTemplate{
Subject: Template("Space shared"),
Message: Template("{{ .username }} added you to Space {{ .spacename }}"),
Message: Template("{user} added you to Space {space}"),
}
SpaceUnshared = NotificationTemplate{
Subject: Template("Removed from Space"),
Message: Template("{{ .username }} removed you from Space {{ .spacename }}"),
Message: Template("{user} removed you from Space {space}"),
}
SpaceDisabled = NotificationTemplate{
Subject: Template("Space disabled"),
Message: Template("{{ .username }} disabled Space {{ .spacename }}"),
Message: Template("{user} disabled Space {space}"),
}
SpaceDeleted = NotificationTemplate{
Subject: Template("Space deleted"),
Message: Template("{{ .username }} deleted Space {{ .spacename }}"),
Message: Template("{user} deleted Space {space}"),
}
SpaceMembershipExpired = NotificationTemplate{
Subject: Template("Membership expired"),
Message: Template("Access to Space {{ .spacename }} lost"),
Message: Template("Access to Space {space} lost"),
}
ShareCreated = NotificationTemplate{
Subject: Template("Resource shared"),
Message: Template("{{ .username }} shared {{ .resourcename }} with you"),
Message: Template("{user} shared {resource} with you"),
}
ShareRemoved = NotificationTemplate{
Subject: Template("Resource unshared"),
Message: Template("{{ .username }} unshared {{ .resourcename }} with you"),
Message: Template("{user} unshared {resource} with you"),
}
ShareExpired = NotificationTemplate{
Subject: Template("Share expired"),
Message: Template("Access to {{ .resourcename }} expired"),
Message: Template("Access to {resource} expired"),
}
)
// holds the information to link the raw template to the details
var _placeholders = map[string]interface{}{
"username": "{user}",
"spacename": "{space}",
"resourcename": "{resource}",
// holds the information to turn the raw template into a parseable go template
var _placeholders = map[string]string{
"{user}": "{{ .username }}",
"{space}": "{{ .spacename }}",
"{resource}": "{{ .resourcename }}",
}
// NotificationTemplate is the data structure for the notifications