Skip to content

Commit

Permalink
reverse template parsing logic
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Mar 8, 2023
1 parent 9611846 commit 30ba345
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 41 deletions.
44 changes: 16 additions & 28 deletions services/userlog/pkg/service/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"strings"
"text/template"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -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)
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
}
subjectraw, messageraw := loadTemplates(nt, locale)

message, err := executeTemplate(msg, vars)
subject, err := executeTemplate(subjectraw, 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")
l.AddDomain(_domain)
return l.Get(nt.Subject), l.Get(nt.Message)
}

subject, err := template.New("").Parse(l.Get(nt.Subject))
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 nil, nil, err
return "", err
}

message, err := template.New("").Parse(l.Get(nt.Message))
return subject, message, err

}

func executeTemplate(tpl *template.Template, vars map[string]interface{}) (string, error) {
var writer bytes.Buffer
if err := tpl.Execute(&writer, vars); err != nil {
return "", err
Expand Down
26 changes: 13 additions & 13 deletions services/userlog/pkg/service/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 30ba345

Please sign in to comment.