2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-01-23 15:37:25 +00:00
|
|
|
defmodule Pleroma.Instances do
|
|
|
|
@moduledoc "Instances context."
|
|
|
|
|
|
|
|
@adapter Pleroma.Instances.Instance
|
|
|
|
|
2019-01-28 12:25:06 +00:00
|
|
|
defdelegate filter_reachable(urls_or_hosts), to: @adapter
|
|
|
|
defdelegate reachable?(url_or_host), to: @adapter
|
|
|
|
defdelegate set_reachable(url_or_host), to: @adapter
|
|
|
|
defdelegate set_unreachable(url_or_host, unreachable_since \\ nil), to: @adapter
|
|
|
|
|
|
|
|
def set_consistently_unreachable(url_or_host),
|
|
|
|
do: set_unreachable(url_or_host, reachability_datetime_threshold())
|
2019-01-23 15:37:25 +00:00
|
|
|
|
2019-01-25 12:10:21 +00:00
|
|
|
def reachability_datetime_threshold do
|
|
|
|
federation_reachability_timeout_days =
|
2019-06-14 15:45:05 +00:00
|
|
|
Pleroma.Config.get([:instance, :federation_reachability_timeout_days], 0)
|
2019-01-25 12:10:21 +00:00
|
|
|
|
|
|
|
if federation_reachability_timeout_days > 0 do
|
|
|
|
NaiveDateTime.add(
|
|
|
|
NaiveDateTime.utc_now(),
|
|
|
|
-federation_reachability_timeout_days * 24 * 3600,
|
|
|
|
:second
|
|
|
|
)
|
|
|
|
else
|
|
|
|
~N[0000-01-01 00:00:00]
|
|
|
|
end
|
|
|
|
end
|
2019-01-24 16:15:23 +00:00
|
|
|
|
|
|
|
def host(url_or_host) when is_binary(url_or_host) do
|
|
|
|
if url_or_host =~ ~r/^http/i do
|
|
|
|
URI.parse(url_or_host).host
|
|
|
|
else
|
|
|
|
url_or_host
|
|
|
|
end
|
|
|
|
end
|
2020-03-02 04:38:25 +00:00
|
|
|
|
|
|
|
def get_cached_favicon(instance_url) when is_binary(instance_url) do
|
|
|
|
Cachex.fetch!(:instances_cache, instance_url, fn _ -> get_favicon(instance_url) end)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_cached_favicon(_instance_url) do
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_favicon(instance_url) when is_binary(instance_url) do
|
|
|
|
try do
|
|
|
|
with {:ok, %Tesla.Env{body: html}} <-
|
|
|
|
Pleroma.HTTP.get(instance_url, [{:Accept, "text/html"}]),
|
|
|
|
favicon_rel <-
|
|
|
|
html
|
|
|
|
|> Floki.parse_document!()
|
|
|
|
|> Floki.attribute("link[rel=icon]", "href")
|
|
|
|
|> List.first(),
|
|
|
|
favicon_url <- URI.merge(URI.parse(instance_url), favicon_rel) |> to_string(),
|
|
|
|
true <- is_binary(favicon_url) do
|
|
|
|
favicon_url
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
2019-01-23 15:37:25 +00:00
|
|
|
end
|