From 1b97543400479b64080b49ba95a739bc811d8e94 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 7 May 2024 15:51:44 -0500 Subject: [PATCH] Change Channel.IsScheme to take scheme object --- backends/rapidpro/channel.go | 5 +++-- channel.go | 3 ++- handlers/external/handler.go | 2 -- handlers/twiml/handlers.go | 6 +++--- test/channel.go | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/backends/rapidpro/channel.go b/backends/rapidpro/channel.go index f33323619..59b7737d5 100644 --- a/backends/rapidpro/channel.go +++ b/backends/rapidpro/channel.go @@ -9,6 +9,7 @@ import ( "github.com/lib/pq" "github.com/nyaruka/courier" "github.com/nyaruka/gocommon/i18n" + "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/null/v3" ) @@ -60,8 +61,8 @@ func (c *Channel) ChannelAddress() courier.ChannelAddress { func (c *Channel) Country() i18n.Country { return i18n.Country(c.Country_.String) } // IsScheme returns whether this channel serves only the passed in scheme -func (c *Channel) IsScheme(scheme string) bool { - return len(c.Schemes_) == 1 && c.Schemes_[0] == scheme +func (c *Channel) IsScheme(scheme *urns.Scheme) bool { + return len(c.Schemes_) == 1 && c.Schemes_[0] == scheme.Prefix } // Roles returns the roles of this channel diff --git a/channel.go b/channel.go index dc881844c..a63111510 100644 --- a/channel.go +++ b/channel.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/nyaruka/gocommon/i18n" + "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/gocommon/uuids" "github.com/nyaruka/null/v3" ) @@ -131,7 +132,7 @@ type Channel interface { Roles() []ChannelRole // is this channel for the passed in scheme (and only that scheme) - IsScheme(string) bool + IsScheme(*urns.Scheme) bool // CallbackDomain returns the domain that should be used for any callbacks the channel registers CallbackDomain(fallbackDomain string) string diff --git a/handlers/external/handler.go b/handlers/external/handler.go index 54f2a991c..34407c4f6 100644 --- a/handlers/external/handler.go +++ b/handlers/external/handler.go @@ -109,7 +109,6 @@ func (h *handler) receiveStopContact(ctx context.Context, channel courier.Channe if err != nil { return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err) } - urn = urn.Normalize() // create a stop channel event channelEvent := h.Backend().NewChannelEvent(channel, courier.EventTypeStopContact, urn, clog) @@ -212,7 +211,6 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w if err != nil { return nil, handlers.WriteAndLogRequestError(ctx, h, channel, w, r, err) } - urn = urn.Normalize() // build our msg msg := h.Backend().NewIncomingMsg(channel, urn, text, "", clog).WithReceivedOn(date) diff --git a/handlers/twiml/handlers.go b/handlers/twiml/handlers.go index 3be7e41f3..b81824d52 100644 --- a/handlers/twiml/handlers.go +++ b/handlers/twiml/handlers.go @@ -137,7 +137,7 @@ func (h *handler) receiveMessage(ctx context.Context, channel courier.Channel, w } text := form.Body - if channel.IsScheme(urns.WhatsApp.Prefix) && form.ButtonText != "" { + if channel.IsScheme(urns.WhatsApp) && form.ButtonText != "" { text = form.ButtonText } @@ -260,7 +260,7 @@ func (h *handler) Send(ctx context.Context, msg courier.MsgOut, res *courier.Sen } // for whatsapp channels, we have to prepend whatsapp to the To and From - if channel.IsScheme(urns.WhatsApp.Prefix) { + if channel.IsScheme(urns.WhatsApp) { form["To"][0] = fmt.Sprintf("%s:+%s", urns.WhatsApp.Prefix, form["To"][0]) form["From"][0] = fmt.Sprintf("%s:%s", urns.WhatsApp.Prefix, form["From"][0]) } @@ -348,7 +348,7 @@ func (h *handler) RedactValues(ch courier.Channel) []string { } func (h *handler) parseURN(channel courier.Channel, text string, country i18n.Country) (urns.URN, error) { - if channel.IsScheme(urns.WhatsApp.Prefix) { + if channel.IsScheme(urns.WhatsApp) { // Twilio Whatsapp from is in the form: whatsapp:+12211414154 or +12211414154 var fromTel string parts := strings.Split(text, ":") diff --git a/test/channel.go b/test/channel.go index d46066524..3ae284fb5 100644 --- a/test/channel.go +++ b/test/channel.go @@ -38,8 +38,8 @@ func (c *MockChannel) SetScheme(scheme string) { c.schemes = []string{scheme} } func (c *MockChannel) Schemes() []string { return c.schemes } // IsScheme returns whether the passed in scheme is the scheme for this channel -func (c *MockChannel) IsScheme(scheme string) bool { - return len(c.schemes) == 1 && c.schemes[0] == scheme +func (c *MockChannel) IsScheme(scheme *urns.Scheme) bool { + return len(c.schemes) == 1 && c.schemes[0] == scheme.Prefix } // Address returns the address as a string of this channel