Merge branch 'features/favicon-unreachable-instance' into 'develop'

instance: Do not fetch unreachable instances

Closes #2346

See merge request pleroma/pleroma!3189
This commit is contained in:
feld 2020-12-07 22:49:45 +00:00
commit 9d0839504e
3 changed files with 24 additions and 6 deletions

View File

@ -77,7 +77,7 @@ def reachable?(url_or_host) when is_binary(url_or_host) do
)
end
def reachable?(_), do: true
def reachable?(url_or_host) when is_binary(url_or_host), do: true
def set_reachable(url_or_host) when is_binary(url_or_host) do
with host <- host(url_or_host),
@ -166,7 +166,8 @@ def get_or_update_favicon(%URI{host: host} = instance_uri) do
defp scrape_favicon(%URI{} = instance_uri) 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),
{_, [favicon_rel | _]} when is_binary(favicon_rel) <-
{:parse,
@ -175,7 +176,15 @@ defp scrape_favicon(%URI{} = instance_uri) do
{:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
favicon
else
_ -> nil
{:reachable, false} ->
Logger.debug(
"Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
)
nil
_ ->
nil
end
rescue
e ->

View File

@ -3,6 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Instances.InstanceTest do
alias Pleroma.Instances
alias Pleroma.Instances.Instance
alias Pleroma.Repo
@ -148,5 +149,13 @@ test "Handles not getting a favicon URL properly" do
)
end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
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

View File

@ -32,9 +32,9 @@ test "returns `true` for host / url marked unreachable for less than `reachabili
assert Instances.reachable?(URI.parse(url).host)
end
test "returns true on non-binary input" do
assert Instances.reachable?(nil)
assert Instances.reachable?(1)
test "raises FunctionClauseError exception on non-binary input" do
assert_raise FunctionClauseError, fn -> Instances.reachable?(nil) end
assert_raise FunctionClauseError, fn -> Instances.reachable?(1) end
end
end