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