1 # Pleroma: A lightweight social networking server
3 # Copyright © 2017-2019 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)
21 if scrubbed_content != "" and scrubbed_content != "\u200B" do
22 "“" <> scrubbed_content <> "”"
29 {:meta, [property: "twitter:description", content: content], []}
31 if attachments == [] or Metadata.activity_nsfw?(object) do
34 {:meta, [property: "twitter:card", content: "summary_large_image"], []}
42 def build_tags(%{user: user}) do
43 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
46 {:meta, [property: "twitter:description", content: truncated_bio], []},
48 {:meta, [property: "twitter:card", content: "summary"], []}
53 defp title_tag(user) do
54 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
57 def image_tag(user) do
58 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
61 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
62 Enum.reduce(attachments, [], fn attachment, acc ->
64 Enum.reduce(attachment["url"], [], fn url, acc ->
65 # TODO: Add additional properties to objects when we have the data available.
66 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
69 {:meta, [property: "twitter:card", content: "player"], []},
70 {:meta, [property: "twitter:player:width", content: "480"], []},
71 {:meta, [property: "twitter:player:height", content: "80"], []},
72 {:meta, [property: "twitter:player", content: player_url(id)], []}
78 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
81 property: "twitter:player",
82 content: Utils.attachment_url(url["href"])
87 # TODO: Need the true width and height values here or Twitter renders an iFrame with
91 {:meta, [property: "twitter:card", content: "player"], []},
92 {:meta, [property: "twitter:player", content: player_url(id)], []},
93 {:meta, [property: "twitter:player:width", content: "480"], []},
94 {:meta, [property: "twitter:player:height", content: "480"], []}
107 defp build_attachments(_id, _object), do: []
109 defp player_url(id) do
110 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)