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