From dccf0dc2f27c3310396c253199fae2aed632a7a8 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Thu, 20 Jan 2022 14:11:58 +0200 Subject: [PATCH] Add and use MsgFlowRef type --- backends/rapidpro/backend_test.go | 3 +++ backends/rapidpro/msg.go | 24 +++---------------- handlers/highconnection/highconnection.go | 2 +- .../highconnection/highconnection_test.go | 21 ++++++++++++---- handlers/test.go | 10 +++++--- msg.go | 13 ++++++---- test.go | 23 +++--------------- 7 files changed, 42 insertions(+), 54 deletions(-) diff --git a/backends/rapidpro/backend_test.go b/backends/rapidpro/backend_test.go index 07298585e..aa738ae91 100644 --- a/backends/rapidpro/backend_test.go +++ b/backends/rapidpro/backend_test.go @@ -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", @@ -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", diff --git a/backends/rapidpro/msg.go b/backends/rapidpro/msg.go index fca7d334f..28cd1fac8 100644 --- a/backends/rapidpro/msg.go +++ b/backends/rapidpro/msg.go @@ -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 @@ -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 { @@ -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 { diff --git a/handlers/highconnection/highconnection.go b/handlers/highconnection/highconnection.go index 70f2cec7c..e5fa0d418 100644 --- a/handlers/highconnection/highconnection.go +++ b/handlers/highconnection/highconnection.go @@ -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}, } diff --git a/handlers/highconnection/highconnection_test.go b/handlers/highconnection/highconnection_test.go index 8a7b8f370..9376bbb4f 100644 --- a/handlers/highconnection/highconnection_test.go +++ b/handlers/highconnection/highconnection_test.go @@ -1,7 +1,6 @@ package highconnection import ( - "encoding/json" "net/http/httptest" "testing" "time" @@ -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", @@ -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", @@ -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", @@ -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", diff --git a/handlers/test.go b/handlers/test.go index 0c67d1f4e..c4fe86250 100644 --- a/handlers/test.go +++ b/handlers/test.go @@ -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 @@ -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 diff --git a/msg.go b/msg.go index b45b51a5b..7721d8be7 100644 --- a/msg.go +++ b/msg.go @@ -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 //----------------------------------------------------------------------------- @@ -98,9 +105,7 @@ type Msg interface { ResponseToExternalID() string IsResend() bool - Flow() json.RawMessage - FlowName() string - FlowUUID() string + Flow() MsgFlowRef Channel() Channel @@ -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 diff --git a/test.go b/test.go index 385375e71..b620925a9 100644 --- a/test.go +++ b/test.go @@ -11,7 +11,6 @@ import ( "sync" "time" - "github.com/buger/jsonparser" "github.com/nyaruka/gocommon/urns" "github.com/nyaruka/gocommon/uuids" @@ -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 @@ -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 } @@ -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