Skip to content

Commit

Permalink
Change Channel.IsScheme to take scheme object
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 7, 2024
1 parent c48a61d commit 4c1de93
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
5 changes: 3 additions & 2 deletions backends/rapidpro/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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

Check warning on line 65 in backends/rapidpro/channel.go

View check run for this annotation

Codecov / codecov/patch

backends/rapidpro/channel.go#L64-L65

Added lines #L64 - L65 were not covered by tests
}

// Roles returns the roles of this channel
Expand Down
3 changes: 2 additions & 1 deletion channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions handlers/twiml/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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])
}
Expand Down Expand Up @@ -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, ":")
Expand Down
4 changes: 2 additions & 2 deletions test/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c1de93

Please sign in to comment.