Skip to content

Commit

Permalink
Add and use MsgFlowRef type
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 20, 2022
1 parent 04bd755 commit dccf0dc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 54 deletions.
3 changes: 3 additions & 0 deletions backends/rapidpro/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (ts *BackendTestSuite) TestMsgUnmarshal() {
"contact_id": 30,
"contact_urn_id": 14,
"error_count": 0,
"flow": {"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"},
"modified_on": "2017-07-21T19:22:23.254133Z",
"id": 204,
"channel_uuid": "f3ad3eb6-d00d-4dc3-92e9-9f34f32940ba",
Expand Down Expand Up @@ -140,6 +141,8 @@ func (ts *BackendTestSuite) TestMsgUnmarshal() {
ts.Equal("external-id", msg.ResponseToExternalID())
ts.True(msg.HighPriority())
ts.True(msg.IsResend())
ts.Equal("Favorites", msg.Flow().Name)
ts.Equal("9de3663f-c5c5-4c92-9f45-ecbc09abcc85", msg.Flow().UUID)

msgJSONNoQR := `{
"status": "P",
Expand Down
24 changes: 3 additions & 21 deletions backends/rapidpro/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ type DBMsg struct {
SessionWaitStartedOn_ *time.Time `json:"session_wait_started_on,omitempty"`
SessionStatus_ string `json:"session_status,omitempty"`

Flow_ json.RawMessage `json:"flow,omitempty"`
Flow_ courier.MsgFlowRef `json:"flow,omitempty"`

channel *DBChannel
workerToken queue.WorkerToken
Expand All @@ -544,25 +544,7 @@ func (m *DBMsg) IsResend() bool { return m.IsResend_ }
func (m *DBMsg) Channel() courier.Channel { return m.channel }
func (m *DBMsg) SessionStatus() string { return m.SessionStatus_ }

func (m *DBMsg) Flow() json.RawMessage {
return m.Flow_
}

func (m *DBMsg) FlowName() string {
if m.Flow_ == nil {
return ""
}
name, _, _, _ := jsonparser.Get(m.Flow_, "name")
return string(name)
}

func (m *DBMsg) FlowUUID() string {
if m.Flow_ == nil {
return ""
}
flowUUID, _, _, _ := jsonparser.Get(m.Flow_, "uuid")
return string(flowUUID)
}
func (m *DBMsg) Flow() courier.MsgFlowRef { return m.Flow_ }

func (m *DBMsg) QuickReplies() []string {
if m.quickReplies != nil {
Expand Down Expand Up @@ -620,7 +602,7 @@ func (m *DBMsg) WithUUID(uuid courier.MsgUUID) courier.Msg { m.UUID_ = uuid; ret
func (m *DBMsg) WithMetadata(metadata json.RawMessage) courier.Msg { m.Metadata_ = metadata; return m }

// WithFlow can be used to add flow to a Msg
func (m *DBMsg) WithFlow(flow json.RawMessage) courier.Msg { m.Flow_ = flow; return m }
func (m *DBMsg) WithFlow(flow courier.MsgFlowRef) courier.Msg { m.Flow_ = flow; return m }

// WithAttachment can be used to append to the media urls for a message
func (m *DBMsg) WithAttachment(url string) courier.Msg {
Expand Down
2 changes: 1 addition & 1 deletion handlers/highconnection/highconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (h *handler) SendMsg(ctx context.Context, msg courier.Msg) (courier.MsgStat
"to": []string{msg.URN().Path()},
"ret_id": []string{msg.ID().String()},
"datacoding": []string{"8"},
"user_data": []string{msg.FlowName()},
"user_data": []string{msg.Flow().Name},
"ret_url": []string{statusURL},
"ret_mo_url": []string{receiveURL},
}
Expand Down
21 changes: 16 additions & 5 deletions handlers/highconnection/highconnection_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package highconnection

import (
"encoding/json"
"net/http/httptest"
"testing"
"time"
Expand Down Expand Up @@ -58,7 +57,10 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "Simple Message",
URN: "tel:+250788383383",
Status: "W",
Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`),
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
Expand Down Expand Up @@ -93,7 +95,10 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "☺",
URN: "tel:+250788383383",
Status: "W",
Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`),
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
Expand All @@ -111,7 +116,10 @@ var defaultSendTestCases = []ChannelSendTestCase{
Text: "This is a longer message than 160 characters and will cause us to split it into two separate parts, isn't that right but it is even longer than before I say, I need to keep adding more things to make it work",
URN: "tel:+250788383383",
Status: "W",
Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`),
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
Expand All @@ -130,7 +138,10 @@ var defaultSendTestCases = []ChannelSendTestCase{
Attachments: []string{"image/jpeg:https://foo.bar/image.jpg"},
URN: "tel:+250788383383",
Status: "W",
Flow: json.RawMessage(`{"uuid": "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", "name": "Favorites"}`),
Flow: &struct {
UUID string
Name string
}{UUID: "9de3663f-c5c5-4c92-9f45-ecbc09abcc85", Name: "Favorites"},
URLParams: map[string]string{
"accountid": "Username",
"password": "Password",
Expand Down
10 changes: 7 additions & 3 deletions handlers/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ type ChannelSendTestCase struct {
HighPriority bool
ResponseToExternalID string
Metadata json.RawMessage
Flow json.RawMessage
Flow *struct {
UUID string
Name string
}

ResponseStatus int
ResponseBody string
Expand Down Expand Up @@ -232,8 +235,9 @@ func RunChannelSendTestCases(t *testing.T, channel courier.Channel, handler cour
if len(testCase.Metadata) > 0 {
msg.WithMetadata(testCase.Metadata)
}
if len(testCase.Flow) > 0 {
msg.WithFlow(testCase.Flow)
if testCase.Flow != nil {
var flowRef = courier.MsgFlowRef{UUID: testCase.Flow.UUID, Name: testCase.Flow.Name}
msg.WithFlow(flowRef)
}

var testRequest *http.Request
Expand Down
13 changes: 9 additions & 4 deletions msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func NewMsgUUIDFromString(uuidString string) MsgUUID {
return MsgUUID{uuid}
}

type MsgFlowRef struct {
UUID string `json:"uuid" validate:"uuid4"`
Name string `json:"name"`
}

var NilMsgFlowRef = MsgFlowRef{"", ""}

//-----------------------------------------------------------------------------
// Msg interface
//-----------------------------------------------------------------------------
Expand All @@ -98,9 +105,7 @@ type Msg interface {
ResponseToExternalID() string
IsResend() bool

Flow() json.RawMessage
FlowName() string
FlowUUID() string
Flow() MsgFlowRef

Channel() Channel

Expand All @@ -117,7 +122,7 @@ type Msg interface {
WithAttachment(url string) Msg
WithURNAuth(auth string) Msg
WithMetadata(metadata json.RawMessage) Msg
WithFlow(flow json.RawMessage) Msg
WithFlow(flow MsgFlowRef) Msg

EventID() int64
SessionStatus() string
Expand Down
23 changes: 3 additions & 20 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync"
"time"

"github.com/buger/jsonparser"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"

Expand Down Expand Up @@ -563,7 +562,7 @@ type mockMsg struct {
metadata json.RawMessage
alreadyWritten bool
isResend bool
flow json.RawMessage
flow MsgFlowRef

receivedOn *time.Time
sentOn *time.Time
Expand All @@ -572,23 +571,7 @@ type mockMsg struct {

func (m *mockMsg) SessionStatus() string { return "" }

func (m *mockMsg) Flow() json.RawMessage { return m.flow }

func (m *mockMsg) FlowName() string {
if m.flow == nil {
return ""
}
name, _, _, _ := jsonparser.Get(m.flow, "name")
return string(name)
}

func (m *mockMsg) FlowUUID() string {
if m.flow == nil {
return ""
}
flowUUID, _, _, _ := jsonparser.Get(m.flow, "uuid")
return string(flowUUID)
}
func (m *mockMsg) Flow() MsgFlowRef { return m.flow }

func (m *mockMsg) Channel() Channel { return m.channel }
func (m *mockMsg) ID() MsgID { return m.id }
Expand Down Expand Up @@ -623,7 +606,7 @@ func (m *mockMsg) WithAttachment(url string) Msg {
}
func (m *mockMsg) WithMetadata(metadata json.RawMessage) Msg { m.metadata = metadata; return m }

func (m *mockMsg) WithFlow(flow json.RawMessage) Msg { m.flow = flow; return m }
func (m *mockMsg) WithFlow(flow MsgFlowRef) Msg { m.flow = flow; return m }

//-----------------------------------------------------------------------------
// Mock status implementation
Expand Down

0 comments on commit dccf0dc

Please sign in to comment.