Move get_favicon to Pleroma.Instances, use /
This commit is contained in:
parent
f6d09fafee
commit
6a679d80c9
7 changed files with 353 additions and 34 deletions
|
|
@ -150,6 +150,7 @@ defmodule Pleroma.Application do
|
|||
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
|
||||
build_cachex("failed_proxy_url", limit: 2500),
|
||||
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000)
|
||||
build_cachex("instances", default_ttl: 25_000, ttl_interval: 1000, limit: 2500)
|
||||
]
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -37,4 +37,32 @@ defmodule Pleroma.Instances do
|
|||
url_or_host
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2253,34 +2253,4 @@ defmodule Pleroma.User do
|
|||
|> Map.put(:bio, HTML.filter_tags(user.bio, filter))
|
||||
|> Map.put(:fields, fields)
|
||||
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
|
||||
|
|
|
|||
|
|
@ -204,6 +204,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
|||
%{}
|
||||
end
|
||||
|
||||
favicon =
|
||||
user
|
||||
|> Map.get(:ap_id, "")
|
||||
|> URI.parse()
|
||||
|> URI.merge("/")
|
||||
|> to_string()
|
||||
|> Pleroma.Instances.get_cached_favicon()
|
||||
|> MediaProxy.url()
|
||||
|
||||
%{
|
||||
id: to_string(user.id),
|
||||
username: username_from_nickname(user.nickname),
|
||||
|
|
@ -246,7 +255,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
|||
relationship: relationship,
|
||||
skip_thread_containment: user.skip_thread_containment,
|
||||
background_image: image_url(user.background) |> MediaProxy.url(),
|
||||
favicon: User.get_cached_favicon(user) |> MediaProxy.url()
|
||||
favicon: favicon
|
||||
}
|
||||
}
|
||||
|> maybe_put_role(user, opts[:for])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue