Merge branch 'feature/905-dynamic-configuration-in-sql' into 'develop'
[akkoma] / lib / pleroma / instances.ex
1 defmodule Pleroma.Instances do
2 @moduledoc "Instances context."
3
4 @adapter Pleroma.Instances.Instance
5
6 defdelegate filter_reachable(urls_or_hosts), to: @adapter
7 defdelegate reachable?(url_or_host), to: @adapter
8 defdelegate set_reachable(url_or_host), to: @adapter
9 defdelegate set_unreachable(url_or_host, unreachable_since \\ nil), to: @adapter
10
11 def set_consistently_unreachable(url_or_host),
12 do: set_unreachable(url_or_host, reachability_datetime_threshold())
13
14 def reachability_datetime_threshold do
15 federation_reachability_timeout_days =
16 Pleroma.Config.get([:instance, :federation_reachability_timeout_days], 0)
17
18 if federation_reachability_timeout_days > 0 do
19 NaiveDateTime.add(
20 NaiveDateTime.utc_now(),
21 -federation_reachability_timeout_days * 24 * 3600,
22 :second
23 )
24 else
25 ~N[0000-01-01 00:00:00]
26 end
27 end
28
29 def host(url_or_host) when is_binary(url_or_host) do
30 if url_or_host =~ ~r/^http/i do
31 URI.parse(url_or_host).host
32 else
33 url_or_host
34 end
35 end
36 end