2021-03-23 14:18:03 +00:00
|
|
|
defmodule Mobilizon.Service.Notifier do
|
|
|
|
@moduledoc """
|
|
|
|
Behaviour for notifiers
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Activities.Activity
|
|
|
|
alias Mobilizon.Config
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Whether the notifier is enabled and configured
|
|
|
|
"""
|
|
|
|
@callback ready?() :: boolean()
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sends one or multiple notifications from an activity
|
|
|
|
"""
|
2021-05-06 10:27:04 +00:00
|
|
|
@callback send(User.t(), Activity.t(), Keyword.t()) :: {:ok, any()} | {:error, String.t()}
|
2021-03-23 14:18:03 +00:00
|
|
|
|
2021-05-06 10:27:04 +00:00
|
|
|
@callback send(User.t(), list(Activity.t()), Keyword.t()) :: {:ok, any()} | {:error, String.t()}
|
2021-03-23 14:18:03 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec notify(User.t(), Activity.t(), Keyword.t()) :: :ok
|
2021-03-23 14:18:03 +00:00
|
|
|
def notify(%User{} = user, %Activity{} = activity, opts \\ []) do
|
2021-05-06 10:27:04 +00:00
|
|
|
Enum.each(providers(opts), & &1.send(user, activity, opts))
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec providers(Keyword.t()) :: list()
|
|
|
|
defp providers(opts) do
|
|
|
|
opts
|
|
|
|
|> Keyword.get(:notifiers, Config.get([__MODULE__, :notifiers]))
|
|
|
|
|> Enum.filter(& &1.ready?())
|
|
|
|
end
|
|
|
|
end
|