Skip to content

Commit 1b97543

Browse files
committed
Change Channel.IsScheme to take scheme object
1 parent c48a61d commit 1b97543

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

Diff for: backends/rapidpro/channel.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/lib/pq"
1010
"github.com/nyaruka/courier"
1111
"github.com/nyaruka/gocommon/i18n"
12+
"github.com/nyaruka/gocommon/urns"
1213
"github.com/nyaruka/null/v3"
1314
)
1415

@@ -60,8 +61,8 @@ func (c *Channel) ChannelAddress() courier.ChannelAddress {
6061
func (c *Channel) Country() i18n.Country { return i18n.Country(c.Country_.String) }
6162

6263
// IsScheme returns whether this channel serves only the passed in scheme
63-
func (c *Channel) IsScheme(scheme string) bool {
64-
return len(c.Schemes_) == 1 && c.Schemes_[0] == scheme
64+
func (c *Channel) IsScheme(scheme *urns.Scheme) bool {
65+
return len(c.Schemes_) == 1 && c.Schemes_[0] == scheme.Prefix
6566
}
6667

6768
// Roles returns the roles of this channel

Diff for: channel.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66

77
"github.com/nyaruka/gocommon/i18n"
8+
"github.com/nyaruka/gocommon/urns"
89
"github.com/nyaruka/gocommon/uuids"
910
"github.com/nyaruka/null/v3"
1011
)
@@ -131,7 +132,7 @@ type Channel interface {
131132
Roles() []ChannelRole
132133

133134
// is this channel for the passed in scheme (and only that scheme)
134-
IsScheme(string) bool
135+
IsScheme(*urns.Scheme) bool
135136

136137
// CallbackDomain returns the domain that should be used for any callbacks the channel registers
137138
CallbackDomain(fallbackDomain string) string

Diff for: handlers/external/handler.go

-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ func (h *handler) receiveStopContact(ctx context.Context, channel courier.Channe
109109
if err != nil {
110110
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
111111
}
112-
urn = urn.Normalize()
113112

114113
// create a stop channel event
115114
channelEvent := h.Backend().NewChannelEvent(channel, courier.EventTypeStopContact, urn, clog)
@@ -212,7 +211,6 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w
212211
if err != nil {
213212
return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err)
214213
}
215-
urn = urn.Normalize()
216214

217215
// build our msg
218216
msg := h.Backend().NewIncomingMsg(channel, urn, text, "", clog).WithReceivedOn(date)

Diff for: handlers/twiml/handlers.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w
137137
}
138138

139139
text := form.Body
140-
if channel.IsScheme(urns.WhatsApp.Prefix) && form.ButtonText != "" {
140+
if channel.IsScheme(urns.WhatsApp) && form.ButtonText != "" {
141141
text = form.ButtonText
142142
}
143143

@@ -260,7 +260,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen
260260
}
261261

262262
// for whatsapp channels, we have to prepend whatsapp to the To and From
263-
if channel.IsScheme(urns.WhatsApp.Prefix) {
263+
if channel.IsScheme(urns.WhatsApp) {
264264
form["To"][0] = fmt.Sprintf("%s:+%s", urns.WhatsApp.Prefix, form["To"][0])
265265
form["From"][0] = fmt.Sprintf("%s:%s", urns.WhatsApp.Prefix, form["From"][0])
266266
}
@@ -348,7 +348,7 @@ func (h *handler) RedactValues(ch courier.Channel) []string {
348348
}
349349

350350
func (h *handler) parseURN(channel courier.Channel, text string, country i18n.Country) (urns.URN, error) {
351-
if channel.IsScheme(urns.WhatsApp.Prefix) {
351+
if channel.IsScheme(urns.WhatsApp) {
352352
// Twilio Whatsapp from is in the form: whatsapp:+12211414154 or +12211414154
353353
var fromTel string
354354
parts := strings.Split(text, ":")

Diff for: test/channel.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ func (c *MockChannel) SetScheme(scheme string) { c.schemes = []string{scheme} }
3838
func (c *MockChannel) Schemes() []string { return c.schemes }
3939

4040
// IsScheme returns whether the passed in scheme is the scheme for this channel
41-
func (c *MockChannel) IsScheme(scheme string) bool {
42-
return len(c.schemes) == 1 && c.schemes[0] == scheme
41+
func (c *MockChannel) IsScheme(scheme *urns.Scheme) bool {
42+
return len(c.schemes) == 1 && c.schemes[0] == scheme.Prefix
4343
}
4444

4545
// Address returns the address as a string of this channel

0 commit comments

Comments
 (0)