2018-12-12 19:55:01 +00:00
|
|
|
defmodule Pleroma.Web.Metadata do
|
|
|
|
alias Phoenix.HTML
|
2018-12-13 21:16:54 +00:00
|
|
|
alias Pleroma.{Formatter, User}
|
2018-12-12 19:55:01 +00:00
|
|
|
alias Pleroma.Web.MediaProxy
|
|
|
|
|
2018-12-13 21:16:54 +00:00
|
|
|
def build_tags(params) do
|
|
|
|
if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
|
2018-12-12 19:55:01 +00:00
|
|
|
|> Enum.map(&to_tag/1)
|
|
|
|
|> Enum.map(&HTML.safe_to_string/1)
|
|
|
|
|> Enum.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def meta_enabled?(type) do
|
2018-12-14 18:55:40 +00:00
|
|
|
Pleroma.Config.get([:metadata, type], false)
|
2018-12-12 19:55:01 +00:00
|
|
|
end
|
|
|
|
|
2018-12-13 21:13:02 +00:00
|
|
|
# opengraph for single status
|
|
|
|
defp opengraph_tags(%{activity: activity, user: user}) do
|
2018-12-14 20:07:06 +00:00
|
|
|
with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
|
2018-12-12 19:55:01 +00:00
|
|
|
[
|
|
|
|
{:meta,
|
2018-12-13 21:16:54 +00:00
|
|
|
[
|
|
|
|
property: "og:title",
|
|
|
|
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
|
|
|
|
], []},
|
2018-12-12 19:55:01 +00:00
|
|
|
{:meta, [property: "og:url", content: activity.data["id"]], []},
|
2018-12-13 21:16:54 +00:00
|
|
|
{:meta, [property: "og:description", content: truncated_content], []},
|
2018-12-13 21:13:02 +00:00
|
|
|
{: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"], []}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# opengraph for user card
|
|
|
|
defp opengraph_tags(%{user: user}) do
|
2018-12-14 20:07:06 +00:00
|
|
|
with truncated_bio = scrub_html_and_truncate(user.bio) do
|
2018-12-13 21:13:02 +00:00
|
|
|
[
|
|
|
|
{:meta,
|
2018-12-13 21:16:54 +00:00
|
|
|
[
|
|
|
|
property: "og:title",
|
|
|
|
content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
|
|
|
|
], []},
|
2018-12-13 21:13:02 +00:00
|
|
|
{: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)], []},
|
2018-12-12 19:55:01 +00:00
|
|
|
{:meta, [property: "og:image:width", content: 120], []},
|
|
|
|
{:meta, [property: "og:image:height", content: 120], []},
|
|
|
|
{:meta, [property: "twitter:card", content: "summary"], []}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 21:13:02 +00:00
|
|
|
def to_tag(data) do
|
|
|
|
with {name, attrs, _content = []} <- data do
|
|
|
|
HTML.Tag.tag(name, attrs)
|
|
|
|
else
|
|
|
|
{name, attrs, content} ->
|
|
|
|
HTML.Tag.content_tag(name, content, attrs)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
raise ArgumentError, message: "make_tag invalid args"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-14 20:07:06 +00:00
|
|
|
defp scrub_html_and_truncate(content) do
|
|
|
|
content
|
|
|
|
# html content comes from DB already encoded, decode first and scrub after
|
|
|
|
|> HtmlEntities.decode()
|
|
|
|
|> Pleroma.HTML.strip_tags()
|
|
|
|
|> Formatter.truncate()
|
|
|
|
end
|
|
|
|
|
2018-12-13 21:13:02 +00:00
|
|
|
defp user_avatar_url(user) do
|
|
|
|
User.avatar_url(user) |> MediaProxy.url()
|
|
|
|
end
|
|
|
|
|
|
|
|
def pleroma_domain do
|
2018-12-14 18:55:40 +00:00
|
|
|
Pleroma.Web.Endpoint.host()
|
2018-12-13 21:13:02 +00:00
|
|
|
end
|
2018-12-13 21:16:54 +00:00
|
|
|
end
|