-
Notifications
You must be signed in to change notification settings - Fork 70
/
channel.go
145 lines (106 loc) · 4.52 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
package courier
import (
"database/sql/driver"
"errors"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/null/v3"
)
const (
// ConfigAPIKey is a constant key for channel configs
ConfigAPIKey = "api_key"
// ConfigAuthToken is a constant key for channel configs
ConfigAuthToken = "auth_token"
// ConfigBaseURL is a constant key for channel configs
ConfigBaseURL = "base_url"
// ConfigCallbackDomain is the domain that should be used for this channel when registering callbacks
ConfigCallbackDomain = "callback_domain"
// ConfigContentType is a constant key for channel configs
ConfigContentType = "content_type"
// ConfigMaxLength is the maximum size of a message in characters
ConfigMaxLength = "max_length"
// ConfigPassword is a constant key for channel configs
ConfigPassword = "password"
// ConfigSecret is the secret used for signing commands by the channel
ConfigSecret = "secret"
// ConfigSendAuthorization is a constant key for channel configs
ConfigSendAuthorization = "send_authorization"
// ConfigSendBody is a constant key for channel configs
ConfigSendBody = "body"
// ConfigSendMethod is a constant key for channel configs
ConfigSendMethod = "method"
// ConfigSendURL is a constant key for channel configs
ConfigSendURL = "send_url"
// ConfigUsername is a constant key for channel configs
ConfigUsername = "username"
// ConfigUseNational is a constant key for channel configs
ConfigUseNational = "use_national"
// ConfigSendHeaders is a constant key for channel configs
ConfigSendHeaders = "headers"
)
// ChannelType is the 1-3 letter code used for channel types in the database
type ChannelType string
// AnyChannelType is our empty channel type used when doing lookups without channel type assertions
const AnyChannelType = ChannelType("")
// ChannelRole is a role that a channel can perform
type ChannelRole string
// different roles that channels can perform
const (
ChannelRoleSend ChannelRole = "S"
ChannelRoleReceive ChannelRole = "R"
ChannelRoleCall ChannelRole = "C"
ChannelRoleAnswer ChannelRole = "A"
)
// ChannelUUID is our typing of a channel's UUID
type ChannelUUID uuids.UUID
// NilChannelUUID is our nil value for channel UUIDs
var NilChannelUUID = ChannelUUID("")
// ChannelID is our SQL type for a channel's id
type ChannelID null.Int
// NilChannelID represents a nil channel id
const NilChannelID = ChannelID(0)
// NewChannelID creates a new ChannelID for the passed in int64
func NewChannelID(id int64) ChannelID {
return ChannelID(id)
}
func (i *ChannelID) Scan(value any) error { return null.ScanInt(value, i) }
func (i ChannelID) Value() (driver.Value, error) { return null.IntValue(i) }
func (i *ChannelID) UnmarshalJSON(b []byte) error { return null.UnmarshalInt(b, i) }
func (i ChannelID) MarshalJSON() ([]byte, error) { return null.MarshalInt(i) }
// ChannelAddress is our SQL type for a channel address
type ChannelAddress null.String
// NilChannelAddress represents a nil channel address
const NilChannelAddress = ChannelAddress("")
func (address ChannelAddress) String() string {
return string(address)
}
// ErrChannelExpired is returned when our cached channel has outlived it's TTL
var ErrChannelExpired = errors.New("channel expired")
// ErrChannelNotFound is returned when we fail to find a channel in the db
var ErrChannelNotFound = errors.New("channel not found")
// ErrChannelWrongType is returned when we find a channel with the set UUID but with a different type
var ErrChannelWrongType = errors.New("channel type wrong")
//-----------------------------------------------------------------------------
// Channel Interface
//-----------------------------------------------------------------------------
// Channel defines the general interface backend Channel implementations must adhere to
type Channel interface {
UUID() ChannelUUID
Name() string
ChannelType() ChannelType
Schemes() []string
Country() i18n.Country
Address() string
ChannelAddress() ChannelAddress
Roles() []ChannelRole
// is this channel for the passed in scheme (and only that scheme)
IsScheme(*urns.Scheme) bool
// CallbackDomain returns the domain that should be used for any callbacks the channel registers
CallbackDomain(fallbackDomain string) string
ConfigForKey(key string, defaultValue any) any
StringConfigForKey(key string, defaultValue string) string
BoolConfigForKey(key string, defaultValue bool) bool
IntConfigForKey(key string, defaultValue int) int
OrgConfigForKey(key string, defaultValue any) any
}