forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
243 lines (205 loc) · 6.61 KB
/
group.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package flows
import (
"fmt"
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// Group represents a grouping of contacts. It can be static (contacts are added and removed manually through
// [actions](#action:add_contact_groups)) or dynamic (contacts are added automatically by a query). It renders as its name in a
// template, and has the following properties which can be accessed:
//
// * `uuid` the UUID of the group
// * `name` the name of the group
//
// Examples:
//
// @contact.groups -> ["Testers","Males"]
// @contact.groups.0.uuid -> b7cf0d83-f1c9-411c-96fd-c511a4cfa86d
// @contact.groups.1.name -> Males
// @(json(contact.groups.1)) -> {"name":"Males","uuid":"4f1f98fc-27a7-4a69-bbdb-24744ba739a9"}
//
// @context group
type Group struct {
assets.Group
parsedQuery *contactql.ContactQuery
}
// NewGroup returns a new group object from the given group asset
func NewGroup(asset assets.Group) *Group {
return &Group{Group: asset}
}
// Asset returns the underlying asset
func (g *Group) Asset() assets.Group { return g.Group }
// ParsedQuery returns the parsed query of a dynamic group (cached)
func (g *Group) ParsedQuery() (*contactql.ContactQuery, error) {
if g.Query() != "" && g.parsedQuery == nil {
var err error
if g.parsedQuery, err = contactql.ParseQuery(g.Query()); err != nil {
return nil, err
}
}
return g.parsedQuery, nil
}
// IsDynamic returns whether this group is dynamic
func (g *Group) IsDynamic() bool { return g.Query() != "" }
// CheckDynamicMembership returns whether the given contact belongs in this dynamic group
func (g *Group) CheckDynamicMembership(env utils.Environment, contact *Contact) (bool, error) {
if !g.IsDynamic() {
return false, fmt.Errorf("can't check membership on a non-dynamic group")
}
parsedQuery, err := g.ParsedQuery()
if err != nil {
return false, err
}
return contactql.EvaluateQuery(env, parsedQuery, contact)
}
// Reference returns a reference to this group
func (g *Group) Reference() *assets.GroupReference {
return assets.NewGroupReference(g.UUID(), g.Name())
}
// Resolve resolves the given key when this group is referenced in an expression
func (g *Group) Resolve(env utils.Environment, key string) types.XValue {
switch key {
case "uuid":
return types.NewXText(string(g.UUID()))
case "name":
return types.NewXText(g.Name())
}
return types.NewXResolveError(g, key)
}
// Describe returns a representation of this type for error messages
func (g *Group) Describe() string { return "group" }
// Reduce is called when this object needs to be reduced to a primitive
func (g *Group) Reduce(env utils.Environment) types.XPrimitive { return types.NewXText(g.Name()) }
// ToXJSON is called when this type is passed to @(json(...))
func (g *Group) ToXJSON(env utils.Environment) types.XText {
return types.ResolveKeys(env, g, "uuid", "name").ToXJSON(env)
}
var _ types.XValue = (*Group)(nil)
var _ types.XResolvable = (*Group)(nil)
// GroupList defines a contact's list of groups
type GroupList struct {
groups []*Group
}
// NewGroupList creates a new group list
func NewGroupList(groups []*Group) *GroupList {
return &GroupList{groups: groups}
}
// NewGroupListFromAssets creates a new group list
func NewGroupListFromAssets(a SessionAssets, groupAssets []assets.Group) (*GroupList, error) {
groups := make([]*Group, len(groupAssets))
var err error
for g, asset := range groupAssets {
if groups[g], err = a.Groups().Get(asset.UUID()); err != nil {
return nil, err
}
}
return &GroupList{groups: groups}, nil
}
// Clone returns a clone of this group list
func (l *GroupList) clone() *GroupList {
groups := make([]*Group, len(l.groups))
copy(groups, l.groups)
return NewGroupList(groups)
}
// FindByUUID returns the group with the passed in UUID or nil if not found
func (l *GroupList) FindByUUID(uuid assets.GroupUUID) *Group {
for _, group := range l.groups {
if group.UUID() == uuid {
return group
}
}
return nil
}
// Add adds the given group to this group list
func (l *GroupList) Add(group *Group) bool {
if l.FindByUUID(group.UUID()) == nil {
l.groups = append(l.groups, group)
return true
}
return false
}
// Remove removes the given group from this group list
func (l *GroupList) Remove(group *Group) bool {
for g := range l.groups {
if l.groups[g].UUID() == group.UUID() {
l.groups = append(l.groups[:g], l.groups[g+1:]...)
return true
}
}
return false
}
// All returns all groups in this group list
func (l *GroupList) All() []*Group {
return l.groups
}
// Count returns the number of groups in this group list
func (l *GroupList) Count() int {
return len(l.groups)
}
// Index is called when this object is indexed into in an expression
func (l *GroupList) Index(index int) types.XValue {
return l.groups[index]
}
// Length is called when the length of this object is requested in an expression
func (l *GroupList) Length() int {
return len(l.groups)
}
// Describe returns a representation of this type for error messages
func (l GroupList) Describe() string { return "groups" }
// Reduce is called when this object needs to be reduced to a primitive
func (l GroupList) Reduce(env utils.Environment) types.XPrimitive {
array := types.NewXArray()
for _, group := range l.groups {
array.Append(group)
}
return array
}
// ToXJSON is called when this type is passed to @(json(...))
func (l GroupList) ToXJSON(env utils.Environment) types.XText {
return l.Reduce(env).ToXJSON(env)
}
var _ types.XValue = (*GroupList)(nil)
var _ types.XIndexable = (*GroupList)(nil)
// GroupAssets provides access to all group assets
type GroupAssets struct {
all []*Group
byUUID map[assets.GroupUUID]*Group
}
// NewGroupAssets creates a new set of group assets
func NewGroupAssets(groups []assets.Group) *GroupAssets {
s := &GroupAssets{
all: make([]*Group, len(groups)),
byUUID: make(map[assets.GroupUUID]*Group, len(groups)),
}
for g, asset := range groups {
group := NewGroup(asset)
s.all[g] = group
s.byUUID[group.UUID()] = group
}
return s
}
// All returns all the groups
func (s *GroupAssets) All() []*Group {
return s.all
}
// Get returns the group with the given UUID
func (s *GroupAssets) Get(uuid assets.GroupUUID) (*Group, error) {
c, found := s.byUUID[uuid]
if !found {
return nil, fmt.Errorf("no such group with uuid '%s'", uuid)
}
return c, nil
}
// FindByName looks for a group with the given name (case-insensitive)
func (s *GroupAssets) FindByName(name string) *Group {
name = strings.ToLower(name)
for _, group := range s.all {
if strings.ToLower(group.Name()) == name {
return group
}
}
return nil
}