aboutsummaryrefslogtreecommitdiff
path: root/lib/pleroma/web/metadata
diff options
context:
space:
mode:
authorAlex Gleason <alex@alexgleason.me>2022-01-03 13:40:19 -0600
committerAlex Gleason <alex@alexgleason.me>2022-01-03 13:40:19 -0600
commit4081be0001332bac402faec7565807df088b0117 (patch)
treea5305404e9bb31b3613dbc9631d36f8827be81c2 /lib/pleroma/web/metadata
parentd00f74e036735c1c238f661076f2925b39daa6ac (diff)
parenta3094b64df344622f1bcb03091ef2ff4dce6da82 (diff)
downloadpleroma-matrix.tar.gz
Merge remote-tracking branch 'origin/develop' into matrixmatrix
Diffstat (limited to 'lib/pleroma/web/metadata')
-rw-r--r--lib/pleroma/web/metadata/opengraph.ex119
-rw-r--r--lib/pleroma/web/metadata/player_view.ex2
-rw-r--r--lib/pleroma/web/metadata/providers/feed.ex (renamed from lib/pleroma/web/metadata/feed.ex)2
-rw-r--r--lib/pleroma/web/metadata/providers/open_graph.ex148
-rw-r--r--lib/pleroma/web/metadata/providers/provider.ex (renamed from lib/pleroma/web/metadata/provider.ex)2
-rw-r--r--lib/pleroma/web/metadata/providers/rel_me.ex (renamed from lib/pleroma/web/metadata/rel_me.ex)2
-rw-r--r--lib/pleroma/web/metadata/providers/restrict_indexing.ex (renamed from lib/pleroma/web/metadata/restrict_indexing.ex)11
-rw-r--r--lib/pleroma/web/metadata/providers/twitter_card.ex (renamed from lib/pleroma/web/metadata/twitter_card.ex)54
-rw-r--r--lib/pleroma/web/metadata/utils.ex10
9 files changed, 198 insertions, 152 deletions
diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex
deleted file mode 100644
index bb1b23208..000000000
--- a/lib/pleroma/web/metadata/opengraph.ex
+++ /dev/null
@@ -1,119 +0,0 @@
-# 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.Providers.OpenGraph do
- alias Pleroma.User
- alias Pleroma.Web.Metadata
- alias Pleroma.Web.Metadata.Providers.Provider
- alias Pleroma.Web.Metadata.Utils
-
- @behaviour Provider
- @media_types ["image", "audio", "video"]
-
- @impl Provider
- def build_tags(%{
- object: object,
- url: url,
- user: user
- }) do
- attachments = build_attachments(object)
- scrubbed_content = Utils.scrub_html_and_truncate(object)
- # Zero width space
- content =
- if scrubbed_content != "" and scrubbed_content != "\u200B" do
- ": “" <> scrubbed_content <> "”"
- else
- ""
- end
-
- # Most previews only show og:title which is inconvenient. Instagram
- # hacks this by putting the description in the title and making the
- # description longer prefixed by how many likes and shares the post
- # has. Here we use the descriptive nickname in the title, and expand
- # the full account & nickname in the description. We also use the cute^Wevil
- # smart quotes around the status text like Instagram, too.
- [
- {:meta,
- [
- property: "og:title",
- content: "#{user.name}" <> content
- ], []},
- {:meta, [property: "og:url", content: url], []},
- {:meta,
- [
- property: "og:description",
- content: "#{Utils.user_name_string(user)}" <> content
- ], []},
- {:meta, [property: "og:type", content: "website"], []}
- ] ++
- if attachments == [] or Metadata.activity_nsfw?(object) do
- [
- {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
- []},
- {:meta, [property: "og:image:width", content: 150], []},
- {:meta, [property: "og:image:height", content: 150], []}
- ]
- else
- attachments
- end
- end
-
- @impl Provider
- def build_tags(%{user: user}) do
- with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
- [
- {:meta,
- [
- property: "og:title",
- content: Utils.user_name_string(user)
- ], []},
- {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
- {:meta, [property: "og:description", content: truncated_bio], []},
- {:meta, [property: "og:type", content: "website"], []},
- {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))], []},
- {:meta, [property: "og:image:width", content: 150], []},
- {:meta, [property: "og:image:height", content: 150], []}
- ]
- end
- end
-
- defp build_attachments(%{data: %{"attachment" => attachments}}) do
- Enum.reduce(attachments, [], fn attachment, acc ->
- rendered_tags =
- Enum.reduce(attachment["url"], [], fn url, acc ->
- # TODO: Add additional properties to objects when we have the data available.
- # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
- # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
- case Utils.fetch_media_type(@media_types, url["mediaType"]) do
- "audio" ->
- [
- {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
- | acc
- ]
-
- "image" ->
- [
- {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
- {:meta, [property: "og:image:width", content: 150], []},
- {:meta, [property: "og:image:height", content: 150], []}
- | acc
- ]
-
- "video" ->
- [
- {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
- | acc
- ]
-
- _ ->
- acc
- end
- end)
-
- acc ++ rendered_tags
- end)
- end
-
- defp build_attachments(_), do: []
-end
diff --git a/lib/pleroma/web/metadata/player_view.ex b/lib/pleroma/web/metadata/player_view.ex
index 5a918532a..9be5e433d 100644
--- a/lib/pleroma/web/metadata/player_view.ex
+++ b/lib/pleroma/web/metadata/player_view.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.PlayerView do
diff --git a/lib/pleroma/web/metadata/feed.ex b/lib/pleroma/web/metadata/providers/feed.ex
index bd1459a17..d0ab5c19e 100644
--- a/lib/pleroma/web/metadata/feed.ex
+++ b/lib/pleroma/web/metadata/providers/feed.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.Feed do
diff --git a/lib/pleroma/web/metadata/providers/open_graph.ex b/lib/pleroma/web/metadata/providers/open_graph.ex
new file mode 100644
index 000000000..df0cca74a
--- /dev/null
+++ b/lib/pleroma/web/metadata/providers/open_graph.ex
@@ -0,0 +1,148 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
+ alias Pleroma.User
+ alias Pleroma.Web.MediaProxy
+ alias Pleroma.Web.Metadata
+ alias Pleroma.Web.Metadata.Providers.Provider
+ alias Pleroma.Web.Metadata.Utils
+
+ @behaviour Provider
+ @media_types ["image", "audio", "video"]
+
+ @impl Provider
+ def build_tags(%{
+ object: object,
+ url: url,
+ user: user
+ }) do
+ attachments = build_attachments(object)
+ scrubbed_content = Utils.scrub_html_and_truncate(object)
+
+ [
+ {:meta,
+ [
+ property: "og:title",
+ content: Utils.user_name_string(user)
+ ], []},
+ {:meta, [property: "og:url", content: url], []},
+ {:meta,
+ [
+ property: "og:description",
+ content: scrubbed_content
+ ], []},
+ {:meta, [property: "og:type", content: "article"], []}
+ ] ++
+ if attachments == [] or Metadata.activity_nsfw?(object) do
+ [
+ {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
+ []},
+ {:meta, [property: "og:image:width", content: 150], []},
+ {:meta, [property: "og:image:height", content: 150], []}
+ ]
+ else
+ attachments
+ end
+ end
+
+ @impl Provider
+ def build_tags(%{user: user}) do
+ with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
+ [
+ {:meta,
+ [
+ property: "og:title",
+ content: Utils.user_name_string(user)
+ ], []},
+ {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
+ {:meta, [property: "og:description", content: truncated_bio], []},
+ {:meta, [property: "og:type", content: "article"], []},
+ {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
+ []},
+ {:meta, [property: "og:image:width", content: 150], []},
+ {:meta, [property: "og:image:height", content: 150], []}
+ ]
+ end
+ end
+
+ defp build_attachments(%{data: %{"attachment" => attachments}}) do
+ Enum.reduce(attachments, [], fn attachment, acc ->
+ rendered_tags =
+ Enum.reduce(attachment["url"], [], fn url, acc ->
+ # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
+ # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
+ case Utils.fetch_media_type(@media_types, url["mediaType"]) do
+ "audio" ->
+ [
+ {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
+ | acc
+ ]
+
+ # Not using preview_url for this. It saves bandwidth, but the image dimensions will
+ # be wrong. We generate it on the fly and have no way to capture or analyze the
+ # image to get the dimensions. This can be an issue for apps/FEs rendering images
+ # in timelines too, but you can get clever with the aspect ratio metadata as a
+ # workaround.
+ "image" ->
+ [
+ {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
+ {:meta, [property: "og:image:alt", content: attachment["name"]], []}
+ | acc
+ ]
+ |> maybe_add_dimensions(url)
+
+ "video" ->
+ [
+ {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
+ | acc
+ ]
+ |> maybe_add_dimensions(url)
+ |> maybe_add_video_thumbnail(url)
+
+ _ ->
+ acc
+ end
+ end)
+
+ acc ++ rendered_tags
+ end)
+ end
+
+ defp build_attachments(_), do: []
+
+ # We can use url["mediaType"] to dynamically fill the metadata
+ defp maybe_add_dimensions(metadata, url) do
+ type = url["mediaType"] |> String.split("/") |> List.first()
+
+ cond do
+ !is_nil(url["height"]) && !is_nil(url["width"]) ->
+ metadata ++
+ [
+ {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
+ {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
+ ]
+
+ true ->
+ metadata
+ end
+ end
+
+ # Media Preview Proxy makes thumbnails of videos without resizing, so we can trust the
+ # width and height of the source video.
+ defp maybe_add_video_thumbnail(metadata, url) do
+ cond do
+ Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
+ metadata ++
+ [
+ {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
+ {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
+ {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}
+ ]
+
+ true ->
+ metadata
+ end
+ end
+end
diff --git a/lib/pleroma/web/metadata/provider.ex b/lib/pleroma/web/metadata/providers/provider.ex
index 767288f9c..c91d87c6d 100644
--- a/lib/pleroma/web/metadata/provider.ex
+++ b/lib/pleroma/web/metadata/providers/provider.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.Provider do
diff --git a/lib/pleroma/web/metadata/rel_me.ex b/lib/pleroma/web/metadata/providers/rel_me.ex
index 8905c9c72..f013def51 100644
--- a/lib/pleroma/web/metadata/rel_me.ex
+++ b/lib/pleroma/web/metadata/providers/rel_me.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.RelMe do
diff --git a/lib/pleroma/web/metadata/restrict_indexing.ex b/lib/pleroma/web/metadata/providers/restrict_indexing.ex
index f15607896..aa6511610 100644
--- a/lib/pleroma/web/metadata/restrict_indexing.ex
+++ b/lib/pleroma/web/metadata/providers/restrict_indexing.ex
@@ -1,16 +1,18 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.RestrictIndexing do
@behaviour Pleroma.Web.Metadata.Providers.Provider
@moduledoc """
- Restricts indexing of remote users.
+ Restricts indexing of remote and/or non-discoverable users.
"""
@impl true
- def build_tags(%{user: %{local: false}}) do
+ def build_tags(%{user: %{local: true, is_discoverable: true}}), do: []
+
+ def build_tags(_) do
[
{:meta,
[
@@ -19,7 +21,4 @@ defmodule Pleroma.Web.Metadata.Providers.RestrictIndexing do
], []}
]
end
-
- @impl true
- def build_tags(%{user: %{local: true}}), do: []
end
diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/providers/twitter_card.ex
index df34b033f..79183df86 100644
--- a/lib/pleroma/web/metadata/twitter_card.ex
+++ b/lib/pleroma/web/metadata/providers/twitter_card.ex
@@ -1,10 +1,11 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
alias Pleroma.User
+ alias Pleroma.Web.MediaProxy
alias Pleroma.Web.Metadata
alias Pleroma.Web.Metadata.Providers.Provider
alias Pleroma.Web.Metadata.Utils
@@ -16,17 +17,10 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
def build_tags(%{activity_id: id, object: object, user: user}) do
attachments = build_attachments(id, object)
scrubbed_content = Utils.scrub_html_and_truncate(object)
- # Zero width space
- content =
- if scrubbed_content != "" and scrubbed_content != "\u200B" do
- "“" <> scrubbed_content <> "”"
- else
- ""
- end
[
title_tag(user),
- {:meta, [property: "twitter:description", content: content], []}
+ {:meta, [property: "twitter:description", content: scrubbed_content], []}
] ++
if attachments == [] or Metadata.activity_nsfw?(object) do
[
@@ -55,14 +49,14 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
end
def image_tag(user) do
- {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
+ {:meta, [property: "twitter:image", content: MediaProxy.preview_url(User.avatar_url(user))],
+ []}
end
defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
Enum.reduce(attachments, [], fn attachment, acc ->
rendered_tags =
Enum.reduce(attachment["url"], [], fn url, acc ->
- # TODO: Add additional properties to objects when we have the data available.
case Utils.fetch_media_type(@media_types, url["mediaType"]) do
"audio" ->
[
@@ -73,25 +67,37 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
| acc
]
+ # Not using preview_url for this. It saves bandwidth, but the image dimensions will
+ # be wrong. We generate it on the fly and have no way to capture or analyze the
+ # image to get the dimensions. This can be an issue for apps/FEs rendering images
+ # in timelines too, but you can get clever with the aspect ratio metadata as a
+ # workaround.
"image" ->
[
{:meta, [property: "twitter:card", content: "summary_large_image"], []},
{:meta,
[
property: "twitter:player",
- content: Utils.attachment_url(url["href"])
+ content: MediaProxy.url(url["href"])
], []}
| acc
]
+ |> maybe_add_dimensions(url)
- # TODO: Need the true width and height values here or Twitter renders an iFrame with
- # a bad aspect ratio
"video" ->
+ # fallback to old placeholder values
+ height = url["height"] || 480
+ width = url["width"] || 480
+
[
{:meta, [property: "twitter:card", content: "player"], []},
{:meta, [property: "twitter:player", content: player_url(id)], []},
- {:meta, [property: "twitter:player:width", content: "480"], []},
- {:meta, [property: "twitter:player:height", content: "480"], []}
+ {:meta, [property: "twitter:player:width", content: "#{width}"], []},
+ {:meta, [property: "twitter:player:height", content: "#{height}"], []},
+ {:meta, [property: "twitter:player:stream", content: MediaProxy.url(url["href"])],
+ []},
+ {:meta,
+ [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
| acc
]
@@ -109,4 +115,20 @@ defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
defp player_url(id) do
Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
end
+
+ # Videos have problems without dimensions, but we used to not provide WxH for images.
+ # A default (read: incorrect) fallback for images is likely to cause rendering bugs.
+ defp maybe_add_dimensions(metadata, url) do
+ cond do
+ !is_nil(url["height"]) && !is_nil(url["width"]) ->
+ metadata ++
+ [
+ {:meta, [property: "twitter:player:width", content: "#{url["width"]}"], []},
+ {:meta, [property: "twitter:player:height", content: "#{url["height"]}"], []}
+ ]
+
+ true ->
+ metadata
+ end
+ end
end
diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex
index 2f0dfb474..caca42934 100644
--- a/lib/pleroma/web/metadata/utils.ex
+++ b/lib/pleroma/web/metadata/utils.ex
@@ -1,19 +1,19 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Metadata.Utils do
+ alias Pleroma.Activity
alias Pleroma.Emoji
alias Pleroma.Formatter
alias Pleroma.HTML
- alias Pleroma.Web.MediaProxy
def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
content
# html content comes from DB already encoded, decode first and scrub after
|> HtmlEntities.decode()
|> String.replace(~r/<br\s?\/?>/, " ")
- |> HTML.get_cached_stripped_html_for_activity(object, "metadata")
+ |> Activity.HTML.get_cached_stripped_html_for_activity(object, "metadata")
|> Emoji.Formatter.demojify()
|> HtmlEntities.decode()
|> Formatter.truncate()
@@ -37,10 +37,6 @@ defmodule Pleroma.Web.Metadata.Utils do
def scrub_html(content), do: content
- def attachment_url(url) do
- MediaProxy.url(url)
- end
-
def user_name_string(user) do
"#{user.name} " <>
if user.local do