X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fmetadata%2Fopengraph.ex;h=b158569748d5559da9833e10201d59728a35f63f;hb=dd1432d6955a72b8483717978d61a505e0608bbc;hp=6d86c0ee6d5720d7b89ada56bf4b4eac9cd4954b;hpb=ff6c9a5c961647b64c3c9fcc05932093595e9588;p=akkoma diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index 6d86c0ee6..b15856974 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -7,7 +7,9 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do @impl Provider def build_tags(%{activity: activity, user: user}) do - with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do + with truncated_content = scrub_html_and_truncate(activity) do + attachments = build_attachments(activity) + [ {:meta, [ @@ -15,12 +17,18 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do content: user_name_string(user) ], []}, {:meta, [property: "og:url", content: activity.data["id"]], []}, - {:meta, [property: "og:description", content: truncated_content], []}, - {:meta, [property: "og:image", content: user_avatar_url(user)], []}, - {:meta, [property: "og:image:width", content: 120], []}, - {:meta, [property: "og:image:height", content: 120], []}, - {:meta, [property: "twitter:card", content: "summary"], []} - ] + {:meta, [property: "og:description", content: truncated_content], []} + ] ++ + if attachments == [] or + Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do + [ + {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, + {:meta, [property: "og:image:width", content: 120], []}, + {:meta, [property: "og:image:height", content: 120], []} + ] + else + attachments + end end end @@ -35,15 +43,44 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do ], []}, {:meta, [property: "og:url", content: User.profile_url(user)], []}, {:meta, [property: "og:description", content: truncated_bio], []}, - {:meta, [property: "og:image", content: user_avatar_url(user)], []}, + {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image:width", content: 120], []}, - {:meta, [property: "og:image:height", content: 120], []}, - {:meta, [property: "twitter:card", content: "summary"], []} + {:meta, [property: "og:image:height", content: 120], []} ] end end - defp scrub_html_and_truncate(content) do + defp build_attachments(activity) do + Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc -> + rendered_tags = + Enum.map(attachment["url"], fn url -> + media_type = + Enum.find(["image", "audio", "video"], fn media_type -> + String.starts_with?(url["mediaType"], media_type) + end) + + if media_type do + {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []} + else + nil + end + end) + + Enum.reject(rendered_tags, &is_nil/1) + acc ++ rendered_tags + end) + end + + defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do + content + # html content comes from DB already encoded, decode first and scrub after + |> HtmlEntities.decode() + |> String.replace(~r//, " ") + |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__) + |> Formatter.truncate() + end + + defp scrub_html_and_truncate(content) when is_binary(content) do content # html content comes from DB already encoded, decode first and scrub after |> HtmlEntities.decode() @@ -52,12 +89,12 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do |> Formatter.truncate() end - defp user_avatar_url(user) do - User.avatar_url(user) |> MediaProxy.url() + defp attachment_url(url) do + MediaProxy.url(url) end defp user_name_string(user) do - "#{user.name}" <> + "#{user.name} " <> if user.local do "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})" else