-
Notifications
You must be signed in to change notification settings - Fork 20
/
base.go
187 lines (156 loc) · 5.7 KB
/
base.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package triggers
import (
"encoding/json"
"time"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
type readFunc func(flows.SessionAssets, json.RawMessage, assets.MissingCallback) (flows.Trigger, error)
var registeredTypes = map[string]readFunc{}
// RegisterType registers a new type of trigger
func RegisterType(name string, f readFunc) {
registeredTypes[name] = f
}
type baseTrigger struct {
type_ string
environment utils.Environment
flow *assets.FlowReference
contact *flows.Contact
connection *flows.Connection
params types.XValue
triggeredOn time.Time
}
func newBaseTrigger(typeName string, env utils.Environment, flow *assets.FlowReference, contact *flows.Contact, connection *flows.Connection, params types.XValue) baseTrigger {
return baseTrigger{type_: typeName, environment: env, flow: flow, contact: contact, connection: connection, params: params, triggeredOn: utils.Now()}
}
// Type returns the type of this trigger
func (t *baseTrigger) Type() string { return t.type_ }
func (t *baseTrigger) Environment() utils.Environment { return t.environment }
func (t *baseTrigger) Flow() *assets.FlowReference { return t.flow }
func (t *baseTrigger) Contact() *flows.Contact { return t.contact }
func (t *baseTrigger) Connection() *flows.Connection { return t.connection }
func (t *baseTrigger) Params() types.XValue { return t.params }
func (t *baseTrigger) TriggeredOn() time.Time { return t.triggeredOn }
// Initialize initializes the session
func (t *baseTrigger) Initialize(session flows.Session, logEvent flows.EventCallback) error {
// try to load the flow
flow, err := session.Assets().Flows().Get(t.Flow().UUID)
if err != nil {
return errors.Wrapf(err, "unable to load %s", t.Flow())
}
if flow.Type() == flows.FlowTypeVoice && t.connection == nil {
return errors.New("unable to trigger voice flow without connection")
}
session.SetType(flow.Type())
session.PushFlow(flow, nil, false)
if t.environment != nil {
session.SetEnvironment(t.environment)
} else {
session.SetEnvironment(utils.NewEnvironmentBuilder().Build())
}
if t.contact != nil {
session.SetContact(t.contact.Clone())
EnsureDynamicGroups(session, logEvent)
}
return nil
}
// InitializeRun performs additional initialization when we create our first run
func (t *baseTrigger) InitializeRun(run flows.FlowRun, logEvent flows.EventCallback) error {
return nil
}
// Context returns the properties available in expressions
func (t *baseTrigger) Context(env utils.Environment) map[string]types.XValue {
return map[string]types.XValue{
"type": types.NewXText(t.type_),
"params": t.params,
}
}
// EnsureDynamicGroups ensures that our session contact is in the correct dynamic groups as
// as far as the engine is concerned
func EnsureDynamicGroups(session flows.Session, logEvent flows.EventCallback) {
allGroups := session.Assets().Groups()
added, removed, errors := session.Contact().ReevaluateDynamicGroups(session.Environment(), allGroups)
// add error event for each group we couldn't re-evaluate
for _, err := range errors {
logEvent(events.NewErrorEvent(err))
}
// add groups changed event for the groups we were added/removed to/from
if len(added) > 0 || len(removed) > 0 {
logEvent(events.NewContactGroupsChangedEvent(added, removed))
}
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type baseTriggerEnvelope struct {
Type string `json:"type" validate:"required"`
Environment json.RawMessage `json:"environment,omitempty"`
Flow *assets.FlowReference `json:"flow" validate:"required"`
Contact json.RawMessage `json:"contact,omitempty"`
Connection *flows.Connection `json:"connection,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
TriggeredOn time.Time `json:"triggered_on" validate:"required"`
}
// ReadTrigger reads a trigger from the given JSON
func ReadTrigger(sessionAssets flows.SessionAssets, data json.RawMessage, missing assets.MissingCallback) (flows.Trigger, error) {
typeName, err := utils.ReadTypeFromJSON(data)
if err != nil {
return nil, err
}
f := registeredTypes[typeName]
if f == nil {
return nil, errors.Errorf("unknown type: '%s'", typeName)
}
return f(sessionAssets, data, missing)
}
func (t *baseTrigger) unmarshal(sessionAssets flows.SessionAssets, e *baseTriggerEnvelope, missing assets.MissingCallback) error {
var err error
t.type_ = e.Type
t.flow = e.Flow
t.connection = e.Connection
t.triggeredOn = e.TriggeredOn
if e.Environment != nil {
if t.environment, err = utils.ReadEnvironment(e.Environment); err != nil {
return errors.Wrap(err, "unable to read environment")
}
}
if e.Contact != nil {
if t.contact, err = flows.ReadContact(sessionAssets, e.Contact, missing); err != nil {
return errors.Wrap(err, "unable to read contact")
}
}
if e.Params != nil {
t.params = types.JSONToXValue(e.Params)
}
return nil
}
func (t *baseTrigger) marshal(e *baseTriggerEnvelope) error {
var err error
e.Type = t.type_
e.Flow = t.flow
e.Connection = t.connection
e.TriggeredOn = t.triggeredOn
if t.environment != nil {
e.Environment, err = json.Marshal(t.environment)
if err != nil {
return err
}
}
if t.contact != nil {
e.Contact, err = json.Marshal(t.contact)
if err != nil {
return err
}
}
if t.params != nil {
e.Params, err = json.Marshal(t.params)
if err != nil {
return err
}
}
return nil
}