-
Notifications
You must be signed in to change notification settings - Fork 20
/
group.go
205 lines (176 loc) · 5.34 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
package flows
import (
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
"github.com/pkg/errors"
)
// 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:
//
// @(extract(contact.groups, "name")) -> [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, errors.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 {
if g == nil {
return nil
}
return assets.NewGroupReference(g.UUID(), g.Name())
}
// Context is called when this object is accessed in an expression
func (g *Group) Context() types.XValue {
return types.NewXDict(map[string]types.XValue{
"uuid": types.NewXText(string(g.UUID())),
"name": types.NewXText(g.Name()),
})
}
// 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))
for g, asset := range groupAssets {
group := a.Groups().Get(asset.UUID())
if group == nil {
return nil, errors.Errorf("no such group: %s", asset.UUID())
}
groups[g] = group
}
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)
}
// Context is called when this object needs to be reduced to a primitive
func (l GroupList) Context() types.XValue {
array := types.NewXArray()
for _, group := range l.groups {
array.Append(group.Context())
}
return array
}
// 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 {
return s.byUUID[uuid]
}
// 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
}