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) 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
23 Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do
25 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
26 {:meta, [property: "og:image:width", content: 120], []},
27 {:meta, [property: "og:image:height", content: 120], []}
36 def build_tags(%{user: user}) do
37 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
42 content: user_name_string(user)
44 {:meta, [property: "og:url", content: User.profile_url(user)], []},
45 {:meta, [property: "og:description", content: truncated_bio], []},
46 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
47 {:meta, [property: "og:image:width", content: 120], []},
48 {:meta, [property: "og:image:height", content: 120], []}
53 defp build_attachments(activity) do
54 Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
56 Enum.map(attachment["url"], fn url ->
58 Enum.find(["image", "audio", "video"], fn media_type ->
59 String.starts_with?(url["mediaType"], media_type)
63 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
69 Enum.reject(rendered_tags, &is_nil/1)
74 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
76 # html content comes from DB already encoded, decode first and scrub after
77 |> HtmlEntities.decode()
78 |> String.replace(~r/<br\s?\/?>/, " ")
79 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
80 |> Formatter.truncate()
83 defp scrub_html_and_truncate(content) when is_binary(content) do
85 # html content comes from DB already encoded, decode first and scrub after
86 |> HtmlEntities.decode()
87 |> String.replace(~r/<br\s?\/?>/, " ")
89 |> Formatter.truncate()
92 defp attachment_url(url) do
96 defp user_name_string(user) do
99 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
101 "(@#{user.nickname})"