1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
7 alias Pleroma.Web.Metadata
8 alias Pleroma.Web.Metadata.Providers.Provider
9 alias Pleroma.Web.Metadata.Utils
19 attachments = build_attachments(id, object)
20 scrubbed_content = Utils.scrub_html_and_truncate(object)
23 if scrubbed_content != "" and scrubbed_content != "\u200B" do
24 "“" <> scrubbed_content <> "”"
32 property: "twitter:title",
33 content: Utils.user_name_string(user)
37 property: "twitter:description",
41 if attachments == [] or Metadata.activity_nsfw?(object) do
44 [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []},
45 {:meta, [property: "twitter:card", content: "summary_large_image"], []}
53 def build_tags(%{user: user}) do
54 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
58 property: "twitter:title",
59 content: Utils.user_name_string(user)
61 {:meta, [property: "twitter:description", content: truncated_bio], []},
62 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))],
64 {:meta, [property: "twitter:card", content: "summary"], []}
69 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
70 Enum.reduce(attachments, [], fn attachment, acc ->
72 Enum.reduce(attachment["url"], [], fn url, acc ->
74 Enum.find(["image", "audio", "video"], fn media_type ->
75 String.starts_with?(url["mediaType"], media_type)
78 # TODO: Add additional properties to objects when we have the data available.
82 {:meta, [property: "twitter:card", content: "player"], []},
83 {:meta, [property: "twitter:player:width", content: "480"], []},
84 {:meta, [property: "twitter:player:height", content: "80"], []},
85 {:meta, [property: "twitter:player", content: player_url(id)], []}
91 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
94 property: "twitter:player",
95 content: Utils.attachment_url(url["href"])
100 # TODO: Need the true width and height values here or Twitter renders an iFrame with a bad aspect ratio
103 {:meta, [property: "twitter:card", content: "player"], []},
104 {:meta, [property: "twitter:player", content: player_url(id)], []},
105 {:meta, [property: "twitter:player:width", content: "480"], []},
106 {:meta, [property: "twitter:player:height", content: "480"], []}
119 defp player_url(id) do
120 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)