# `Imison.Notifications.Channel`
[🔗](https://github.com/elpikel/imison/blob/v0.1.0/lib/imison/notifications/channel.ex#L1)

A way of delivering notifications, such as `Imison.Notifications.Channels.Email`.

List channel modules in the `channels` option; the default holds only the
email channel when Swoosh is available. A host app can add its own module
implementing this behaviour without a new Imison release.

Each channel's settings (`config`, e.g. the email recipients) are stored
in the database under its `type/0`, and a channel is used only when they
are enabled and pass `validate_config/1`. Rules and delivery records live
in Imison, not in the channels: a channel only renders and sends one
`Imison.Notifications.Message`.

The dashboard's Notifications screen shows one panel per channel, titled
by the optional `c:label/0` (else `c:type/0`), under it the optional
`c:describe/1`, and a form field per entry of the optional
`c:config_fields/0` (none without it), with an on/off switch, Save and
"Send test". Saving stores the fields' values in the channel's `config`
under their names, after `validate_config/1`; settings the fields don't
list are kept. A `:secret` field is never shown back: the screen says a
value is set, and leaving it blank keeps the stored value.

# `config_field`

```elixir
@type config_field() :: %{
  :name =&gt; String.t(),
  :label =&gt; String.t(),
  :type =&gt; :recipients | :text | :url | :secret,
  optional(:hint) =&gt; String.t()
}
```

One setting on the channel's panel. `name` is its key in the channel's
`config`; `type` decides the input and the stored value:

  * `:recipients`: a textarea of values separated by commas or new lines,
    stored as a list of strings (blanks and repeats dropped)
  * `:text`: a text input, stored as a trimmed string (removed when blank)
  * `:url`: a URL input, stored like `:text`
  * `:secret`: a password input, stored like `:text`, never shown back;
    blank keeps the stored value

`hint` is optional help shown under the field.

# `config_fields`
*optional* 

```elixir
@callback config_fields() :: [config_field()]
```

The settings the channel's panel asks for, in order; see
`t:config_field/0`. Defaults to none.

# `deliver`

```elixir
@callback deliver(
  Imison.Notifications.Message.t(),
  channel_config :: map(),
  Imison.Config.t()
) ::
  :ok | {:error, term()}
```

Delivers `message` with the channel's stored settings and the instance's
config (which holds, e.g., the host's `mailer` and the `from` address).

Returns `{:error, reason}` when it fails; Imison logs it and records the
delivery as failed. An exception is treated the same way.

Besides messages about one error, a channel receives the daily digest
(`trigger: :digest`) when the `digest` rule is on: its error fields are
`nil` and its `summary` is set (see `Imison.Notifications.Message`). A
channel must render it or skip it (return `:ok` without sending), and
must not assume a message's error fields are set.

# `describe`
*optional* 

```elixir
@callback describe(Imison.Config.t()) :: String.t()
```

A line under the panel's title, from the instance's config, e.g. which
mailer sends the email. Defaults to none.

# `label`
*optional* 

```elixir
@callback label() :: String.t()
```

The title of the channel's panel on the Notifications screen, e.g.
`"Email"`. Defaults to `c:type/0`.

# `type`

```elixir
@callback type() :: String.t()
```

Keys the channel's stored settings, so one row in `imison_channels` holds
them per channel, e.g. `"email"`.

# `validate_config`

```elixir
@callback validate_config(channel_config :: map()) :: :ok | {:error, keyword()}
```

Checks the channel's stored settings: `:ok` when it can deliver with them,
otherwise the reasons keyed by setting.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
