1 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
2 alias Pleroma.Web.Metadata.Providers.Provider
3 alias Pleroma.{HTML, Formatter, User}
4 alias Pleroma.Web.MediaProxy
9 def build_tags(%{activity: activity, user: user}) do
10 with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
11 attachments = build_attachments(activity)
17 content: user_name_string(user)
19 {:meta, [property: "og:url", content: activity.data["id"]], []},
20 {:meta, [property: "og:description", content: truncated_content], []}
22 if attachments == [] or Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do
24 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
25 {:meta, [property: "og:image:width", content: 120], []},
26 {:meta, [property: "og:image:height", content: 120], []}
35 def build_tags(%{user: user}) do
36 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
41 content: user_name_string(user)
43 {:meta, [property: "og:url", content: User.profile_url(user)], []},
44 {:meta, [property: "og:description", content: truncated_bio], []},
45 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
46 {:meta, [property: "og:image:width", content: 120], []},
47 {:meta, [property: "og:image:height", content: 120], []}
52 defp build_attachments(activity) do
53 Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
55 Enum.map(attachment["url"], fn url ->
57 Enum.find(["image", "audio", "video"], fn media_type ->
58 String.starts_with?(url["mediaType"], media_type)
62 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
68 Enum.reject(rendered_tags, &is_nil/1)
73 defp scrub_html_and_truncate(content) do
75 # html content comes from DB already encoded, decode first and scrub after
76 |> HtmlEntities.decode()
77 |> String.replace(~r/<br\s?\/?>/, " ")
79 |> Formatter.truncate()
82 defp attachment_url(url) do
86 defp user_name_string(user) do
89 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"