2018-10-11 15:37:39 +00:00
|
|
|
defmodule Mobilizon do
|
2017-12-08 08:58:14 +00:00
|
|
|
@moduledoc """
|
2019-09-07 23:49:56 +00:00
|
|
|
Mobilizon is a decentralized and federated Meetup-like using
|
|
|
|
[ActivityPub](http://activitypub.rocks/).
|
2017-12-08 08:58:14 +00:00
|
|
|
|
2019-09-07 23:49:56 +00:00
|
|
|
It consists of an API server build with [Elixir](http://elixir-lang.github.io/)
|
|
|
|
and the [Phoenix Framework](https://hexdocs.pm/phoenix).
|
2018-01-14 16:56:50 +00:00
|
|
|
|
2019-09-07 23:49:56 +00:00
|
|
|
Mobilizon relies on `Guardian` for auth and `Geo`/Postgis for geographical
|
|
|
|
information.
|
2017-12-08 08:58:14 +00:00
|
|
|
"""
|
2019-09-15 23:13:20 +00:00
|
|
|
|
|
|
|
use Application
|
|
|
|
|
|
|
|
import Cachex.Spec
|
|
|
|
|
2020-01-28 18:18:33 +00:00
|
|
|
alias Mobilizon.{Config, Storage, Web}
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2021-06-27 11:15:24 +00:00
|
|
|
alias Mobilizon.Service.{ErrorPage, ErrorReporting}
|
2019-09-15 23:13:20 +00:00
|
|
|
alias Mobilizon.Service.Export.{Feed, ICalendar}
|
|
|
|
|
|
|
|
@name Mix.Project.config()[:name]
|
|
|
|
@version Mix.Project.config()[:version]
|
2022-09-21 06:53:50 +00:00
|
|
|
@env Application.compile_env(:mobilizon, :env)
|
2019-09-15 23:13:20 +00:00
|
|
|
|
|
|
|
@spec named_version :: String.t()
|
|
|
|
def named_version, do: "#{@name} #{@version}"
|
|
|
|
|
2019-09-22 16:29:13 +00:00
|
|
|
@spec user_agent :: String.t()
|
2019-09-15 23:13:20 +00:00
|
|
|
def user_agent do
|
2020-01-28 18:18:33 +00:00
|
|
|
info = "#{Web.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
|
2019-09-15 23:13:20 +00:00
|
|
|
|
|
|
|
"#{named_version()}; #{info}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec start(:normal | {:takeover, node} | {:failover, node}, term) ::
|
|
|
|
{:ok, pid} | {:ok, pid, term} | {:error, term}
|
|
|
|
def start(_type, _args) do
|
2019-12-20 12:04:34 +00:00
|
|
|
children =
|
|
|
|
[
|
|
|
|
# supervisors
|
|
|
|
Storage.Repo,
|
2020-02-18 07:57:00 +00:00
|
|
|
{Phoenix.PubSub, name: Mobilizon.PubSub},
|
2019-12-20 12:04:34 +00:00
|
|
|
Web.Endpoint,
|
2020-02-18 07:57:00 +00:00
|
|
|
{Absinthe.Subscription, Web.Endpoint},
|
2019-12-20 12:04:34 +00:00
|
|
|
{Oban, Application.get_env(:mobilizon, Oban)},
|
|
|
|
# workers
|
2023-11-21 15:03:22 +00:00
|
|
|
Guardian.DB.Sweeper,
|
2019-12-20 12:04:34 +00:00
|
|
|
ActivityPub.Federator,
|
2021-10-10 14:25:50 +00:00
|
|
|
TzWorld.Backend.DetsWithIndexCache,
|
2019-12-20 12:04:34 +00:00
|
|
|
cachex_spec(:feed, 2500, 60, 60, &Feed.create_cache/1),
|
|
|
|
cachex_spec(:ics, 2500, 60, 60, &ICalendar.create_cache/1),
|
2020-02-14 16:56:36 +00:00
|
|
|
cachex_spec(
|
|
|
|
:actor_key_rotation,
|
|
|
|
2500,
|
|
|
|
div(Application.get_env(:mobilizon, :activitypub)[:actor_key_rotation_delay], 60),
|
|
|
|
60 * 30
|
|
|
|
),
|
2019-12-20 12:04:34 +00:00
|
|
|
cachex_spec(:statistics, 10, 60, 60),
|
|
|
|
cachex_spec(:config, 10, 60, 60),
|
2020-02-18 07:57:00 +00:00
|
|
|
cachex_spec(:rich_media_cache, 10, 60, 60),
|
2020-11-26 10:41:13 +00:00
|
|
|
cachex_spec(:activity_pub, 2500, 3, 15),
|
2021-09-10 09:35:32 +00:00
|
|
|
cachex_spec(:default_actors, 2500, 3, 15),
|
2020-11-26 10:41:13 +00:00
|
|
|
%{
|
|
|
|
id: :cache_key_value,
|
|
|
|
start: {Cachex, :start_link, [:key_value]}
|
|
|
|
}
|
2019-12-20 12:04:34 +00:00
|
|
|
] ++
|
|
|
|
task_children(@env)
|
2019-09-15 23:13:20 +00:00
|
|
|
|
2021-11-08 11:14:13 +00:00
|
|
|
children =
|
|
|
|
if Mobilizon.PythonPort.python_exists?() do
|
|
|
|
children ++ [Mobilizon.PythonWorker]
|
|
|
|
else
|
|
|
|
children
|
|
|
|
end
|
|
|
|
|
2021-06-27 11:15:24 +00:00
|
|
|
ErrorReporting.configure()
|
|
|
|
|
2022-11-07 08:02:09 +00:00
|
|
|
setup_ecto_dev_logger(@env)
|
2022-11-06 15:56:04 +00:00
|
|
|
|
2021-08-03 10:15:54 +00:00
|
|
|
# Only attach the telemetry logger when we aren't in an IEx shell
|
|
|
|
unless Code.ensure_loaded?(IEx) && IEx.started?() do
|
|
|
|
Oban.Telemetry.attach_default_logger(:info)
|
|
|
|
ErrorReporting.attach()
|
|
|
|
end
|
2021-04-20 13:27:33 +00:00
|
|
|
|
2024-02-09 11:13:22 +00:00
|
|
|
with :ok <- load_certificates() do
|
|
|
|
Supervisor.start_link(children, strategy: :one_for_one, name: Mobilizon.Supervisor)
|
|
|
|
end
|
2019-09-15 23:13:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec config_change(keyword, keyword, [atom]) :: :ok
|
|
|
|
def config_change(changed, _new, removed) do
|
2020-01-28 18:18:33 +00:00
|
|
|
Web.Endpoint.config_change(changed, removed)
|
2019-09-15 23:13:20 +00:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2021-01-22 18:11:13 +00:00
|
|
|
# sobelow_skip ["DOS.StringToAtom"]
|
2019-09-15 23:13:20 +00:00
|
|
|
@spec cachex_spec(atom, integer, integer, integer, function | nil) :: Supervisor.child_spec()
|
|
|
|
defp cachex_spec(name, limit, default, interval, fallback \\ nil) do
|
|
|
|
%{
|
2021-01-22 18:11:13 +00:00
|
|
|
id: String.to_atom("cache_#{to_string(name)}"),
|
2019-09-15 23:13:20 +00:00
|
|
|
start:
|
|
|
|
{Cachex, :start_link,
|
|
|
|
[
|
|
|
|
name,
|
2019-09-16 00:07:44 +00:00
|
|
|
Keyword.merge(
|
|
|
|
cachex_options(limit, default, interval),
|
|
|
|
fallback_options(fallback)
|
|
|
|
)
|
2019-09-15 23:13:20 +00:00
|
|
|
]}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec cachex_options(integer, integer, integer) :: keyword
|
|
|
|
defp cachex_options(limit, default, interval) do
|
|
|
|
[
|
|
|
|
limit: limit,
|
|
|
|
expiration:
|
|
|
|
expiration(
|
|
|
|
default: :timer.minutes(default),
|
|
|
|
interval: :timer.seconds(interval)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec fallback_options(function | nil) :: keyword
|
|
|
|
defp fallback_options(nil), do: []
|
|
|
|
defp fallback_options(fallback), do: [fallback: fallback(default: fallback)]
|
2019-12-03 10:29:51 +00:00
|
|
|
|
2019-12-20 12:04:34 +00:00
|
|
|
defp task_children(:test), do: []
|
2020-10-09 10:17:33 +00:00
|
|
|
defp task_children(_), do: [relay_actor(), anonymous_actor(), render_error_page()]
|
2019-12-20 12:04:34 +00:00
|
|
|
|
|
|
|
defp relay_actor do
|
2019-12-03 10:29:51 +00:00
|
|
|
%{
|
2019-12-20 12:04:34 +00:00
|
|
|
id: :relay_actor_init,
|
2020-01-22 01:14:42 +00:00
|
|
|
start: {Task, :start_link, [&ActivityPub.Relay.init/0]},
|
2019-12-03 10:29:51 +00:00
|
|
|
restart: :temporary
|
|
|
|
}
|
|
|
|
end
|
2019-12-20 12:04:34 +00:00
|
|
|
|
|
|
|
defp anonymous_actor do
|
|
|
|
%{
|
|
|
|
id: :anonymous_actor_init,
|
|
|
|
start: {Task, :start_link, [&Mobilizon.Config.anonymous_actor_id/0]},
|
|
|
|
restart: :temporary
|
|
|
|
}
|
|
|
|
end
|
2020-10-09 10:17:33 +00:00
|
|
|
|
|
|
|
defp render_error_page do
|
|
|
|
%{
|
|
|
|
id: :render_error_page_init,
|
|
|
|
start: {Task, :start_link, [&ErrorPage.init/0]},
|
|
|
|
restart: :temporary
|
|
|
|
}
|
|
|
|
end
|
2022-11-07 08:02:09 +00:00
|
|
|
|
|
|
|
defp setup_ecto_dev_logger(:dev) do
|
|
|
|
Ecto.DevLogger.install(Storage.Repo)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp setup_ecto_dev_logger(_), do: nil
|
2024-02-09 11:13:22 +00:00
|
|
|
|
|
|
|
defp load_certificates do
|
|
|
|
custom_cert_path = System.get_env("MOBILIZON_CA_CERT_PATH")
|
|
|
|
|
|
|
|
if is_binary(custom_cert_path) do
|
|
|
|
with :ok <- :tls_certificate_check.override_trusted_authorities({:file, custom_cert_path}) do
|
|
|
|
:public_key.cacerts_load(custom_cert_path)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|