Merge branch 'patch-3' into 'develop'
[akkoma] / lib / pleroma / emails / admin_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.Emails.AdminEmail do
6 @moduledoc "Admin emails"
7
8 import Swoosh.Email
9
10 alias Pleroma.Config
11 alias Pleroma.Web.Router.Helpers
12
13 defp instance_config, do: Config.get(:instance)
14 defp instance_name, do: instance_config()[:name]
15
16 defp instance_notify_email do
17 Keyword.get(instance_config(), :notify_email, instance_config()[:email])
18 end
19
20 defp user_url(user) do
21 Helpers.user_feed_url(Pleroma.Web.Endpoint, :feed_redirect, user.id)
22 end
23
24 def test_email(mail_to \\ nil) do
25 html_body = """
26 <h3>Instance Test Email</h3>
27 <p>A test email was requested. Hello. :)</p>
28 """
29
30 new()
31 |> to(mail_to || Config.get([:instance, :email]))
32 |> from({instance_name(), instance_notify_email()})
33 |> subject("Instance Test Email")
34 |> html_body(html_body)
35 end
36
37 def report(to, reporter, account, statuses, comment) do
38 comment_html =
39 if comment do
40 "<p>Comment: #{comment}"
41 else
42 ""
43 end
44
45 statuses_html =
46 if is_list(statuses) && length(statuses) > 0 do
47 statuses_list_html =
48 statuses
49 |> Enum.map(fn
50 %{id: id} ->
51 status_url = Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, id)
52 "<li><a href=\"#{status_url}\">#{status_url}</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="#{user_url(reporter)}">#{reporter.nickname}</a></p>
72 <p>Reported Account: <a href="#{user_url(account)}">#{account.nickname}</a></p>
73 #{comment_html}
74 #{statuses_html}
75 <p>
76 <a href="#{Pleroma.Web.base_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 end