open_graph.ex 4.68 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
feld committed
40
          {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))], []},
rinpatch's avatar
rinpatch committed
41
42
          {:meta, [property: "og:image:width", content: 150], []},
          {:meta, [property: "og:image:height", content: 150], []}
rinpatch's avatar
rinpatch committed
43
44
45
46
        ]
      else
        attachments
      end
47
48
49
50
  end

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

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

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

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

            _ ->
              acc
rinpatch's avatar
rinpatch committed
103
104
105
106
107
108
          end
        end)

      acc ++ rendered_tags
    end)
  end
109
110

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

  # 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141

  defp maybe_add_video_thumbnail(url, metadata) do
    cond do
      Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
        [
          {: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"])], []}
        ]

      true ->
        metadata
    end
  end
142
end