Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / application_requirements.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.Filters.Exiftool, "exiftool"),
168 check_filter(Pleroma.Upload.Filters.Mogrify, "mogrify"),
169 check_filter(Pleroma.Upload.Filters.Mogrifun, "mogrify")
170 ]
171
172 preview_proxy_commands_status =
173 if !Config.get([:media_preview_proxy, :enabled]) or
174 MediaHelper.missing_dependencies() == [] do
175 true
176 else
177 Logger.error(
178 "The following dependencies required by Media preview proxy " <>
179 "(which is currently enabled) are not installed: " <>
180 inspect(MediaHelper.missing_dependencies())
181 )
182
183 false
184 end
185
186 if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
187 :ok
188 else
189 {:error,
190 "System commands missing. Check logs and see `docs/installation` for more details."}
191 end
192 end
193
194 defp check_system_commands!(result), do: result
195
196 defp check_repo_pool_size!(:ok) do
197 if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and
198 not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do
199 Logger.error("""
200 !!!CONFIG WARNING!!!
201
202 The database pool size has been altered from the recommended value of 10.
203
204 Please revert or ensure your database is tuned appropriately and then set
205 `config :pleroma, :dangerzone, override_repo_pool_size: true`.
206
207 If you are experiencing database timeouts, please check the "Optimizing
208 your PostgreSQL performance" section in the documentation. If you still
209 encounter issues after that, please open an issue on the tracker.
210 """)
211
212 {:error, "Repo.pool_size different than recommended value."}
213 else
214 :ok
215 end
216 end
217
218 defp check_repo_pool_size!(result), do: result
219
220 defp check_filter(filter, command_required) do
221 filters = Config.get([Pleroma.Upload, :filters])
222
223 if filter in filters and not Pleroma.Utils.command_available?(command_required) do
224 Logger.error(
225 "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
226 "#{command_required} command is not found"
227 )
228
229 false
230 else
231 true
232 end
233 end
234 end