logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

welcome_email.ex (1706B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.User.WelcomeEmail do
  5. @moduledoc """
  6. The module represents the functions to send welcome email.
  7. """
  8. alias Pleroma.Config
  9. alias Pleroma.Emails
  10. alias Pleroma.User
  11. import Pleroma.Config.Helpers, only: [instance_name: 0]
  12. @spec enabled?() :: boolean()
  13. def enabled?, do: Config.get([:welcome, :email, :enabled], false)
  14. @spec send_email(User.t()) :: {:ok, Oban.Job.t()}
  15. def send_email(%User{} = user) do
  16. user
  17. |> Emails.UserEmail.welcome(email_options(user))
  18. |> Emails.Mailer.deliver_async()
  19. end
  20. defp email_options(user) do
  21. bindings = [user: user, instance_name: instance_name()]
  22. %{}
  23. |> add_sender(Config.get([:welcome, :email, :sender], nil))
  24. |> add_option(:subject, bindings)
  25. |> add_option(:html, bindings)
  26. |> add_option(:text, bindings)
  27. end
  28. defp add_option(opts, option, bindings) do
  29. [:welcome, :email, option]
  30. |> Config.get(nil)
  31. |> eval_string(bindings)
  32. |> merge_options(opts, option)
  33. end
  34. defp add_sender(opts, {_name, _email} = sender) do
  35. merge_options(sender, opts, :sender)
  36. end
  37. defp add_sender(opts, sender) when is_binary(sender) do
  38. add_sender(opts, {instance_name(), sender})
  39. end
  40. defp add_sender(opts, _), do: opts
  41. defp merge_options(nil, options, _option), do: options
  42. defp merge_options(value, options, option) do
  43. Map.merge(options, %{option => value})
  44. end
  45. defp eval_string(nil, _), do: nil
  46. defp eval_string("", _), do: nil
  47. defp eval_string(str, bindings), do: EEx.eval_string(str, bindings)
  48. end