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)
16 content: user_name_string(user)
18 {:meta, [property: "og:url", content: activity.data["id"]], []},
19 {:meta, [property: "og:description", content: truncated_content], []},
20 {:meta, [property: "twitter:card", content: "summary"], []}
21 ] ++ if attachments == [] do [
22 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
23 {:meta, [property: "og:image:width", content: 120], []},
24 {:meta, [property: "og:image:height", content: 120], []} ] else attachments end
29 def build_tags(%{user: user}) do
30 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
35 content: user_name_string(user)
37 {:meta, [property: "og:url", content: User.profile_url(user)], []},
38 {:meta, [property: "og:description", content: truncated_bio], []},
39 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
40 {:meta, [property: "og:image:width", content: 120], []},
41 {:meta, [property: "og:image:height", content: 120], []},
42 {:meta, [property: "twitter:card", content: "summary"], []}
47 defp build_attachments(activity) do
48 Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
50 Enum.map(attachment["url"], fn url ->
52 Enum.find(["image", "audio", "video"], fn media_type ->
53 String.starts_with?(url["mediaType"], media_type)
57 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
63 Enum.reject(rendered_tags, &is_nil/1)
68 defp scrub_html_and_truncate(content) do
70 # html content comes from DB already encoded, decode first and scrub after
71 |> HtmlEntities.decode()
72 |> String.replace(~r/<br\s?\/?>/, " ")
74 |> Formatter.truncate()
77 defp attachment_url(url) do
81 defp user_name_string(user) do
84 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"