Skip to content

Commit

Permalink
Move code to generate templating into flows.Template
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jul 4, 2024
1 parent 8f35095 commit 228c915
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
67 changes: 20 additions & 47 deletions flows/actions/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,29 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow
for _, dest := range destinations {
urn := dest.URN.URN()
channelRef := assets.NewChannelReference(dest.Channel.UUID(), dest.Channel.Name())

var msg *flows.MsgOut

if template != nil {
locales := []i18n.Locale{run.Session().MergedEnvironment().DefaultLocale(), run.Session().Environment().DefaultLocale()}
templateTranslation := template.FindTranslation(dest.Channel, locales)
if templateTranslation != nil {
msg = a.getTemplateMsg(run, urn, channelRef, templateTranslation, unsendableReason, logEvent)
translation := template.FindTranslation(dest.Channel, locales)
if translation != nil {
// TODO in future we won't be localizing template variables
localizedVariables, _ := run.GetTextArray(uuids.UUID(a.UUID()), "template_variables", a.TemplateVariables, nil)

// evaluate the variables
evaluatedVariables := make([]string, len(localizedVariables))
for i, varExp := range localizedVariables {
v, _ := run.EvaluateTemplate(varExp, logEvent)
evaluatedVariables[i] = v
}

templating := template.Templating(translation, evaluatedVariables)

// the message we return is an approximate preview of what the channel will send using the template
preview := translation.Preview(templating.Variables)
locale := translation.Locale()

msg = flows.NewMsgOut(urn, channelRef, preview.Text, preview.Attachments, preview.QuickReplies, templating, flows.NilMsgTopic, locale, unsendableReason)
}
}

Expand All @@ -111,46 +127,3 @@ func (a *SendMsgAction) Execute(run flows.Run, step flows.Step, logModifier flow

return nil
}

// for message actions that specify a template, this generates a mesage with templating information and content that can
// be used as a preview
func (a *SendMsgAction) getTemplateMsg(run flows.Run, urn urns.URN, channelRef *assets.ChannelReference, translation *flows.TemplateTranslation, unsendableReason flows.UnsendableReason, logEvent flows.EventCallback) *flows.MsgOut {
// localize and evaluate the variables
localizedVariables, _ := run.GetTextArray(uuids.UUID(a.UUID()), "template_variables", a.TemplateVariables, nil)
evaluatedVariables := make([]string, len(localizedVariables))
for i, varExp := range localizedVariables {
v, _ := run.EvaluateTemplate(varExp, logEvent)
evaluatedVariables[i] = v
}

// cross-reference with asset to get variable types and filter out invalid values
variables := make([]*flows.TemplatingVariable, len(translation.Variables()))
for i, v := range translation.Variables() {
// we pad out any missing variables with empty values
value := ""
if i < len(evaluatedVariables) {
value = evaluatedVariables[i]
}

variables[i] = &flows.TemplatingVariable{Type: v.Type(), Value: value}
}

// create a list of components that have variables
components := make([]*flows.TemplatingComponent, 0, len(translation.Components()))
for _, comp := range translation.Components() {
if len(comp.Variables()) > 0 {
components = append(components, &flows.TemplatingComponent{
Type: comp.Type(),
Name: comp.Name(),
Variables: comp.Variables(),
})
}
}

// the message we return is an approximate preview of what the channel will send using the template
preview := translation.Preview(variables)
locale := translation.Locale()
templating := flows.NewMsgTemplating(a.Template, components, variables)

return flows.NewMsgOut(urn, channelRef, preview.Text, preview.Attachments, preview.QuickReplies, templating, flows.NilMsgTopic, locale, unsendableReason)
}
28 changes: 28 additions & 0 deletions flows/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ func (t *Template) FindTranslation(channel *Channel, locales []i18n.Locale) *Tem
return candidates[match]
}

func (t *Template) Templating(tt *TemplateTranslation, vars []string) *MsgTemplating {

Check warning on line 53 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L53

Added line #L53 was not covered by tests
// cross-reference with asset to get variable types and filter out invalid values
variables := make([]*TemplatingVariable, len(tt.Variables()))
for i, v := range tt.Variables() {

Check warning on line 56 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L55-L56

Added lines #L55 - L56 were not covered by tests
// we pad out any missing variables with empty values
value := ""
if i < len(vars) {
value = vars[i]

Check warning on line 60 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L58-L60

Added lines #L58 - L60 were not covered by tests
}

variables[i] = &TemplatingVariable{Type: v.Type(), Value: value}

Check warning on line 63 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L63

Added line #L63 was not covered by tests
}

// create a list of components that have variables
components := make([]*TemplatingComponent, 0, len(tt.Components()))
for _, comp := range tt.Components() {
if len(comp.Variables()) > 0 {
components = append(components, &TemplatingComponent{
Type: comp.Type(),
Name: comp.Name(),
Variables: comp.Variables(),
})

Check warning on line 74 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L67-L74

Added lines #L67 - L74 were not covered by tests
}
}

return NewMsgTemplating(t.Reference(), components, variables)

Check warning on line 78 in flows/template.go

View check run for this annotation

Codecov / codecov/patch

flows/template.go#L78

Added line #L78 was not covered by tests
}

// TemplateTranslation represents a single translation for a template
type TemplateTranslation struct {
assets.TemplateTranslation
Expand Down

0 comments on commit 228c915

Please sign in to comment.