-
Notifications
You must be signed in to change notification settings - Fork 20
/
language.go
52 lines (41 loc) · 1.55 KB
/
language.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package modifiers
import (
"encoding/json"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
)
func init() {
RegisterType(TypeLanguage, readLanguageModifier)
}
// TypeLanguage is the type of our language modifier
const TypeLanguage string = "language"
// LanguageModifier modifies the language of a contact
type LanguageModifier struct {
baseModifier
Language utils.Language `json:"language"`
}
// NewLanguageModifier creates a new language modifier
func NewLanguageModifier(language utils.Language) *LanguageModifier {
return &LanguageModifier{
baseModifier: newBaseModifier(TypeLanguage),
Language: language,
}
}
// Apply applies this modification to the given contact
func (m *LanguageModifier) Apply(env utils.Environment, assets flows.SessionAssets, contact *flows.Contact, log flows.EventCallback) {
if contact.Language() != m.Language {
contact.SetLanguage(m.Language)
log(events.NewContactLanguageChangedEvent(m.Language))
m.reevaluateDynamicGroups(env, assets, contact, log)
}
}
var _ flows.Modifier = (*LanguageModifier)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
func readLanguageModifier(assets flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Modifier, error) {
m := &LanguageModifier{}
return m, utils.UnmarshalAndValidate(data, m)
}