Add support for remote favicons
This commit is contained in:
parent
a8447c3803
commit
f6d09fafee
|
@ -2253,4 +2253,34 @@ def sanitize_html(%User{} = user, filter) do
|
||||||
|> Map.put(:bio, HTML.filter_tags(user.bio, filter))
|
|> Map.put(:bio, HTML.filter_tags(user.bio, filter))
|
||||||
|> Map.put(:fields, fields)
|
|> Map.put(:fields, fields)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_cached_favicon(%User{} = user) do
|
||||||
|
key = "favicon:#{user.ap_id}"
|
||||||
|
Cachex.fetch!(:user_cache, key, fn _ -> get_favicon(user) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_cached_favicon(_user) do
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_favicon(user) do
|
||||||
|
try do
|
||||||
|
with url <- user.ap_id,
|
||||||
|
true <- is_binary(url),
|
||||||
|
{:ok, %Tesla.Env{body: html}} <- Pleroma.HTTP.get(url),
|
||||||
|
favicon_rel <-
|
||||||
|
html
|
||||||
|
|> Floki.parse_document!()
|
||||||
|
|> Floki.attribute("link[rel=icon]", "href")
|
||||||
|
|> List.first(),
|
||||||
|
favicon_url <- URI.merge(URI.parse(url), favicon_rel) |> to_string(),
|
||||||
|
true <- is_binary(favicon_url) do
|
||||||
|
favicon_url
|
||||||
|
else
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -245,7 +245,8 @@ defp do_render("show.json", %{user: user} = opts) do
|
||||||
hide_favorites: user.hide_favorites,
|
hide_favorites: user.hide_favorites,
|
||||||
relationship: relationship,
|
relationship: relationship,
|
||||||
skip_thread_containment: user.skip_thread_containment,
|
skip_thread_containment: user.skip_thread_containment,
|
||||||
background_image: image_url(user.background) |> MediaProxy.url()
|
background_image: image_url(user.background) |> MediaProxy.url(),
|
||||||
|
favicon: User.get_cached_favicon(user) |> MediaProxy.url()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|> maybe_put_role(user, opts[:for])
|
|> maybe_put_role(user, opts[:for])
|
||||||
|
|
|
@ -1342,6 +1342,10 @@ def get("https://relay.mastodon.host/actor", _, _, _) do
|
||||||
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/relay/relay.json")}}
|
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/relay/relay.json")}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get("http://localhost:4001/users/" <> _, _, _, _) do
|
||||||
|
{:ok, %Tesla.Env{status: 200, body: File.read!("test/fixtures/tesla_mock/7369654.html")}}
|
||||||
|
end
|
||||||
|
|
||||||
def get(url, query, body, headers) do
|
def get(url, query, body, headers) do
|
||||||
{:error,
|
{:error,
|
||||||
"Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{
|
"Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{
|
||||||
|
|
|
@ -75,6 +75,7 @@ test "Represent a user account" do
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
ap_id: user.ap_id,
|
ap_id: user.ap_id,
|
||||||
background_image: "https://example.com/images/asuka_hospital.png",
|
background_image: "https://example.com/images/asuka_hospital.png",
|
||||||
|
favicon: nil,
|
||||||
confirmation_pending: false,
|
confirmation_pending: false,
|
||||||
tags: [],
|
tags: [],
|
||||||
is_admin: false,
|
is_admin: false,
|
||||||
|
@ -152,6 +153,7 @@ test "Represent a Service(bot) account" do
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
ap_id: user.ap_id,
|
ap_id: user.ap_id,
|
||||||
background_image: nil,
|
background_image: nil,
|
||||||
|
favicon: nil,
|
||||||
confirmation_pending: false,
|
confirmation_pending: false,
|
||||||
tags: [],
|
tags: [],
|
||||||
is_admin: false,
|
is_admin: false,
|
||||||
|
|
Loading…
Reference in New Issue