1 defmodule Pleroma.Web.Metadata do
3 alias Pleroma.{Formatter, User}
4 alias Pleroma.Web.MediaProxy
6 def build_tags(params) do
7 if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
9 |> Enum.map(&HTML.safe_to_string/1)
13 def meta_enabled?(type) do
14 Pleroma.Config.get([:metadata, type], false)
17 # opengraph for single status
18 defp opengraph_tags(%{activity: activity, user: user}) do
19 with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
27 "(@#{user.nickname}@{pleroma_domain})"
32 {:meta, [property: "og:url", content: activity.data["id"]], []},
33 {:meta, [property: "og:description", content: truncated_content], []},
34 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
35 {:meta, [property: "og:image:width", content: 120], []},
36 {:meta, [property: "og:image:height", content: 120], []},
37 {:meta, [property: "twitter:card", content: "summary"], []}
42 # opengraph for user card
43 defp opengraph_tags(%{user: user}) do
44 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
49 content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
51 {:meta, [property: "og:url", content: User.profile_url(user)], []},
52 {:meta, [property: "og:description", content: truncated_bio], []},
53 {:meta, [property: "og:image", content: user_avatar_url(user)], []},
54 {:meta, [property: "og:image:width", content: 120], []},
55 {:meta, [property: "og:image:height", content: 120], []},
56 {:meta, [property: "twitter:card", content: "summary"], []}
61 defp opengraph_tags(_) do
66 with {name, attrs, _content = []} <- data do
67 HTML.Tag.tag(name, attrs)
69 {name, attrs, content} ->
70 HTML.Tag.content_tag(name, content, attrs)
73 raise ArgumentError, message: "make_tag invalid args"
77 defp scrub_html_and_truncate(content) do
79 # html content comes from DB already encoded, decode first and scrub after
80 |> HtmlEntities.decode()
81 |> Pleroma.HTML.strip_tags()
82 |> Formatter.truncate()
85 defp user_avatar_url(user) do
86 User.avatar_url(user) |> MediaProxy.url()
90 Pleroma.Web.Endpoint.host()