+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
defmodule Pleroma.Web.Metadata do
alias Phoenix.HTML
- alias Pleroma.{Formatter, User}
- alias Pleroma.Web.MediaProxy
def build_tags(params) do
- if(meta_enabled?(:opengraph), do: opengraph_tags(params), else: [])
- |> Enum.map(&to_tag/1)
- |> Enum.map(&HTML.safe_to_string/1)
- |> Enum.join("\n")
- end
+ providers = [
+ Pleroma.Web.Metadata.Providers.RelMe,
+ Pleroma.Web.Metadata.Providers.RestrictIndexing
+ | activated_providers()
+ ]
- def meta_enabled?(type) do
- Pleroma.Config.get([:metadata, type], false)
- end
+ Enum.reduce(providers, "", fn parser, acc ->
+ rendered_html =
+ params
+ |> parser.build_tags()
+ |> Enum.map(&to_tag/1)
+ |> Enum.map(&HTML.safe_to_string/1)
+ |> Enum.join()
- # opengraph for single status
- defp opengraph_tags(%{activity: activity, user: user}) do
- with truncated_content = scrub_html_and_truncate(activity.data["object"]["content"]) do
- [
- {:meta,
- [
- property: "og:title",
- content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) post ##{activity.id}"
- ], []},
- {:meta, [property: "og:url", content: activity.data["id"]], []},
- {:meta, [property: "og:description", content: truncated_content], []},
- {: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
- with truncated_bio = scrub_html_and_truncate(user.bio) do
- [
- {:meta,
- [
- property: "og:title",
- content: "#{user.name} (@#{user.nickname}@#{pleroma_domain()}) profile"
- ], []},
- {: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)], []},
- {:meta, [property: "og:image:width", content: 120], []},
- {:meta, [property: "og:image:height", content: 120], []},
- {:meta, [property: "twitter:card", content: "summary"], []}
- ]
- end
+ acc <> rendered_html
+ end)
end
def to_tag(data) do
end
end
- 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()
+ def activity_nsfw?(%{data: %{"sensitive" => sensitive}}) do
+ Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false and sensitive
end
- defp user_avatar_url(user) do
- User.avatar_url(user) |> MediaProxy.url()
+ def activity_nsfw?(_) do
+ false
end
- def pleroma_domain do
- Pleroma.Web.Endpoint.host()
+ defp activated_providers do
+ unless Pleroma.Config.restrict_unauthenticated_access?(:activities, :local) do
+ [Pleroma.Web.Metadata.Providers.Feed | Pleroma.Config.get([__MODULE__, :providers], [])]
+ else
+ []
+ end
end
end