2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon do
|
2017-12-08 09:58:14 +01:00
|
|
|
@moduledoc """
|
2019-09-08 01:49:56 +02:00
|
|
|
Mobilizon is a decentralized and federated Meetup-like using
|
|
|
|
[ActivityPub](http://activitypub.rocks/).
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2019-09-08 01:49:56 +02: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 17:56:50 +01:00
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
Mobilizon relies on `Guardian` for auth and `Geo`/Postgis for geographical
|
|
|
|
information.
|
2017-12-08 09:58:14 +01:00
|
|
|
"""
|
2019-09-16 01:13:20 +02:00
|
|
|
|
|
|
|
use Application
|
|
|
|
|
|
|
|
import Cachex.Spec
|
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.{Config, Storage, Web}
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2019-09-16 01:13:20 +02:00
|
|
|
alias Mobilizon.Service.Export.{Feed, ICalendar}
|
|
|
|
|
|
|
|
@name Mix.Project.config()[:name]
|
|
|
|
@version Mix.Project.config()[:version]
|
2019-12-20 13:04:34 +01:00
|
|
|
@env Mix.env()
|
2019-09-16 01:13:20 +02:00
|
|
|
|
|
|
|
@spec named_version :: String.t()
|
|
|
|
def named_version, do: "#{@name} #{@version}"
|
|
|
|
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec user_agent :: String.t()
|
2019-09-16 01:13:20 +02:00
|
|
|
def user_agent do
|
2020-01-28 19:18:33 +01:00
|
|
|
info = "#{Web.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
|
2019-09-16 01:13:20 +02: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 13:04:34 +01:00
|
|
|
children =
|
|
|
|
[
|
|
|
|
# supervisors
|
|
|
|
Storage.Repo,
|
2020-02-18 08:57:00 +01:00
|
|
|
{Phoenix.PubSub, name: Mobilizon.PubSub},
|
2019-12-20 13:04:34 +01:00
|
|
|
Web.Endpoint,
|
2020-02-18 08:57:00 +01:00
|
|
|
{Absinthe.Subscription, Web.Endpoint},
|
2019-12-20 13:04:34 +01:00
|
|
|
{Oban, Application.get_env(:mobilizon, Oban)},
|
|
|
|
# workers
|
|
|
|
Guardian.DB.Token.SweeperServer,
|
|
|
|
ActivityPub.Federator,
|
|
|
|
cachex_spec(:feed, 2500, 60, 60, &Feed.create_cache/1),
|
|
|
|
cachex_spec(:ics, 2500, 60, 60, &ICalendar.create_cache/1),
|
2020-02-14 17:56:36 +01:00
|
|
|
cachex_spec(
|
|
|
|
:actor_key_rotation,
|
|
|
|
2500,
|
|
|
|
div(Application.get_env(:mobilizon, :activitypub)[:actor_key_rotation_delay], 60),
|
|
|
|
60 * 30
|
|
|
|
),
|
2019-12-20 13:04:34 +01:00
|
|
|
cachex_spec(:statistics, 10, 60, 60),
|
|
|
|
cachex_spec(:config, 10, 60, 60),
|
2020-02-18 08:57:00 +01:00
|
|
|
cachex_spec(:rich_media_cache, 10, 60, 60),
|
2019-12-20 13:04:34 +01:00
|
|
|
cachex_spec(:activity_pub, 2500, 3, 15)
|
|
|
|
] ++
|
|
|
|
task_children(@env)
|
2019-09-16 01:13:20 +02:00
|
|
|
|
2019-11-04 15:10:58 +01:00
|
|
|
Supervisor.start_link(children, strategy: :one_for_one, name: Mobilizon.Supervisor)
|
2019-09-16 01:13:20 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec config_change(keyword, keyword, [atom]) :: :ok
|
|
|
|
def config_change(changed, _new, removed) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Web.Endpoint.config_change(changed, removed)
|
2019-09-16 01:13:20 +02:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec cachex_spec(atom, integer, integer, integer, function | nil) :: Supervisor.child_spec()
|
|
|
|
defp cachex_spec(name, limit, default, interval, fallback \\ nil) do
|
|
|
|
%{
|
|
|
|
id: :"cache_#{name}",
|
|
|
|
start:
|
|
|
|
{Cachex, :start_link,
|
|
|
|
[
|
|
|
|
name,
|
2019-09-16 02:07:44 +02:00
|
|
|
Keyword.merge(
|
|
|
|
cachex_options(limit, default, interval),
|
|
|
|
fallback_options(fallback)
|
|
|
|
)
|
2019-09-16 01:13:20 +02: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 11:29:51 +01:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
defp task_children(:test), do: []
|
|
|
|
defp task_children(_), do: [relay_actor(), anonymous_actor()]
|
|
|
|
|
|
|
|
defp relay_actor do
|
2019-12-03 11:29:51 +01:00
|
|
|
%{
|
2019-12-20 13:04:34 +01:00
|
|
|
id: :relay_actor_init,
|
2020-01-22 02:14:42 +01:00
|
|
|
start: {Task, :start_link, [&ActivityPub.Relay.init/0]},
|
2019-12-03 11:29:51 +01:00
|
|
|
restart: :temporary
|
|
|
|
}
|
|
|
|
end
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
defp anonymous_actor do
|
|
|
|
%{
|
|
|
|
id: :anonymous_actor_init,
|
|
|
|
start: {Task, :start_link, [&Mobilizon.Config.anonymous_actor_id/0]},
|
|
|
|
restart: :temporary
|
|
|
|
}
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|