Merge remote-tracking branch 'upstream/develop' into registration-workflow
[akkoma] / lib / pleroma / instances.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.Instances do
6 @moduledoc "Instances context."
7
8 @adapter Pleroma.Instances.Instance
9
10 defdelegate filter_reachable(urls_or_hosts), to: @adapter
11 defdelegate reachable?(url_or_host), to: @adapter
12 defdelegate set_reachable(url_or_host), to: @adapter
13 defdelegate set_unreachable(url_or_host, unreachable_since \\ nil), to: @adapter
14 defdelegate get_consistently_unreachable(), to: @adapter
15
16 def set_consistently_unreachable(url_or_host),
17 do: set_unreachable(url_or_host, reachability_datetime_threshold())
18
19 def reachability_datetime_threshold do
20 federation_reachability_timeout_days =
21 Pleroma.Config.get([:instance, :federation_reachability_timeout_days], 0)
22
23 if federation_reachability_timeout_days > 0 do
24 NaiveDateTime.add(
25 NaiveDateTime.utc_now(),
26 -federation_reachability_timeout_days * 24 * 3600,
27 :second
28 )
29 else
30 ~N[0000-01-01 00:00:00]
31 end
32 end
33
34 def host(url_or_host) when is_binary(url_or_host) do
35 if url_or_host =~ ~r/^http/i do
36 URI.parse(url_or_host).host
37 else
38 url_or_host
39 end
40 end
41 end