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, z = %{data: %{"attachment" => attachments}}) do
72 Enum.reduce(attachments, [], fn attachment, acc ->
74 Enum.reduce(attachment["url"], [], fn url, acc ->
76 Enum.find(["image", "audio", "video"], fn media_type ->
77 String.starts_with?(url["mediaType"], media_type)
80 # TODO: Add additional properties to objects when we have the data available.
84 {:meta, [property: "twitter:card", content: "player"], []},
85 {:meta, [property: "twitter:player:width", content: "480"], []},
86 {:meta, [property: "twitter:player:height", content: "80"], []},
87 {:meta, [property: "twitter:player", content: player_url(id)], []}
93 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
96 property: "twitter:player",
97 content: Utils.attachment_url(url["href"])
102 # TODO: Need the true width and height values here or Twitter renders an iFrame with a bad aspect ratio
105 {:meta, [property: "twitter:card", content: "player"], []},
106 {:meta, [property: "twitter:player", content: player_url(id)], []},
107 {:meta, [property: "twitter:player:width", content: "480"], []},
108 {:meta, [property: "twitter:player:height", content: "480"], []}
121 defp player_url(id) do
122 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)