Skip to content

Commit

Permalink
Simplify getting flow from an event
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Jan 7, 2025
1 parent 7ec8355 commit 42c552e
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 36 deletions.
6 changes: 2 additions & 4 deletions core/handlers/contact_urns_changed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log/slog"

"github.com/jmoiron/sqlx"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/mailroom/core/hooks"
Expand All @@ -23,10 +22,9 @@ func handleContactURNsChanged(ctx context.Context, rt *runtime.Runtime, tx *sqlx

slog.Debug("contact urns changed", "contact", scene.ContactUUID(), "session", scene.SessionID(), "urns", event.URNs)

var flow *assets.FlowReference
var flow *models.Flow
if scene.Session() != nil {
run, _ := scene.Session().FindStep(e.StepUUID())
flow = run.FlowReference()
flow, _ = scene.Session().LocateEvent(e)
}

// create our URN changed event
Expand Down
7 changes: 1 addition & 6 deletions core/handlers/msg_created.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ func handleMsgCreated(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa
}

// and the flow
var flow *models.Flow
run, _ := scene.Session().FindStep(e.StepUUID())
flowAsset, _ := oa.FlowByUUID(run.FlowReference().UUID)
if flowAsset != nil {
flow = flowAsset.(*models.Flow)
}
flow, _ := scene.Session().LocateEvent(e)

msg, err := models.NewOutgoingFlowMsg(rt, oa.Org(), channel, scene.Session(), flow, event.Msg, event.CreatedOn())
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions core/handlers/optin_requested.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ func handleOptInRequested(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
}

// and the flow
var flow *models.Flow
run, _ := scene.Session().FindStep(e.StepUUID())
flowAsset, _ := oa.FlowByUUID(run.FlowReference().UUID)
if flowAsset != nil {
flow = flowAsset.(*models.Flow)
}
flow, _ := scene.Session().LocateEvent(e)

msg := models.NewOutgoingOptInMsg(rt, scene.Session(), flow, optIn, channel, urn, event.CreatedOn())

Expand Down
7 changes: 2 additions & 5 deletions core/handlers/ticket_opened.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ func handleTicketOpened(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, o

var openedInID models.FlowID
if scene.Session() != nil {
run, _ := scene.Session().FindStep(e.StepUUID())
flowAsset, _ := oa.FlowByUUID(run.FlowReference().UUID)
if flowAsset != nil {
openedInID = flowAsset.(*models.Flow).ID()
}
flow, _ := scene.Session().LocateEvent(e)
openedInID = flow.ID()
}

ticket := models.NewTicket(
Expand Down
11 changes: 4 additions & 7 deletions core/handlers/warning.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ func init() {
func handleWarning(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx, oa *models.OrgAssets, scene *models.Scene, e flows.Event) error {
event := e.(*events.WarningEvent)

run, _ := scene.Session().FindStep(e.StepUUID())
flow, _ := oa.FlowByUUID(run.FlowReference().UUID)
if flow != nil {
logMsg := warningsLogs[event.Text]
if logMsg != "" {
slog.Error(logMsg, "session", scene.SessionID(), "flow", flow.UUID(), "text", event.Text)
}
flow, _ := scene.Session().LocateEvent(e)
logMsg := warningsLogs[event.Text]
if logMsg != "" {
slog.Error(logMsg, "session", scene.SessionID(), "flow", flow.UUID(), "text", event.Text)
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions core/handlers/webhook_called.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func handleWebhookCalled(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
scene.AppendToEventPreCommitHook(hooks.UnsubscribeResthookHook, unsub)
}

run, step := scene.Session().FindStep(e.StepUUID())
flow := run.Flow().Asset().(*models.Flow)
flow, nodeUUID := scene.Session().LocateEvent(e)

// create an HTTP log
if flow != nil {
Expand All @@ -52,7 +51,7 @@ func handleWebhookCalled(ctx context.Context, rt *runtime.Runtime, tx *sqlx.Tx,
}

// pass node and response time to the hook that monitors webhook health
scene.AppendToEventPreCommitHook(hooks.MonitorWebhooks, &hooks.WebhookCall{NodeUUID: step.NodeUUID(), Event: event})
scene.AppendToEventPreCommitHook(hooks.MonitorWebhooks, &hooks.WebhookCall{NodeUUID: nodeUUID, Event: event})

return nil
}
2 changes: 1 addition & 1 deletion core/hooks/commit_urn_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (h *commitURNChangesHook) Apply(ctx context.Context, rt *runtime.Runtime, t
changes = append(changes, urnChange)

if urnChange.Flow != nil {
flowUUID = urnChange.Flow.UUID
flowUUID = urnChange.Flow.UUID()
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ type ContactURNsChanged struct {
ContactID ContactID
OrgID OrgID
URNs []urns.URN
Flow *assets.FlowReference // for logging
Flow *Flow // for logging
}

func (i *URNID) Scan(value any) error { return null.ScanInt(value, i) }
Expand Down
8 changes: 5 additions & 3 deletions core/models/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,11 @@ func (s *Session) Sprint() flows.Sprint {
return s.sprint
}

// FindStep finds the run and step with the given UUID
func (s *Session) FindStep(uuid flows.StepUUID) (flows.Run, flows.Step) {
return s.findStep(uuid)
// LocateEvent finds the flow and node UUID for an event belonging to this session
func (s *Session) LocateEvent(e flows.Event) (*Flow, flows.NodeUUID) {
run, step := s.findStep(e.StepUUID())
flow := run.Flow().Asset().(*Flow)
return flow, step.NodeUUID()
}

// Timeout returns the amount of time after our last message sends that we should timeout
Expand Down

0 comments on commit 42c552e

Please sign in to comment.