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.Metadata
9 alias Pleroma.Web.Metadata.Providers.Provider
10 alias Pleroma.Web.Metadata.Utils
13 @media_types ["image", "audio", "video"]
16 def build_tags(%{activity_id: id, object: object, user: user}) do
17 attachments = build_attachments(id, object)
18 scrubbed_content = Utils.scrub_html_and_truncate(object)
22 {:meta, [property: "twitter:description", content: scrubbed_content], []}
24 if attachments == [] or Metadata.activity_nsfw?(object) do
27 {:meta, [property: "twitter:card", content: "summary"], []}
35 def build_tags(%{user: user}) do
36 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
39 {:meta, [property: "twitter:description", content: truncated_bio], []},
41 {:meta, [property: "twitter:card", content: "summary"], []}
46 defp title_tag(user) do
47 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
50 def image_tag(user) do
51 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
54 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
55 Enum.reduce(attachments, [], fn attachment, acc ->
57 Enum.reduce(attachment["url"], [], fn url, acc ->
58 # TODO: Add additional properties to objects when we have the data available.
59 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
62 {:meta, [property: "twitter:card", content: "player"], []},
63 {:meta, [property: "twitter:player:width", content: "480"], []},
64 {:meta, [property: "twitter:player:height", content: "80"], []},
65 {:meta, [property: "twitter:player", content: player_url(id)], []}
71 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
74 property: "twitter:player",
75 content: Utils.attachment_url(url["href"])
80 # TODO: Need the true width and height values here or Twitter renders an iFrame with
84 {:meta, [property: "twitter:card", content: "player"], []},
85 {:meta, [property: "twitter:player", content: player_url(id)], []},
86 {:meta, [property: "twitter:player:width", content: "480"], []},
87 {:meta, [property: "twitter:player:height", content: "480"], []},
88 {:meta, [property: "twitter:player:stream", content: url["href"]], []},
90 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
103 defp build_attachments(_id, _object), do: []
105 defp player_url(id) do
106 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)