diff options
| author | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2020-03-01 09:48:32 +0100 |
|---|---|---|
| committer | Haelwenn (lanodan) Monnier <contact@hacktivis.me> | 2020-07-08 06:28:39 +0200 |
| commit | f6d09fafee83514889bbcf6531e0bc01e33b0b16 (patch) | |
| tree | b5e1379e8c3bcc508a29e66d42cfd5ab04d48d98 /lib | |
| parent | a8447c3803b9d618f8c1d1910698c8cca6e908e4 (diff) | |
| download | pleroma-f6d09fafee83514889bbcf6531e0bc01e33b0b16.tar.gz pleroma-f6d09fafee83514889bbcf6531e0bc01e33b0b16.zip | |
Add support for remote favicons
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/user.ex | 30 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/views/account_view.ex | 3 |
2 files changed, 32 insertions, 1 deletions
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index e98332744..25ea112a2 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2253,4 +2253,34 @@ 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 diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index a6e64b4ab..efe835e3c 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -245,7 +245,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do hide_favorites: user.hide_favorites, relationship: relationship, 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]) |
