53062b961e38ec881c5163184b3d6e991d821911
[akkoma] / lib / pleroma / user / welcome_email.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.Utils, 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_nickname], 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 def add_sender(opts, nickname) do
44 nickname
45 |> fetch_sender()
46 |> merge_options(opts, :sender)
47 end
48
49 defp merge_options(nil, options, _option), do: options
50
51 defp merge_options(value, options, option) do
52 Map.merge(options, %{option => value})
53 end
54
55 defp fetch_sender(nickname) when is_binary(nickname) do
56 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
57 {instance_name(), user.email}
58 else
59 _ -> nil
60 end
61 end
62
63 defp fetch_sender(_), do: nil
64
65 defp eval_string(nil, _), do: nil
66 defp eval_string("", _), do: nil
67 defp eval_string(str, bindings), do: EEx.eval_string(str, bindings)
68 end