First
[anni] / lib / pleroma / emails / admin_email.ex
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
5 defmodule Pleroma.Emails.AdminEmail do
6   @moduledoc "Admin emails"
7
8   import Swoosh.Email
9
10   alias Pleroma.Config
11   alias Pleroma.HTML
12   alias Pleroma.Web.Router.Helpers
13
14   defp instance_config, do: Config.get(:instance)
15   defp instance_name, do: instance_config()[:name]
16
17   defp instance_notify_email do
18     Keyword.get(instance_config(), :notify_email, instance_config()[:email])
19   end
20
21   def test_email(mail_to \\ nil) do
22     html_body = """
23     <h3>Instance Test Email</h3>
24     <p>A test email was requested. Hello. :)</p>
25     """
26
27     new()
28     |> to(mail_to || Config.get([:instance, :email]))
29     |> from({instance_name(), instance_notify_email()})
30     |> subject("Instance Test Email")
31     |> html_body(html_body)
32   end
33
34   def report(to, reporter, account, statuses, comment) do
35     comment_html =
36       if comment do
37         "<p>Comment: #{comment}"
38       else
39         ""
40       end
41
42     statuses_html =
43       if is_list(statuses) && length(statuses) > 0 do
44         statuses_list_html =
45           statuses
46           |> Enum.map(fn
47             %{id: id} ->
48               status_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, id)
49               "<li><a href=\"#{status_url}\">#{status_url}</li>"
50
51             %{"id" => id} when is_binary(id) ->
52               "<li><a href=\"#{id}\">#{id}</li>"
53
54             id when is_binary(id) ->
55               "<li><a href=\"#{id}\">#{id}</li>"
56           end)
57           |> Enum.join("\n")
58
59         """
60         <p> Statuses:
61           <ul>
62             #{statuses_list_html}
63           </ul>
64         </p>
65         """
66       else
67         ""
68       end
69
70     html_body = """
71     <p>Reported by: <a href="#{reporter.ap_id}">#{reporter.nickname}</a></p>
72     <p>Reported Account: <a href="#{account.ap_id}">#{account.nickname}</a></p>
73     #{comment_html}
74     #{statuses_html}
75     <p>
76     <a href="#{Pleroma.Web.Endpoint.url()}/pleroma/admin/#/reports/index">View Reports in AdminFE</a>
77     """
78
79     new()
80     |> to({to.name, to.email})
81     |> from({instance_name(), instance_notify_email()})
82     |> subject("#{instance_name()} Report")
83     |> html_body(html_body)
84   end
85
86   def new_unapproved_registration(to, account) do
87     html_body = """
88     <p>New account for review: <a href="#{account.ap_id}">@#{account.nickname}</a></p>
89     <blockquote>#{HTML.strip_tags(account.registration_reason)}</blockquote>
90     <a href="#{Pleroma.Web.Endpoint.url()}/pleroma/admin/#/users/#{account.id}/">Visit AdminFE</a>
91     """
92
93     new()
94     |> to({to.name, to.email})
95     |> from({instance_name(), instance_notify_email()})
96     |> subject("New account up for review on #{instance_name()} (@#{account.nickname})")
97     |> html_body(html_body)
98   end
99 end