instance: Do not fetch unreachable instances

Closes: https://git.pleroma.social/pleroma/pleroma/-/issues/2346
This commit is contained in:
Haelwenn (lanodan) Monnier 2020-12-07 20:09:34 +01:00
parent ed76323776
commit e1a2e8b17c
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
2 changed files with 20 additions and 2 deletions

View file

@ -166,7 +166,8 @@ defmodule Pleroma.Instances.Instance do
defp scrape_favicon(%URI{} = instance_uri) do defp scrape_favicon(%URI{} = instance_uri) do
try do try do
with {:ok, %Tesla.Env{body: html}} <- with {_, true} <- {:reachable, reachable?(instance_uri.host)},
{:ok, %Tesla.Env{body: html}} <-
Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media), Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media),
{_, [favicon_rel | _]} when is_binary(favicon_rel) <- {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
{:parse, {:parse,
@ -175,7 +176,15 @@ defmodule Pleroma.Instances.Instance do
{:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
favicon favicon
else else
_ -> nil {:reachable, false} ->
Logger.debug(
"Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
)
nil
_ ->
nil
end end
rescue rescue
e -> e ->

View file

@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Instances.InstanceTest do defmodule Pleroma.Instances.InstanceTest do
alias Pleroma.Instances
alias Pleroma.Instances.Instance alias Pleroma.Instances.Instance
alias Pleroma.Repo alias Pleroma.Repo
@ -148,5 +149,13 @@ defmodule Pleroma.Instances.InstanceTest do
) )
end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: " end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
end end
test "Doesn't scrapes unreachable instances" do
instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold())
url = "https://" <> instance.host
assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~
"Instance.scrape_favicon(\"#{url}\") ignored unreachable host"
end
end end
end end