1 # Pleroma: A lightweight social networking server
3 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
4 # SPDX-License-Identifier: AGPL-3.0-only
6 defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
8 alias Pleroma.Web.MediaProxy
9 alias Pleroma.Web.Metadata
10 alias Pleroma.Web.Metadata.Providers.Provider
11 alias Pleroma.Web.Metadata.Utils
14 @media_types ["image", "audio", "video"]
17 def build_tags(%{activity_id: id, object: object, user: user}) do
18 attachments = build_attachments(id, object)
19 scrubbed_content = Utils.scrub_html_and_truncate(object)
23 {:meta, [property: "twitter:description", content: scrubbed_content], []}
25 if attachments == [] or Metadata.activity_nsfw?(object) do
28 {:meta, [property: "twitter:card", content: "summary"], []}
36 def build_tags(%{user: user}) do
37 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
40 {:meta, [property: "twitter:description", content: truncated_bio], []},
42 {:meta, [property: "twitter:card", content: "summary"], []}
47 defp title_tag(user) do
48 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
51 def image_tag(user) do
52 {:meta, [property: "twitter:image", content: MediaProxy.preview_url(User.avatar_url(user))],
56 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
57 Enum.reduce(attachments, [], fn attachment, acc ->
59 Enum.reduce(attachment["url"], [], fn url, acc ->
60 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
63 {:meta, [property: "twitter:card", content: "player"], []},
64 {:meta, [property: "twitter:player:width", content: "480"], []},
65 {:meta, [property: "twitter:player:height", content: "80"], []},
66 {:meta, [property: "twitter:player", content: player_url(id)], []}
70 # Not using preview_url for this. It saves bandwidth, but the image dimensions will
71 # be wrong. We generate it on the fly and have no way to capture or analyze the
72 # image to get the dimensions. This can be an issue for apps/FEs rendering images
73 # in timelines too, but you can get clever with the aspect ratio metadata as a
77 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
80 property: "twitter:player",
81 content: MediaProxy.url(url["href"])
85 |> maybe_add_dimensions(url)
88 # fallback to old placeholder values
89 height = url["height"] || 480
90 width = url["width"] || 480
93 {:meta, [property: "twitter:card", content: "player"], []},
94 {:meta, [property: "twitter:player", content: player_url(id)], []},
95 {:meta, [property: "twitter:player:width", content: "#{width}"], []},
96 {:meta, [property: "twitter:player:height", content: "#{height}"], []},
97 {:meta, [property: "twitter:player:stream", content: MediaProxy.url(url["href"])],
100 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
113 defp build_attachments(_id, _object), do: []
115 defp player_url(id) do
116 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
119 # Videos have problems without dimensions, but we used to not provide WxH for images.
120 # A default (read: incorrect) fallback for images is likely to cause rendering bugs.
121 defp maybe_add_dimensions(metadata, url) do
123 !is_nil(url["height"]) && !is_nil(url["width"]) ->
126 {:meta, [property: "twitter:player:width", content: "#{url["width"]}"], []},
127 {:meta, [property: "twitter:player:height", content: "#{url["height"]}"], []}