Glific v0.3.1 Glific.Flows.FlowContext View Source

When we are running a flow, we are running it in the context of a contact and/or a conversation (or other Glific data types). Let encapsulate this in a module and isolate the flow from the other aspects of Glific

Link to this section Summary

Functions

Check if there is an active context (i.e. with a non null, node_uuid for this contact)

Standard changeset pattern we use for all data types

Create a FlowContext

Execute one (or more) steps in a flow based on the message stream

Retrieve the value from a results string

Start a new context, if there is an existing context, blow it away

Load the context object, given a flow object and a contact. At some point, we'll get the genserver to cache this

Count the number of times we have sent the same message in the recent past

Resets the context and sends control back to the parent context if one exists

Set the new node for the context

Given an input string, consume the input and advance the state of the context

Update the recent_* state as we consume or send a message

Update the contact results with each element of the json map

Update the contact results state as we step through the flow

Find all the contexts which need to be woken up and processed

Link to this section Types

Specs

t() :: %Glific.Flows.FlowContext{
  __meta__: Ecto.Schema.Metadata.t(),
  completed_at: :utc_datetime | nil,
  contact: Glific.Contacts.Contact.t() | Ecto.Association.NotLoaded.t() | nil,
  contact_id: non_neg_integer() | nil,
  delay: integer(),
  flow: Glific.Flows.Flow.t() | Ecto.Association.NotLoaded.t() | nil,
  flow_id: non_neg_integer() | nil,
  flow_uuid: Ecto.UUID.t() | nil,
  id: term(),
  inserted_at: :utc_datetime | nil,
  node: Glific.Flows.Node.t() | nil,
  node_uuid: Ecto.UUID.t() | nil,
  parent: t() | Ecto.Association.NotLoaded.t() | nil,
  parent_id: non_neg_integer() | nil,
  recent_inbound: [map()] | [],
  recent_outbound: [map()] | [],
  results: map() | nil,
  updated_at: :utc_datetime | nil,
  uuid_map: map() | nil,
  wakeup_at: :utc_datetime | nil
}

Link to this section Functions

Link to this function

active_context(contact_id, parent_id \\ nil)

View Source

Specs

active_context(non_neg_integer(), non_neg_integer() | nil) :: t() | nil

Check if there is an active context (i.e. with a non null, node_uuid for this contact)

Link to this function

changeset(context, attrs)

View Source

Specs

changeset(t(), map()) :: Ecto.Changeset.t()

Standard changeset pattern we use for all data types

Link to this function

create_flow_context(attrs \\ %{})

View Source

Specs

create_flow_context(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a FlowContext

Link to this function

execute(context, messages)

View Source

Specs

execute(t(), [String.t()]) :: {:ok, t(), [String.t()]} | {:error, String.t()}

Execute one (or more) steps in a flow based on the message stream

Link to this function

get_result_value(context, value)

View Source

Specs

get_result_value(t(), String.t()) :: String.t() | nil

Retrieve the value from a results string

Link to this function

init_context(flow, contact, parent_id \\ nil, current_delay \\ 0)

View Source

Specs

init_context(
  Glific.Flows.Flow.t(),
  Glific.Contacts.Contact.t(),
  non_neg_integer() | nil,
  non_neg_integer() | 0
) :: {:ok, t(), [String.t()]} | {:error, String.t()}

Start a new context, if there is an existing context, blow it away

Link to this function

load_context(context, flow)

View Source

Specs

load_context(t(), Glific.Flows.Flow.t()) :: t()

Load the context object, given a flow object and a contact. At some point, we'll get the genserver to cache this

Link to this function

match_outbound(context, uuid, go_back \\ 6)

View Source

Specs

match_outbound(t(), Ecto.UUID.t(), integer()) :: integer()

Count the number of times we have sent the same message in the recent past

Specs

reset_context(t()) :: t() | nil

Resets the context and sends control back to the parent context if one exists

Specs

set_node(t(), Glific.Flows.Node.t()) :: t()

Set the new node for the context

Link to this function

step_forward(context, body)

View Source

Specs

step_forward(t(), String.t()) :: {:ok, map()} | {:error, String.t()}

Given an input string, consume the input and advance the state of the context

Link to this function

update_recent(context, body, type)

View Source

Specs

update_recent(t(), String.t(), atom()) :: t()

Update the recent_* state as we consume or send a message

Link to this function

update_results(context, key, json)

View Source

Specs

update_results(t(), String.t(), map()) :: t()

Update the contact results with each element of the json map

Link to this function

update_results(context, key, input, category)

View Source

Specs

update_results(t(), String.t(), String.t(), String.t()) :: t()

Update the contact results state as we step through the flow

Specs

wakeup() :: [t()]

Find all the contexts which need to be woken up and processed