-
Notifications
You must be signed in to change notification settings - Fork 20
/
channel.go
179 lines (148 loc) · 4.37 KB
/
channel.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
package flows
import (
"encoding/json"
"fmt"
"github.com/nyaruka/goflow/utils"
)
type ChannelRole string
const (
ChannelRoleSend ChannelRole = "send"
ChannelRoleReceive ChannelRole = "receive"
ChannelRoleCall ChannelRole = "call"
ChannelRoleAnswer ChannelRole = "answer"
ChannelRoleUSSD ChannelRole = "ussd"
)
type channel struct {
uuid ChannelUUID
name string
address string
schemes []string
roles []ChannelRole
}
func NewChannel(uuid ChannelUUID, name string, address string, schemes []string, roles []ChannelRole) Channel {
return &channel{
uuid: uuid,
name: name,
address: address,
schemes: schemes,
roles: roles,
}
}
// UUID returns the UUID of this channel
func (c *channel) UUID() ChannelUUID { return c.uuid }
// Name returns the name of this channel
func (c *channel) Name() string { return c.name }
// Address returns the address of this channel
func (c *channel) Address() string { return c.address }
// Schemes returns the supported schemes of this channel
func (c *channel) Schemes() []string { return c.schemes }
// Roles returns the roles of this channel
func (c *channel) Roles() []ChannelRole { return c.roles }
// Reference returns a reference to this channel
func (c *channel) Reference() *ChannelReference { return NewChannelReference(c.uuid, c.name) }
func (c *channel) SupportsScheme(scheme string) bool {
for _, s := range c.schemes {
if s == scheme {
return true
}
}
return false
}
func (c *channel) HasRole(role ChannelRole) bool {
for _, r := range c.roles {
if r == role {
return true
}
}
return false
}
// Resolve satisfies our resolver interface
func (c *channel) Resolve(key string) interface{} {
switch key {
case "uuid":
return c.uuid
case "name":
return c.name
case "address":
return c.address
}
return fmt.Errorf("No field '%s' on channel", key)
}
// Default returns the default value for a channel, which is itself
func (c *channel) Default() interface{} {
return c
}
// String returns the default string value for a channel, which is its name
func (c *channel) String() string {
return c.name
}
var _ utils.VariableResolver = (*channel)(nil)
// ChannelSet defines the unordered set of all channels for a session
type ChannelSet struct {
channels []Channel
channelsByUUID map[ChannelUUID]Channel
}
func NewChannelSet(channels []Channel) *ChannelSet {
s := &ChannelSet{channels: channels, channelsByUUID: make(map[ChannelUUID]Channel, len(channels))}
for _, channel := range s.channels {
s.channelsByUUID[channel.UUID()] = channel
}
return s
}
// GetForURN returns the best channel for the given URN
func (s *ChannelSet) GetForURN(urn *ContactURN) Channel {
// if caller has told us which channel to use for this URN, use that
if urn.Channel() != nil {
return urn.Channel()
}
// if not, return the first channel which supports this URN scheme
scheme := urn.Scheme()
for _, ch := range s.channels {
if ch.HasRole(ChannelRoleSend) && ch.SupportsScheme(scheme) {
return ch
}
}
return nil
}
func (s *ChannelSet) FindByUUID(uuid ChannelUUID) Channel {
return s.channelsByUUID[uuid]
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type channelEnvelope struct {
UUID ChannelUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name"`
Address string `json:"address"`
Schemes []string `json:"schemes" validate:"min=1"`
Roles []ChannelRole `json:"roles" validate:"min=1,dive,eq=send|eq=receive|eq=call|eq=answer|eq=ussd"`
}
// ReadChannel decodes a channel from the passed in JSON
func ReadChannel(data json.RawMessage) (Channel, error) {
ce := channelEnvelope{}
if err := utils.UnmarshalAndValidate(data, &ce, "channel"); err != nil {
return nil, err
}
return &channel{
uuid: ce.UUID,
name: ce.Name,
address: ce.Address,
schemes: ce.Schemes,
roles: ce.Roles,
}, nil
}
// ReadChannelSet decodes channels from the passed in JSON
func ReadChannelSet(data json.RawMessage) (*ChannelSet, error) {
items, err := utils.UnmarshalArray(data)
if err != nil {
return nil, err
}
channels := make([]Channel, len(items))
for c := range items {
channels[c], err = ReadChannel(items[c])
if err != nil {
return nil, err
}
}
return NewChannelSet(channels), nil
}