From c2c3e7d39b2e26827ec738719aad0353d2cdffdc Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 2 Feb 2018 16:53:06 -0500 Subject: [PATCH 1/2] Tweaks to make action output match that of rapidpro --- flows/actions/send_email.go | 4 ++++ flows/actions/update_contact.go | 2 ++ flows/definition/legacy.go | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/flows/actions/send_email.go b/flows/actions/send_email.go index 5921c64f3..ee80d5cdd 100644 --- a/flows/actions/send_email.go +++ b/flows/actions/send_email.go @@ -2,6 +2,7 @@ package actions import ( "fmt" + "regexp" "strings" "github.com/nyaruka/goflow/excellent" @@ -54,6 +55,9 @@ func (a *EmailAction) Execute(run flows.FlowRun, step flows.Step, log flows.Even return nil } + // make sure the subject is single line - replace '\t\n\r\f\v' to ' ' + subject = regexp.MustCompile(`\s+`).ReplaceAllString(subject, " ") + body, err := excellent.EvaluateTemplateAsString(run.Environment(), run.Context(), a.Body, false) if err != nil { log.Add(events.NewErrorEvent(err)) diff --git a/flows/actions/update_contact.go b/flows/actions/update_contact.go index 0cbe32fdf..579f9fae9 100644 --- a/flows/actions/update_contact.go +++ b/flows/actions/update_contact.go @@ -5,6 +5,7 @@ import ( "github.com/nyaruka/goflow/flows" "github.com/nyaruka/goflow/flows/events" "github.com/nyaruka/goflow/utils" + "strings" ) // TypeUpdateContact is the type for our update contact action @@ -53,6 +54,7 @@ func (a *UpdateContactAction) Execute(run flows.FlowRun, step flows.Step, log fl // get our localized value if any template := run.GetText(flows.UUID(a.UUID()), "value", a.Value) value, err := excellent.EvaluateTemplateAsString(run.Environment(), run.Context(), template, false) + value = strings.TrimSpace(value) // if we received an error, log it if err != nil { diff --git a/flows/definition/legacy.go b/flows/definition/legacy.go index dd488f079..c4a747479 100644 --- a/flows/definition/legacy.go +++ b/flows/definition/legacy.go @@ -548,6 +548,7 @@ func migrateAction(baseLanguage utils.Language, a legacyAction, translations *fl if a.Field == "name" || a.Field == "first_name" { // we can emulate setting only the first name with an expression if a.Field == "first_name" { + migratedValue = strings.TrimSpace(migratedValue) migratedValue = fmt.Sprintf("%s @(word_slice(contact.name, 2, -1))", migratedValue) } @@ -565,6 +566,12 @@ func migrateAction(baseLanguage utils.Language, a legacyAction, translations *fl Path: migratedValue, BaseAction: actions.NewBaseAction(a.UUID), }, nil + } else if a.Field == "tel_e164" { + return &actions.AddURNAction{ + Scheme: "tel", + Path: migratedValue, + BaseAction: actions.NewBaseAction(a.UUID), + }, nil } return &actions.SaveContactField{ From 7e9a30fd60ca703dede59bfa4cf07d847c97e843 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 2 Feb 2018 17:04:10 -0500 Subject: [PATCH 2/2] Allow blank values in UpdateContact actions so we can clear things like language --- flows/actions/update_contact.go | 6 +++--- flows/events/update_contact.go | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/flows/actions/update_contact.go b/flows/actions/update_contact.go index 579f9fae9..479dcefec 100644 --- a/flows/actions/update_contact.go +++ b/flows/actions/update_contact.go @@ -27,7 +27,7 @@ const TypeUpdateContact string = "update_contact" type UpdateContactAction struct { BaseAction FieldName string `json:"field_name" validate:"required,eq=name|eq=language"` - Value string `json:"value" validate:"required"` + Value string `json:"value"` } // Type returns the type of this action @@ -35,8 +35,8 @@ func (a *UpdateContactAction) Type() string { return TypeUpdateContact } // Validate validates our action is valid and has all the assets it needs func (a *UpdateContactAction) Validate(assets flows.SessionAssets) error { - // check language is valid - if a.FieldName == "language" { + // check language is valid if specified + if a.FieldName == "language" && a.Value != "" { if _, err := utils.ParseLanguage(a.Value); err != nil { return err } diff --git a/flows/events/update_contact.go b/flows/events/update_contact.go index ca81569c0..a109b80a0 100644 --- a/flows/events/update_contact.go +++ b/flows/events/update_contact.go @@ -44,12 +44,15 @@ func (e *UpdateContactEvent) Apply(run flows.FlowRun) error { if e.FieldName == "name" { run.Contact().SetName(e.Value) } else { - lang, err := utils.ParseLanguage(e.Value) - if err != nil { - return err + if e.Value != "" { + lang, err := utils.ParseLanguage(e.Value) + if err != nil { + return err + } + run.Contact().SetLanguage(lang) + } else { + run.Contact().SetLanguage(utils.NilLanguage) } - - run.Contact().SetLanguage(lang) } return run.Contact().UpdateDynamicGroups(run.Session())