twitter_card.ex 4.57 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Maksim's avatar
Maksim committed
2

Haelwenn's avatar
Haelwenn committed
3
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
4
# SPDX-License-Identifier: AGPL-3.0-only
5

rinpatch's avatar
rinpatch committed
6
defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
feld's avatar
feld committed
7
  alias Pleroma.User
feld's avatar
feld committed
8
  alias Pleroma.Web.MediaProxy
9
  alias Pleroma.Web.Metadata
feld's avatar
feld committed
10
11
  alias Pleroma.Web.Metadata.Providers.Provider
  alias Pleroma.Web.Metadata.Utils
12
13

  @behaviour Provider
Maksim's avatar
Maksim committed
14
  @media_types ["image", "audio", "video"]
15
16

  @impl Provider
Maksim's avatar
Maksim committed
17
  def build_tags(%{activity_id: id, object: object, user: user}) do
href's avatar
href committed
18
    attachments = build_attachments(id, object)
feld's avatar
feld committed
19
20
21
    scrubbed_content = Utils.scrub_html_and_truncate(object)

    [
Maksim's avatar
Maksim committed
22
      title_tag(user),
io 's avatar
io committed
23
      {:meta, [property: "twitter:description", content: scrubbed_content], []}
feld's avatar
feld committed
24
25
26
    ] ++
      if attachments == [] or Metadata.activity_nsfw?(object) do
        [
Maksim's avatar
Maksim committed
27
          image_tag(user),
28
          {:meta, [property: "twitter:card", content: "summary"], []}
feld's avatar
feld committed
29
30
31
        ]
      else
        attachments
rinpatch's avatar
rinpatch committed
32
33
34
      end
  end

35
  @impl Provider
feld's avatar
feld committed
36
  def build_tags(%{user: user}) do
37
    with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
feld's avatar
feld committed
38
      [
Maksim's avatar
Maksim committed
39
        title_tag(user),
feld's avatar
feld committed
40
        {:meta, [property: "twitter:description", content: truncated_bio], []},
Maksim's avatar
Maksim committed
41
        image_tag(user),
feld's avatar
feld committed
42
43
44
        {:meta, [property: "twitter:card", content: "summary"], []}
      ]
    end
rinpatch's avatar
rinpatch committed
45
46
  end

Maksim's avatar
Maksim committed
47
48
49
50
51
  defp title_tag(user) do
    {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
  end

  def image_tag(user) do
feld's avatar
feld committed
52
53
    {:meta, [property: "twitter:image", content: MediaProxy.preview_url(User.avatar_url(user))],
     []}
Maksim's avatar
Maksim committed
54
55
  end

56
  defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
feld's avatar
feld committed
57
58
59
    Enum.reduce(attachments, [], fn attachment, acc ->
      rendered_tags =
        Enum.reduce(attachment["url"], [], fn url, acc ->
Maksim's avatar
Maksim committed
60
          case Utils.fetch_media_type(@media_types, url["mediaType"]) do
feld's avatar
feld committed
61
62
63
            "audio" ->
              [
                {:meta, [property: "twitter:card", content: "player"], []},
href's avatar
href committed
64
65
66
                {:meta, [property: "twitter:player:width", content: "480"], []},
                {:meta, [property: "twitter:player:height", content: "80"], []},
                {:meta, [property: "twitter:player", content: player_url(id)], []}
feld's avatar
feld committed
67
68
69
                | acc
              ]

feld's avatar
Credo    
feld committed
70
71
            # Not using preview_url for this. It saves bandwidth, but the image dimensions will
            # be wrong. We generate it on the fly and have no way to capture or analyze the
feld's avatar
feld committed
72
73
74
            # image to get the dimensions. This can be an issue for apps/FEs rendering images
            # in timelines too, but you can get clever with the aspect ratio metadata as a
            # workaround.
feld's avatar
feld committed
75
76
77
78
79
80
            "image" ->
              [
                {:meta, [property: "twitter:card", content: "summary_large_image"], []},
                {:meta,
                 [
                   property: "twitter:player",
feld's avatar
feld committed
81
                   content: MediaProxy.url(url["href"])
82
                 ], []}
feld's avatar
feld committed
83
84
                | acc
              ]
85
              |> maybe_add_dimensions(url)
feld's avatar
feld committed
86
87

            "video" ->
88
89
90
91
              # fallback to old placeholder values
              height = url["height"] || 480
              width = url["width"] || 480

feld's avatar
feld committed
92
93
              [
                {:meta, [property: "twitter:card", content: "player"], []},
href's avatar
href committed
94
                {:meta, [property: "twitter:player", content: player_url(id)], []},
95
96
                {:meta, [property: "twitter:player:width", content: "#{width}"], []},
                {:meta, [property: "twitter:player:height", content: "#{height}"], []},
feld's avatar
Lint    
feld committed
97
98
                {:meta, [property: "twitter:player:stream", content: MediaProxy.url(url["href"])],
                 []},
feld's avatar
feld committed
99
100
                {:meta,
                 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
feld's avatar
feld committed
101
102
103
104
105
106
                | acc
              ]

            _ ->
              acc
          end
rinpatch's avatar
rinpatch committed
107
        end)
feld's avatar
feld committed
108
109

      acc ++ rendered_tags
rinpatch's avatar
rinpatch committed
110
111
    end)
  end
rinpatch's avatar
rinpatch committed
112

113
  defp build_attachments(_id, _object), do: []
href's avatar
href committed
114
115
116
117

  defp player_url(id) do
    Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
  end
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

  # Videos have problems without dimensions, but we used to not provide WxH for images.
  # A default (read: incorrect) fallback for images is likely to cause rendering bugs.
  defp maybe_add_dimensions(metadata, url) do
    cond do
      !is_nil(url["height"]) && !is_nil(url["width"]) ->
        metadata ++
          [
            {:meta, [property: "twitter:player:width", content: "#{url["width"]}"], []},
            {:meta, [property: "twitter:player:height", content: "#{url["height"]}"], []}
          ]

      true ->
        metadata
    end
  end
rinpatch's avatar
rinpatch committed
134
end