Merge remote-tracking branch 'pleroma/develop' into features/poll-validation
[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 import Ecto.Query
13
14 require Logger
15
16 @spec verify!() :: :ok | VerifyError.t()
17 def verify! do
18 :ok
19 |> check_confirmation_accounts!
20 |> check_migrations_applied!()
21 |> check_welcome_message_config!()
22 |> check_rum!()
23 |> handle_result()
24 end
25
26 defp handle_result(:ok), do: :ok
27 defp handle_result({:error, message}), do: raise(VerifyError, message: message)
28
29 defp check_welcome_message_config!(:ok) do
30 if Pleroma.Config.get([:welcome, :email, :enabled], false) and
31 not Pleroma.Emails.Mailer.enabled?() do
32 Logger.error("""
33 To send welcome email do you need to enable mail.
34 \nconfig :pleroma, Pleroma.Emails.Mailer, enabled: true
35 """)
36
37 {:error, "The mail disabled."}
38 else
39 :ok
40 end
41 end
42
43 defp check_welcome_message_config!(result), do: result
44
45 # Checks account confirmation email
46 #
47 def check_confirmation_accounts!(:ok) do
48 if Pleroma.Config.get([:instance, :account_activation_required]) &&
49 not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
50 Logger.error(
51 "Account activation enabled, but no Mailer settings enabled.\nPlease set config :pleroma, :instance, account_activation_required: false\nOtherwise setup and enable Mailer."
52 )
53
54 {:error,
55 "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."}
56 else
57 :ok
58 end
59 end
60
61 def check_confirmation_accounts!(result), do: result
62
63 # Checks for pending migrations.
64 #
65 def check_migrations_applied!(:ok) do
66 unless Pleroma.Config.get(
67 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
68 false
69 ) do
70 {_, res, _} =
71 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
72 down_migrations =
73 Ecto.Migrator.migrations(repo)
74 |> Enum.reject(fn
75 {:up, _, _} -> true
76 {:down, _, _} -> false
77 end)
78
79 if length(down_migrations) > 0 do
80 down_migrations_text =
81 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
82
83 Logger.error(
84 "The following migrations were not applied:\n#{down_migrations_text}If you want to start Pleroma anyway, set\nconfig :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
85 )
86
87 {:error, "Unapplied Migrations detected"}
88 else
89 :ok
90 end
91 end)
92
93 res
94 else
95 :ok
96 end
97 end
98
99 def check_migrations_applied!(result), do: result
100
101 # Checks for settings of RUM indexes.
102 #
103 defp check_rum!(:ok) do
104 {_, res, _} =
105 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
106 migrate =
107 from(o in "columns",
108 where: o.table_name == "objects",
109 where: o.column_name == "fts_content"
110 )
111 |> repo.exists?(prefix: "information_schema")
112
113 setting = Pleroma.Config.get([:database, :rum_enabled], false)
114
115 do_check_rum!(setting, migrate)
116 end)
117
118 res
119 end
120
121 defp check_rum!(result), do: result
122
123 defp do_check_rum!(setting, migrate) do
124 case {setting, migrate} do
125 {true, false} ->
126 Logger.error(
127 "Use `RUM` index is enabled, but were not applied migrations for it.\nIf you want to start Pleroma anyway, set\nconfig :pleroma, :database, rum_enabled: false\nOtherwise apply the following migrations:\n`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
128 )
129
130 {:error, "Unapplied RUM Migrations detected"}
131
132 {false, true} ->
133 Logger.error(
134 "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\nIf you want to use `RUM`, set\nconfig :pleroma, :database, rum_enabled: true\nOtherwise roll `RUM` migrations back.\n`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
135 )
136
137 {:error, "RUM Migrations detected"}
138
139 _ ->
140 :ok
141 end
142 end
143 end