open_graph.ex 4.76 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3
# SPDX-License-Identifier: AGPL-3.0-only
4

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

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

  @impl Provider
16
  def build_tags(%{
17
        object: object,
18
        url: url,
19
20
        user: user
      }) do
21
    attachments = build_attachments(object)
feld's avatar
feld committed
22
    scrubbed_content = Utils.scrub_html_and_truncate(object)
rinpatch's avatar
rinpatch committed
23

rinpatch's avatar
rinpatch committed
24
25
26
27
    [
      {:meta,
       [
         property: "og:title",
io 's avatar
io committed
28
         content: Utils.user_name_string(user)
rinpatch's avatar
rinpatch committed
29
       ], []},
30
      {:meta, [property: "og:url", content: url], []},
rinpatch's avatar
rinpatch committed
31
32
33
      {:meta,
       [
         property: "og:description",
io 's avatar
io committed
34
         content: scrubbed_content
rinpatch's avatar
rinpatch committed
35
       ], []},
36
      {:meta, [property: "og:type", content: "article"], []}
rinpatch's avatar
rinpatch committed
37
    ] ++
38
      if attachments == [] or Metadata.activity_nsfw?(object) do
rinpatch's avatar
rinpatch committed
39
        [
feld's avatar
Lint    
feld committed
40
41
          {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
           []},
rinpatch's avatar
rinpatch committed
42
43
          {:meta, [property: "og:image:width", content: 150], []},
          {:meta, [property: "og:image:height", content: 150], []}
rinpatch's avatar
rinpatch committed
44
45
46
47
        ]
      else
        attachments
      end
48
49
50
51
  end

  @impl Provider
  def build_tags(%{user: user}) do
52
    with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
53
54
55
56
      [
        {:meta,
         [
           property: "og:title",
feld's avatar
feld committed
57
           content: Utils.user_name_string(user)
58
         ], []},
59
        {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
60
        {:meta, [property: "og:description", content: truncated_bio], []},
61
        {:meta, [property: "og:type", content: "article"], []},
feld's avatar
Lint    
feld committed
62
63
        {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
         []},
rinpatch's avatar
rinpatch committed
64
65
        {:meta, [property: "og:image:width", content: 150], []},
        {:meta, [property: "og:image:height", content: 150], []}
66
67
68
69
      ]
    end
  end

70
  defp build_attachments(%{data: %{"attachment" => attachments}}) do
71
    Enum.reduce(attachments, [], fn attachment, acc ->
rinpatch's avatar
rinpatch committed
72
      rendered_tags =
73
        Enum.reduce(attachment["url"], [], fn url, acc ->
74
          # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
75
          # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
Maksim's avatar
Maksim committed
76
          case Utils.fetch_media_type(@media_types, url["mediaType"]) do
rinpatch's avatar
rinpatch committed
77
78
            "audio" ->
              [
feld's avatar
feld committed
79
                {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
rinpatch's avatar
rinpatch committed
80
81
82
                | acc
              ]

feld's avatar
Credo    
feld committed
83
84
85
86
87
            # 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
            # analyze the 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.
rinpatch's avatar
rinpatch committed
88
89
            "image" ->
              [
feld's avatar
feld committed
90
                {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
io 's avatar
io committed
91
                {:meta, [property: "og:image:alt", content: attachment["name"]], []}
rinpatch's avatar
rinpatch committed
92
93
                | acc
              ]
94
              |> maybe_add_dimensions(url)
rinpatch's avatar
rinpatch committed
95
96
97

            "video" ->
              [
feld's avatar
feld committed
98
                {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
rinpatch's avatar
rinpatch committed
99
100
                | acc
              ]
101
              |> maybe_add_dimensions(url)
102
              |> maybe_add_video_thumbnail(url)
rinpatch's avatar
rinpatch committed
103
104
105

            _ ->
              acc
rinpatch's avatar
rinpatch committed
106
107
108
109
110
111
          end
        end)

      acc ++ rendered_tags
    end)
  end
112
113

  defp build_attachments(_), do: []
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

  # We can use url["mediaType"] to dynamically fill the metadata
  defp maybe_add_dimensions(metadata, url) do
    type = url["mediaType"] |> String.split("/") |> List.first()

    cond do
      !is_nil(url["height"]) && !is_nil(url["width"]) ->
        metadata ++
          [
            {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
            {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
          ]

      true ->
        metadata
    end
  end
131

132
  defp maybe_add_video_thumbnail(metadata, url) do
133
134
    cond do
      Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
135
        metadata ++
feld's avatar
Lint    
feld committed
136
137
138
139
140
          [
            {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
            {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
            {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}
          ]
141
142
143
144
145

      true ->
        metadata
    end
  end
146
end