-
Notifications
You must be signed in to change notification settings - Fork 20
/
users.go
86 lines (73 loc) · 2.08 KB
/
users.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
package flows
import (
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/envs"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// User adds some functionality to user assets.
type User struct {
assets.User
}
// NewUser returns a new user object from the given user asset
func NewUser(asset assets.User) *User {
return &User{User: asset}
}
// Asset returns the underlying asset
func (u *User) Asset() assets.User { return u.User }
// Reference returns a reference to this user
func (u *User) Reference() *assets.UserReference {
return assets.NewUserReference(u.Email(), u.Name())
}
// Format returns a friendly string version of this user depending on what fields are set
func (u *User) Format() string {
// if user has a name set, use that
if u.Name() != "" {
return u.Name()
}
// otherwise use email
return u.Email()
}
// Context returns the properties available in expressions
//
// __default__:text -> the name or email
// email:text -> the email address of the user
// name:text -> the name of the user
// first_name:text -> the first name of the user
//
// @context user
func (u *User) Context(env envs.Environment) map[string]types.XValue {
var firstName types.XText
names := utils.TokenizeString(u.Name())
if len(names) >= 1 {
firstName = types.NewXText(names[0])
}
return map[string]types.XValue{
"__default__": types.NewXText(u.Format()),
"email": types.NewXText(u.Email()),
"name": types.NewXText(u.Name()),
"first_name": firstName,
}
}
// UserAssets provides access to all user assets
type UserAssets struct {
all []*User
byEmail map[string]*User
}
// NewUserAssets creates a new set of user assets
func NewUserAssets(users []assets.User) *UserAssets {
s := &UserAssets{
all: make([]*User, len(users)),
byEmail: make(map[string]*User, len(users)),
}
for i, asset := range users {
user := NewUser(asset)
s.all[i] = user
s.byEmail[user.Email()] = user
}
return s
}
// Get returns the user with the given email
func (s *UserAssets) Get(email string) *User {
return s.byEmail[email]
}