Skip to content

Commit

Permalink
Merge pull request #1149 from nyaruka/mobile_bindings_msg_wait
Browse files Browse the repository at this point in the history
Add method to mobile bindings to make it easier to get msg wait events
  • Loading branch information
rowanseymour authored Jan 11, 2023
2 parents c298221 + 132c17b commit d0d4a18
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
36 changes: 28 additions & 8 deletions mobile/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/flows/definition/migrations"
"github.com/nyaruka/goflow/flows/engine"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/resumes"
"github.com/nyaruka/goflow/flows/triggers"
"github.com/nyaruka/goflow/utils"
Expand Down Expand Up @@ -207,6 +208,22 @@ func (m *Modifier) Payload() string {
return m.payload
}

type Hint struct {
target flows.Hint
}

func (h *Hint) Type() string {
return string(h.target.Type())
}

type MsgWaitEvent struct {
target *events.MsgWaitEvent
}

func (e *MsgWaitEvent) Hint() *Hint {
return &Hint{target: e.target.Hint}
}

// Sprint is an interaction with the engine - i.e. a start or resume of a session
type Sprint struct {
target flows.Sprint
Expand All @@ -232,6 +249,17 @@ func (s *Sprint) Events() *EventSlice {
return events
}

// MsgWaitEvent returns the first msg wait event if there is one
func (s *Sprint) MsgWaitEvent() *MsgWaitEvent {
for _, event := range s.target.Events() {
switch typed := event.(type) {
case *events.MsgWaitEvent:
return &MsgWaitEvent{target: typed}
}
}
return nil
}

// Session represents a session with the flow engine
type Session struct {
target flows.Session
Expand Down Expand Up @@ -265,14 +293,6 @@ func (s *Session) ToJSON() (string, error) {
return string(data), nil
}

type Hint struct {
target flows.Hint
}

func (h *Hint) Type() string {
return string(h.target.Type())
}

type Engine struct {
target flows.Engine
}
Expand Down
3 changes: 3 additions & 0 deletions mobile/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func TestMobileBindings(t *testing.T) {
modifiers := sprint.Modifiers()
assert.Equal(t, 0, modifiers.Length())

waitEvent := sprint.MsgWaitEvent()
assert.NotNil(t, waitEvent)

attachments := mobile.NewStringSlice(1)
attachments.Add("content://io.rapidpro.surveyor/files/selfie.jpg")
msg := mobile.NewMsgIn("8e6f0213-a122-4c50-a430-442085754c16", "Hi there", attachments)
Expand Down

0 comments on commit d0d4a18

Please sign in to comment.