total rebase
[anni] / lib / pleroma / application_requirements.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.ApplicationRequirements do
6   @moduledoc """
7   The module represents the collection of validations to runs before start server.
8   """
9
10   defmodule VerifyError do
11     defexception([:message])
12     @type t :: %__MODULE__{}
13   end
14
15   alias Pleroma.Config
16   alias Pleroma.Helpers.MediaHelper
17
18   import Ecto.Query
19
20   require Logger
21
22   @spec verify!() :: :ok | VerifyError.t()
23   def verify! do
24     :ok
25     |> check_system_commands!()
26     |> check_confirmation_accounts!()
27     |> check_migrations_applied!()
28     |> check_welcome_message_config!()
29     |> check_rum!()
30     |> check_repo_pool_size!()
31     |> handle_result()
32   end
33
34   defp handle_result(:ok), do: :ok
35   defp handle_result({:error, message}), do: raise(VerifyError, message: message)
36
37   defp check_welcome_message_config!(:ok) do
38     if Pleroma.Config.get([:welcome, :email, :enabled], false) and
39          not Pleroma.Emails.Mailer.enabled?() do
40       Logger.warning("""
41       To send welcome emails, you need to enable the mailer.
42       Welcome emails will NOT be sent with the current config.
43
44       Enable the mailer:
45         config :pleroma, Pleroma.Emails.Mailer, enabled: true
46       """)
47     end
48
49     :ok
50   end
51
52   defp check_welcome_message_config!(result), do: result
53
54   # Checks account confirmation email
55   #
56   def check_confirmation_accounts!(:ok) do
57     if Pleroma.Config.get([:instance, :account_activation_required]) &&
58          not Pleroma.Emails.Mailer.enabled?() do
59       Logger.warning("""
60       Account activation is required, but the mailer is disabled.
61       Users will NOT be able to confirm their accounts with this config.
62       Either disable account activation or enable the mailer.
63
64       Disable account activation:
65         config :pleroma, :instance, account_activation_required: false
66
67       Enable the mailer:
68         config :pleroma, Pleroma.Emails.Mailer, enabled: true
69       """)
70     end
71
72     :ok
73   end
74
75   def check_confirmation_accounts!(result), do: result
76
77   # Checks for pending migrations.
78   #
79   def check_migrations_applied!(:ok) do
80     unless Pleroma.Config.get(
81              [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
82              false
83            ) do
84       {_, res, _} =
85         Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
86           down_migrations =
87             Ecto.Migrator.migrations(repo)
88             |> Enum.reject(fn
89               {:up, _, _} -> true
90               {:down, _, _} -> false
91             end)
92
93           if length(down_migrations) > 0 do
94             down_migrations_text =
95               Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
96
97             Logger.error(
98               "The following migrations were not applied:\n#{down_migrations_text}" <>
99                 "If you want to start Pleroma anyway, set\n" <>
100                 "config :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
101             )
102
103             {:error, "Unapplied Migrations detected"}
104           else
105             :ok
106           end
107         end)
108
109       res
110     else
111       :ok
112     end
113   end
114
115   def check_migrations_applied!(result), do: result
116
117   # Checks for settings of RUM indexes.
118   #
119   defp check_rum!(:ok) do
120     {_, res, _} =
121       Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
122         migrate =
123           from(o in "columns",
124             where: o.table_name == "objects",
125             where: o.column_name == "fts_content"
126           )
127           |> repo.exists?(prefix: "information_schema")
128
129         setting = Pleroma.Config.get([:database, :rum_enabled], false)
130
131         do_check_rum!(setting, migrate)
132       end)
133
134     res
135   end
136
137   defp check_rum!(result), do: result
138
139   defp do_check_rum!(setting, migrate) do
140     case {setting, migrate} do
141       {true, false} ->
142         Logger.error(
143           "Use `RUM` index is enabled, but were not applied migrations for it.\n" <>
144             "If you want to start Pleroma anyway, set\n" <>
145             "config :pleroma, :database, rum_enabled: false\n" <>
146             "Otherwise apply the following migrations:\n" <>
147             "`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
148         )
149
150         {:error, "Unapplied RUM Migrations detected"}
151
152       {false, true} ->
153         Logger.error(
154           "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\n" <>
155             "If you want to use `RUM`, set\n" <>
156             "config :pleroma, :database, rum_enabled: true\n" <>
157             "Otherwise roll `RUM` migrations back.\n" <>
158             "`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
159         )
160
161         {:error, "RUM Migrations detected"}
162
163       _ ->
164         :ok
165     end
166   end
167
168   defp check_system_commands!(:ok) do
169     filter_commands_statuses = [
170       check_filter(Pleroma.Upload.Filter.Exiftool.StripLocation, "exiftool"),
171       check_filter(Pleroma.Upload.Filter.Exiftool.ReadDescription, "exiftool"),
172       check_filter(Pleroma.Upload.Filter.Mogrify, "mogrify"),
173       check_filter(Pleroma.Upload.Filter.Mogrifun, "mogrify"),
174       check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "ffprobe")
175     ]
176
177     preview_proxy_commands_status =
178       if !Config.get([:media_preview_proxy, :enabled]) or
179            MediaHelper.missing_dependencies() == [] do
180         true
181       else
182         Logger.error(
183           "The following dependencies required by Media preview proxy " <>
184             "(which is currently enabled) are not installed: " <>
185             inspect(MediaHelper.missing_dependencies())
186         )
187
188         false
189       end
190
191     if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
192       :ok
193     else
194       {:error,
195        "System commands missing. Check logs and see `docs/installation` for more details."}
196     end
197   end
198
199   defp check_repo_pool_size!(:ok) do
200     if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and
201          not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do
202       Logger.error("""
203       !!!CONFIG WARNING!!!
204
205       The database pool size has been altered from the recommended value of 10.
206
207       Please revert or ensure your database is tuned appropriately and then set
208       `config :pleroma, :dangerzone, override_repo_pool_size: true`.
209
210       If you are experiencing database timeouts, please check the "Optimizing
211       your PostgreSQL performance" section in the documentation. If you still
212       encounter issues after that, please open an issue on the tracker.
213       """)
214
215       {:error, "Repo.pool_size different than recommended value."}
216     else
217       :ok
218     end
219   end
220
221   defp check_repo_pool_size!(result), do: result
222
223   defp check_filter(filter, command_required) do
224     filters = Config.get([Pleroma.Upload, :filters])
225
226     if filter in filters and not Pleroma.Utils.command_available?(command_required) do
227       Logger.error(
228         "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
229           "#{command_required} command is not found"
230       )
231
232       false
233     else
234       true
235     end
236   end
237 end