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
18 attachments = build_attachments(object)
19 scrubbed_content = Utils.scrub_html_and_truncate(object)
22 if scrubbed_content != "" and scrubbed_content != "\u200B" do
23 "“" <> scrubbed_content <> "”"
31 property: "twitter:title",
32 content: Utils.user_name_string(user)
36 property: "twitter:description",
40 if attachments == [] or Metadata.activity_nsfw?(object) do
43 [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []},
44 {:meta, [property: "twitter:card", content: "summary_large_image"], []}
52 def build_tags(%{user: user}) do
53 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
57 property: "twitter:title",
58 content: Utils.user_name_string(user)
60 {:meta, [property: "twitter:description", content: truncated_bio], []},
61 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))],
63 {:meta, [property: "twitter:card", content: "summary"], []}
68 defp build_attachments(%{data: %{"attachment" => attachments}}) do
69 Enum.reduce(attachments, [], fn attachment, acc ->
71 Enum.reduce(attachment["url"], [], fn url, acc ->
72 content_type = url["mediaType"]
75 Enum.find(["image", "audio", "video"], fn media_type ->
76 String.starts_with?(url["mediaType"], media_type)
79 # TODO: Add additional properties to objects when we have the data available.
83 {:meta, [property: "twitter:card", content: "player"], []},
84 {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])],
91 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
94 property: "twitter:player",
97 Pleroma.Uploaders.Uploader.preview_url(content_type, url["href"])
103 # TODO: Need the true width and height values here or Twitter renders an iFrame with a bad aspect ratio
106 {:meta, [property: "twitter:card", content: "player"], []},
107 {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])],
109 {:meta, [property: "twitter:player:width", content: "1280"], []},
110 {:meta, [property: "twitter:player:height", content: "720"], []}