View Source Glific (Glific v5.1.6)

Glific keeps the contexts that define your domain and business logic.

Contexts are also responsible for managing your data, regardless if it comes from the database, an external API or others.

For now we'll keep some commonly used functions here, until we need a new file

Link to this section Summary

Functions

Adds a limit to restrict accessing data from big tables like messages, contacts which slows DB and takes longer to complete request

Convert map string keys to :atom keys

Delete multiple items from the map

execute string in eex

Return a time object where you go back x units. We introduce the notion of hour and minute

A hack to suppress error messages when running lots of flows. These are expected and we want to improve signal <-> noise ratio

See if the current time is within the past time units

Log the error and also send it over to our friends at appsignal

Given a string separated by spaces, commas, or semi-colons, create a set of individual elements in the string

Wrapper to return :ok/:error when parsing strings to potential integers

parse and integer and die if parse fails

Wrapper to return :ok/:error when parsing strings to potential integers

You shouldn’t really use String.to_atom/1 on user-supplied data. The BEAM has a limit on how many different atoms you can have and they’re not garbage collected! With data coming from outside the system, stick to strings or use String.to_existing_atom/1 instead! So this is a generic function which will convert the string to atom and throws an error in case of invalid key

Compute the signature at a specific time for the body

easy way for glific developers to get a stacktrace when debugging

Lets get rid of all non valid characters. We are assuming any language and hence using unicode syntax and not restricting ourselves to alphanumeric

convert string to snake case

Intermediary function to update the input params with organization id as operation is performed by glific_admin for other organizations

Really simple function to ensure folks do not add Repo and/or IO calls to an EEx snippet. This is an extremely short term fix to avoid shooting ourselves in the foot, but we should move to lua for flows scripting in the near future

Validates inputted shortcode, if shortcode is invalid it returns message that the shortcode is invalid along with the valid shortcode.

Verifying Google Captcha

Link to this section Functions

@spec add_limit(map()) :: map()

Adds a limit to restrict accessing data from big tables like messages, contacts which slows DB and takes longer to complete request

Adding upper limit to 50 when limit is passed and is more than 50 Adding limit to 25 when limit is not passed in args

@spec atomize_keys(any()) :: any()

Convert map string keys to :atom keys

Link to this function

delete_multiple(map, list)

View Source
@spec delete_multiple(map(), list()) :: map()

Delete multiple items from the map

@spec execute_eex(String.t()) :: String.t()

execute string in eex

Link to this function

go_back_time(go_back, time \\ DateTime.utc_now(), unit \\ :hour)

View Source
@spec go_back_time(integer(), DateTime.t(), atom()) :: DateTime.t()

Return a time object where you go back x units. We introduce the notion of hour and minute

@spec ignore_error?(String.t()) :: boolean()

A hack to suppress error messages when running lots of flows. These are expected and we want to improve signal <-> noise ratio

Link to this function

in_past_time(time, units \\ :hours, back \\ 24)

View Source
@spec in_past_time(DateTime.t(), atom(), integer()) :: boolean()

See if the current time is within the past time units

Link to this function

log_error(error, send_appsignal? \\ true)

View Source
@spec log_error(String.t(), boolean()) :: {:error, String.t()}

Log the error and also send it over to our friends at appsignal

Link to this function

make_set(str, separators \\ [",", ";"])

View Source
@spec make_set(String.t(), list()) :: MapSet.t()

Given a string separated by spaces, commas, or semi-colons, create a set of individual elements in the string

Link to this function

parse_maybe_integer(value)

View Source
@spec parse_maybe_integer(String.t() | integer()) ::
  {:ok, integer()} | {:ok, nil} | :error

Wrapper to return :ok/:error when parsing strings to potential integers

Link to this function

parse_maybe_integer!(value)

View Source
@spec parse_maybe_integer!(String.t() | integer()) :: integer()

parse and integer and die if parse fails

Link to this function

parse_maybe_number(value)

View Source
@spec parse_maybe_number(String.t() | integer()) ::
  {:ok, integer()} | {:ok, nil} | :error

Wrapper to return :ok/:error when parsing strings to potential integers

Link to this function

safe_string_to_atom(value, default \\ :invalid_atom)

View Source
@spec safe_string_to_atom(String.t() | atom(), atom()) :: atom()

You shouldn’t really use String.to_atom/1 on user-supplied data. The BEAM has a limit on how many different atoms you can have and they’re not garbage collected! With data coming from outside the system, stick to strings or use String.to_existing_atom/1 instead! So this is a generic function which will convert the string to atom and throws an error in case of invalid key

Link to this function

signature(organization_id, body, timestamp)

View Source
@spec signature(non_neg_integer(), String.t(), non_neg_integer()) :: String.t()

Compute the signature at a specific time for the body

@spec stacktrace() :: String.t()

easy way for glific developers to get a stacktrace when debugging

@spec string_clean(String.t() | nil) :: String.t() | nil

Lets get rid of all non valid characters. We are assuming any language and hence using unicode syntax and not restricting ourselves to alphanumeric

@spec string_snake_case(String.t() | nil) :: String.t() | nil

convert string to snake case

Link to this function

substitute_organization_id(params, value, key)

View Source
@spec substitute_organization_id(map(), any(), atom()) :: map()

Intermediary function to update the input params with organization id as operation is performed by glific_admin for other organizations

@spec suspicious_code(String.t()) :: boolean()

Really simple function to ensure folks do not add Repo and/or IO calls to an EEx snippet. This is an extremely short term fix to avoid shooting ourselves in the foot, but we should move to lua for flows scripting in the near future

Note that folks can potentially find other ways to access the same modules, so this by no means should be considered remotely secure

Link to this function

validate_shortcode(changeset)

View Source
@spec validate_shortcode(Ecto.Changeset.t()) ::
  Ecto.Changeset.t() | Ecto.Changeset.t()

Validates inputted shortcode, if shortcode is invalid it returns message that the shortcode is invalid along with the valid shortcode.

Link to this function

verify_google_captcha(token)

View Source
@spec verify_google_captcha(String.t()) :: {:ok, String.t()} | {:error, any()}

Verifying Google Captcha