Skip to content

Commit

Permalink
Merge pull request #146 from nyaruka/tweaks_to_match_rp
Browse files Browse the repository at this point in the history
Tweaks to match RapidPro
  • Loading branch information
rowanseymour authored Feb 3, 2018
2 parents eee202a + 7e9a30f commit 33f9c52
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions flows/actions/send_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"fmt"
"regexp"
"strings"

"github.com/nyaruka/goflow/excellent"
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 5 additions & 3 deletions flows/actions/update_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,16 +27,16 @@ 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
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
}
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions flows/definition/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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{
Expand Down
13 changes: 8 additions & 5 deletions flows/events/update_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 33f9c52

Please sign in to comment.