Merge branch 'issue/2069' into 'develop'
[akkoma] / lib / pleroma / application_requirements.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.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 |> handle_result()
28 end
29
30 defp handle_result(:ok), do: :ok
31 defp handle_result({:error, message}), do: raise(VerifyError, message: message)
32
33 defp check_welcome_message_config!(:ok) do
34 if Pleroma.Config.get([:welcome, :email, :enabled], false) and
35 not Pleroma.Emails.Mailer.enabled?() do
36 Logger.error("""
37 To send welcome email do you need to enable mail.
38 \nconfig :pleroma, Pleroma.Emails.Mailer, enabled: true
39 """)
40
41 {:error, "The mail disabled."}
42 else
43 :ok
44 end
45 end
46
47 defp check_welcome_message_config!(result), do: result
48
49 # Checks account confirmation email
50 #
51 def check_confirmation_accounts!(:ok) do
52 if Pleroma.Config.get([:instance, :account_activation_required]) &&
53 not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
54 Logger.error(
55 "Account activation enabled, but no Mailer settings enabled.\n" <>
56 "Please set config :pleroma, :instance, account_activation_required: false\n" <>
57 "Otherwise setup and enable Mailer."
58 )
59
60 {:error,
61 "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."}
62 else
63 :ok
64 end
65 end
66
67 def check_confirmation_accounts!(result), do: result
68
69 # Checks for pending migrations.
70 #
71 def check_migrations_applied!(:ok) do
72 unless Pleroma.Config.get(
73 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
74 false
75 ) do
76 {_, res, _} =
77 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
78 down_migrations =
79 Ecto.Migrator.migrations(repo)
80 |> Enum.reject(fn
81 {:up, _, _} -> true
82 {:down, _, _} -> false
83 end)
84
85 if length(down_migrations) > 0 do
86 down_migrations_text =
87 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
88
89 Logger.error(
90 "The following migrations were not applied:\n#{down_migrations_text}" <>
91 "If you want to start Pleroma anyway, set\n" <>
92 "config :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
93 )
94
95 {:error, "Unapplied Migrations detected"}
96 else
97 :ok
98 end
99 end)
100
101 res
102 else
103 :ok
104 end
105 end
106
107 def check_migrations_applied!(result), do: result
108
109 # Checks for settings of RUM indexes.
110 #
111 defp check_rum!(:ok) do
112 {_, res, _} =
113 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
114 migrate =
115 from(o in "columns",
116 where: o.table_name == "objects",
117 where: o.column_name == "fts_content"
118 )
119 |> repo.exists?(prefix: "information_schema")
120
121 setting = Pleroma.Config.get([:database, :rum_enabled], false)
122
123 do_check_rum!(setting, migrate)
124 end)
125
126 res
127 end
128
129 defp check_rum!(result), do: result
130
131 defp do_check_rum!(setting, migrate) do
132 case {setting, migrate} do
133 {true, false} ->
134 Logger.error(
135 "Use `RUM` index is enabled, but were not applied migrations for it.\n" <>
136 "If you want to start Pleroma anyway, set\n" <>
137 "config :pleroma, :database, rum_enabled: false\n" <>
138 "Otherwise apply the following migrations:\n" <>
139 "`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
140 )
141
142 {:error, "Unapplied RUM Migrations detected"}
143
144 {false, true} ->
145 Logger.error(
146 "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\n" <>
147 "If you want to use `RUM`, set\n" <>
148 "config :pleroma, :database, rum_enabled: true\n" <>
149 "Otherwise roll `RUM` migrations back.\n" <>
150 "`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
151 )
152
153 {:error, "RUM Migrations detected"}
154
155 _ ->
156 :ok
157 end
158 end
159
160 defp check_system_commands!(:ok) do
161 filter_commands_statuses = [
162 check_filter(Pleroma.Upload.Filters.Exiftool, "exiftool"),
163 check_filter(Pleroma.Upload.Filters.Mogrify, "mogrify"),
164 check_filter(Pleroma.Upload.Filters.Mogrifun, "mogrify")
165 ]
166
167 preview_proxy_commands_status =
168 if !Config.get([:media_preview_proxy, :enabled]) or
169 MediaHelper.missing_dependencies() == [] do
170 true
171 else
172 Logger.error(
173 "The following dependencies required by Media preview proxy " <>
174 "(which is currently enabled) are not installed: " <>
175 inspect(MediaHelper.missing_dependencies())
176 )
177
178 false
179 end
180
181 if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
182 :ok
183 else
184 {:error,
185 "System commands missing. Check logs and see `docs/installation` for more details."}
186 end
187 end
188
189 defp check_system_commands!(result), do: result
190
191 defp check_filter(filter, command_required) do
192 filters = Config.get([Pleroma.Upload, :filters])
193
194 if filter in filters and not Pleroma.Utils.command_available?(command_required) do
195 Logger.error(
196 "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
197 "#{command_required} command is not found"
198 )
199
200 false
201 else
202 true
203 end
204 end
205 end