-
Notifications
You must be signed in to change notification settings - Fork 20
/
group.go
202 lines (172 loc) · 5.09 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
package flows
import (
"strings"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
)
// Group adds some functionality to group assets.
type Group struct {
assets.Group
parsedQuery *contactql.ContactQuery
}
// NewGroup returns a new group object from the given group asset
func NewGroup(env envs.Environment, fields *FieldAssets, asset assets.Group) (*Group, error) {
if asset.Query() != "" {
query, err := contactql.ParseQuery(env, asset.Query(), fields)
if err != nil {
return nil, err
}
return &Group{Group: asset, parsedQuery: query}, nil
}
return &Group{Group: asset}, nil
}
// Asset returns the underlying asset
func (g *Group) Asset() assets.Group { return g.Group }
// UsesQuery returns whether this group is query based
func (g *Group) UsesQuery() bool { return g.Query() != "" }
// CheckQueryBasedMembership returns whether the given contact belongs in a query based group
func (g *Group) CheckQueryBasedMembership(env envs.Environment, contact *Contact) (bool, error) {
if !g.UsesQuery() {
panic("can't check membership on a non-query based group")
}
if contact.Status() != ContactStatusActive {
return false, nil
}
return contactql.EvaluateQuery(env, g.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())
}
// ToXValue returns a representation of this object for use in expressions
//
// uuid:text -> the UUID of the group
// name:text -> the name of the group
//
// @context group
func (g *Group) ToXValue(env envs.Environment) types.XValue {
return types.NewXObject(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(a SessionAssets, refs []*assets.GroupReference, missing assets.MissingCallback) *GroupList {
groups := make([]*Group, 0, len(refs))
for _, ref := range refs {
group := a.Groups().Get(ref.UUID)
if group == nil {
missing(ref, nil)
} else {
groups = append(groups, group)
}
}
return &GroupList{groups: groups}
}
// returns a clone of this group list
func (l *GroupList) clone() *GroupList {
groups := make([]*Group, len(l.groups))
copy(groups, l.groups)
return &GroupList{groups: groups}
}
// returns this group list as a slice of group references
func (l *GroupList) references() []*assets.GroupReference {
refs := make([]*assets.GroupReference, len(l.groups))
for i, group := range l.groups {
refs[i] = group.Reference()
}
return refs
}
// 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 i := range l.groups {
if l.groups[i].UUID() == group.UUID() {
l.groups = append(l.groups[:i], l.groups[i+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)
}
// ToXValue returns a representation of this object for use in expressions
func (l GroupList) ToXValue(env envs.Environment) types.XValue {
array := make([]types.XValue, len(l.groups))
for i, group := range l.groups {
array[i] = group.ToXValue(env)
}
return types.NewXArray(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(env envs.Environment, fields *FieldAssets, groups []assets.Group) (*GroupAssets, []assets.Group) {
broken := make([]assets.Group, 0)
s := &GroupAssets{
all: make([]*Group, 0, len(groups)),
byUUID: make(map[assets.GroupUUID]*Group, len(groups)),
}
for _, asset := range groups {
group, err := NewGroup(env, fields, asset)
if err != nil {
broken = append(broken, asset)
} else {
s.all = append(s.all, group)
s.byUUID[group.UUID()] = group
}
}
return s, broken
}
// 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
}