remove `unread_conversation_count` from User
[akkoma] / lib / pleroma / application.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.Application do
6 use Application
7
8 import Cachex.Spec
9
10 alias Pleroma.Config
11
12 require Logger
13
14 @name Mix.Project.config()[:name]
15 @version Mix.Project.config()[:version]
16 @repository Mix.Project.config()[:source_url]
17 @env Mix.env()
18
19 def name, do: @name
20 def version, do: @version
21 def named_version, do: @name <> " " <> @version
22 def repository, do: @repository
23
24 def user_agent do
25 case Config.get([:http, :user_agent], :default) do
26 :default ->
27 info = "#{Pleroma.Web.base_url()} <#{Config.get([:instance, :email], "")}>"
28 named_version() <> "; " <> info
29
30 custom ->
31 custom
32 end
33 end
34
35 # See http://elixir-lang.org/docs/stable/elixir/Application.html
36 # for more information on OTP Applications
37 def start(_type, _args) do
38 # Scrubbers are compiled at runtime and therefore will cause a conflict
39 # every time the application is restarted, so we disable module
40 # conflicts at runtime
41 Code.compiler_options(ignore_module_conflict: true)
42 Pleroma.Telemetry.Logger.attach()
43 Config.Holder.save_default()
44 Pleroma.HTML.compile_scrubbers()
45 Config.DeprecationWarnings.warn()
46 Pleroma.Plugs.HTTPSecurityPlug.warn_if_disabled()
47 Pleroma.ApplicationRequirements.verify!()
48 setup_instrumenters()
49 load_custom_modules()
50 check_system_commands()
51 Pleroma.Docs.JSON.compile()
52
53 adapter = Application.get_env(:tesla, :adapter)
54
55 if adapter == Tesla.Adapter.Gun do
56 if version = Pleroma.OTPVersion.version() do
57 [major, minor] =
58 version
59 |> String.split(".")
60 |> Enum.map(&String.to_integer/1)
61 |> Enum.take(2)
62
63 if (major == 22 and minor < 2) or major < 22 do
64 raise "
65 !!!OTP VERSION WARNING!!!
66 You are using gun adapter with OTP version #{version}, which doesn't support correct handling of unordered certificates chains. Please update your Erlang/OTP to at least 22.2.
67 "
68 end
69 else
70 raise "
71 !!!OTP VERSION WARNING!!!
72 To support correct handling of unordered certificates chains - OTP version must be > 22.2.
73 "
74 end
75 end
76
77 # Define workers and child supervisors to be supervised
78 children =
79 [
80 Pleroma.Repo,
81 Config.TransferTask,
82 Pleroma.Emoji,
83 Pleroma.Plugs.RateLimiter.Supervisor
84 ] ++
85 cachex_children() ++
86 http_children(adapter, @env) ++
87 [
88 Pleroma.Stats,
89 Pleroma.JobQueueMonitor,
90 {Oban, Config.get(Oban)}
91 ] ++
92 task_children(@env) ++
93 streamer_child(@env) ++
94 chat_child(@env, chat_enabled?()) ++
95 [
96 Pleroma.Web.Endpoint,
97 Pleroma.Gopher.Server
98 ]
99
100 # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
101 # for other strategies and supported options
102 opts = [strategy: :one_for_one, name: Pleroma.Supervisor]
103 Supervisor.start_link(children, opts)
104 end
105
106 def load_custom_modules do
107 dir = Config.get([:modules, :runtime_dir])
108
109 if dir && File.exists?(dir) do
110 dir
111 |> Pleroma.Utils.compile_dir()
112 |> case do
113 {:error, _errors, _warnings} ->
114 raise "Invalid custom modules"
115
116 {:ok, modules, _warnings} ->
117 if @env != :test do
118 Enum.each(modules, fn mod ->
119 Logger.info("Custom module loaded: #{inspect(mod)}")
120 end)
121 end
122
123 :ok
124 end
125 end
126 end
127
128 defp setup_instrumenters do
129 require Prometheus.Registry
130
131 if Application.get_env(:prometheus, Pleroma.Repo.Instrumenter) do
132 :ok =
133 :telemetry.attach(
134 "prometheus-ecto",
135 [:pleroma, :repo, :query],
136 &Pleroma.Repo.Instrumenter.handle_event/4,
137 %{}
138 )
139
140 Pleroma.Repo.Instrumenter.setup()
141 end
142
143 Pleroma.Web.Endpoint.MetricsExporter.setup()
144 Pleroma.Web.Endpoint.PipelineInstrumenter.setup()
145 Pleroma.Web.Endpoint.Instrumenter.setup()
146 end
147
148 defp cachex_children do
149 [
150 build_cachex("used_captcha", ttl_interval: seconds_valid_interval()),
151 build_cachex("user", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
152 build_cachex("object", default_ttl: 25_000, ttl_interval: 1000, limit: 2500),
153 build_cachex("rich_media", default_ttl: :timer.minutes(120), limit: 5000),
154 build_cachex("scrubber", limit: 2500),
155 build_cachex("idempotency", expiration: idempotency_expiration(), limit: 2500),
156 build_cachex("web_resp", limit: 2500),
157 build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
158 build_cachex("failed_proxy_url", limit: 2500),
159 build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
160 ]
161 end
162
163 defp emoji_packs_expiration,
164 do: expiration(default: :timer.seconds(5 * 60), interval: :timer.seconds(60))
165
166 defp idempotency_expiration,
167 do: expiration(default: :timer.seconds(6 * 60 * 60), interval: :timer.seconds(60))
168
169 defp seconds_valid_interval,
170 do: :timer.seconds(Config.get!([Pleroma.Captcha, :seconds_valid]))
171
172 @spec build_cachex(String.t(), keyword()) :: map()
173 def build_cachex(type, opts),
174 do: %{
175 id: String.to_atom("cachex_" <> type),
176 start: {Cachex, :start_link, [String.to_atom(type <> "_cache"), opts]},
177 type: :worker
178 }
179
180 defp chat_enabled?, do: Config.get([:chat, :enabled])
181
182 defp streamer_child(env) when env in [:test, :benchmark], do: []
183
184 defp streamer_child(_) do
185 [
186 {Registry,
187 [
188 name: Pleroma.Web.Streamer.registry(),
189 keys: :duplicate,
190 partitions: System.schedulers_online()
191 ]}
192 ]
193 end
194
195 defp chat_child(_env, true) do
196 [Pleroma.Web.ChatChannel.ChatChannelState]
197 end
198
199 defp chat_child(_, _), do: []
200
201 defp task_children(:test) do
202 [
203 %{
204 id: :web_push_init,
205 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
206 restart: :temporary
207 }
208 ]
209 end
210
211 defp task_children(_) do
212 [
213 %{
214 id: :web_push_init,
215 start: {Task, :start_link, [&Pleroma.Web.Push.init/0]},
216 restart: :temporary
217 },
218 %{
219 id: :internal_fetch_init,
220 start: {Task, :start_link, [&Pleroma.Web.ActivityPub.InternalFetchActor.init/0]},
221 restart: :temporary
222 }
223 ]
224 end
225
226 # start hackney and gun pools in tests
227 defp http_children(_, :test) do
228 http_children(Tesla.Adapter.Hackney, nil) ++ http_children(Tesla.Adapter.Gun, nil)
229 end
230
231 defp http_children(Tesla.Adapter.Hackney, _) do
232 pools = [:federation, :media]
233
234 pools =
235 if Config.get([Pleroma.Upload, :proxy_remote]) do
236 [:upload | pools]
237 else
238 pools
239 end
240
241 for pool <- pools do
242 options = Config.get([:hackney_pools, pool])
243 :hackney_pool.child_spec(pool, options)
244 end
245 end
246
247 defp http_children(Tesla.Adapter.Gun, _) do
248 Pleroma.Gun.ConnectionPool.children() ++
249 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
250 end
251
252 defp http_children(_, _), do: []
253
254 defp check_system_commands do
255 filters = Config.get([Pleroma.Upload, :filters])
256
257 check_filter = fn filter, command_required ->
258 with true <- filter in filters,
259 false <- Pleroma.Utils.command_available?(command_required) do
260 Logger.error(
261 "#{filter} is specified in list of Pleroma.Upload filters, but the #{command_required} command is not found"
262 )
263 end
264 end
265
266 check_filter.(Pleroma.Upload.Filters.Exiftool, "exiftool")
267 check_filter.(Pleroma.Upload.Filters.Mogrify, "mogrify")
268 check_filter.(Pleroma.Upload.Filters.Mogrifun, "mogrify")
269 end
270 end