instance: Handle not getting a favicon

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-09-11 11:22:50 +02:00
parent 6316350918
commit dbc013f24c
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
2 changed files with 50 additions and 33 deletions

View File

@ -159,13 +159,11 @@ defp scrape_favicon(%URI{} = instance_uri) do
Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}],
adapter: [pool: :media] adapter: [pool: :media]
), ),
favicon_rel <- {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
html {:parse,
|> Floki.parse_document!() html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
|> Floki.attribute("link[rel=icon]", "href") {_, favicon} when is_binary(favicon) <-
|> List.first(), {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
favicon <- URI.merge(instance_uri, favicon_rel) |> to_string(),
true <- is_binary(favicon) do
favicon favicon
else else
_ -> nil _ -> nil

View File

@ -99,6 +99,7 @@ test "does NOT modify `unreachable_since` value of existing record in case it's
end end
end end
describe "get_or_update_favicon/1" do
test "Scrapes favicon URLs" do test "Scrapes favicon URLs" do
Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} -> Tesla.Mock.mock(fn %{url: "https://favicon.example.org/"} ->
%Tesla.Env{ %Tesla.Env{
@ -118,7 +119,8 @@ test "Returns nil on too long favicon URLs" do
Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} -> Tesla.Mock.mock(fn %{url: "https://long-favicon.example.org/"} ->
%Tesla.Env{ %Tesla.Env{
status: 200, status: 200,
body: ~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>] body:
~s[<html><head><link rel="icon" href="] <> long_favicon_url <> ~s["></head></html>]
} }
end) end)
@ -130,4 +132,21 @@ test "Returns nil on too long favicon URLs" do
end) =~ end) =~
"Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{" "Instance.get_or_update_favicon(\"long-favicon.example.org\") error: %Postgrex.Error{"
end end
test "Handles not getting a favicon URL properly" do
Tesla.Mock.mock(fn %{url: "https://no-favicon.example.org/"} ->
%Tesla.Env{
status: 200,
body: ~s[<html><head><h1>I wil look down and whisper "GNO.."</h1></head></html>]
}
end)
refute capture_log(fn ->
assert nil ==
Instance.get_or_update_favicon(
URI.parse("https://no-favicon.example.org/")
)
end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: "
end
end
end end