opengraph.ex 4 KB
Newer Older
1
# Pleroma: A lightweight social networking server
feld's avatar
feld committed
2
# Copyright © 2017-2020 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
7
  alias Pleroma.User
  alias Pleroma.Web.Metadata
8
  alias Pleroma.Web.Metadata.Providers.Provider
feld's avatar
feld committed
9
  alias Pleroma.Web.Metadata.Utils
10
11

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

  @impl Provider
15
  def build_tags(%{
16
        object: object,
17
        url: url,
18
19
        user: user
      }) do
20
    attachments = build_attachments(object)
feld's avatar
feld committed
21
    scrubbed_content = Utils.scrub_html_and_truncate(object)
22
23
24
25
26
27
28
    # Zero width space
    content =
      if scrubbed_content != "" and scrubbed_content != "\u200B" do
        ": “" <> scrubbed_content <> "”"
      else
        ""
      end
rinpatch's avatar
rinpatch committed
29

rinpatch's avatar
rinpatch committed
30
31
32
33
34
35
    # Most previews only show og:title which is inconvenient. Instagram
    # hacks this by putting the description in the title and making the
    # description longer prefixed by how many likes and shares the post
    # has. Here we use the descriptive nickname in the title, and expand
    # the full account & nickname in the description. We also use the cute^Wevil
    # smart quotes around the status text like Instagram, too.
rinpatch's avatar
rinpatch committed
36
37
38
39
    [
      {:meta,
       [
         property: "og:title",
40
         content: "#{user.name}" <> content
rinpatch's avatar
rinpatch committed
41
       ], []},
42
      {:meta, [property: "og:url", content: url], []},
rinpatch's avatar
rinpatch committed
43
44
45
      {:meta,
       [
         property: "og:description",
feld's avatar
feld committed
46
         content: "#{Utils.user_name_string(user)}" <> content
rinpatch's avatar
rinpatch committed
47
48
       ], []},
      {:meta, [property: "og:type", content: "website"], []}
rinpatch's avatar
rinpatch committed
49
    ] ++
50
      if attachments == [] or Metadata.activity_nsfw?(object) do
rinpatch's avatar
rinpatch committed
51
        [
feld's avatar
feld committed
52
53
          {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
           []},
rinpatch's avatar
rinpatch committed
54
55
          {:meta, [property: "og:image:width", content: 150], []},
          {:meta, [property: "og:image:height", content: 150], []}
rinpatch's avatar
rinpatch committed
56
57
58
59
        ]
      else
        attachments
      end
60
61
62
63
  end

  @impl Provider
  def build_tags(%{user: user}) do
64
    with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
65
66
67
68
      [
        {:meta,
         [
           property: "og:title",
feld's avatar
feld committed
69
           content: Utils.user_name_string(user)
70
         ], []},
71
        {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
72
        {:meta, [property: "og:description", content: truncated_bio], []},
rinpatch's avatar
rinpatch committed
73
        {:meta, [property: "og:type", content: "website"], []},
feld's avatar
feld committed
74
        {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))], []},
rinpatch's avatar
rinpatch committed
75
76
        {:meta, [property: "og:image:width", content: 150], []},
        {:meta, [property: "og:image:height", content: 150], []}
77
78
79
80
      ]
    end
  end

81
  defp build_attachments(%{data: %{"attachment" => attachments}}) do
82
    Enum.reduce(attachments, [], fn attachment, acc ->
rinpatch's avatar
rinpatch committed
83
      rendered_tags =
84
        Enum.reduce(attachment["url"], [], fn url, acc ->
rinpatch's avatar
rinpatch committed
85
86
          # TODO: Add additional properties to objects when we have the data available.
          # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
87
          # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
Maksim's avatar
Maksim committed
88
          case Utils.fetch_media_type(@media_types, url["mediaType"]) do
rinpatch's avatar
rinpatch committed
89
90
            "audio" ->
              [
Maksim's avatar
Maksim committed
91
                {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
rinpatch's avatar
rinpatch committed
92
93
94
95
96
                | acc
              ]

            "image" ->
              [
Maksim's avatar
Maksim committed
97
                {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
rinpatch's avatar
rinpatch committed
98
99
100
101
102
103
104
                {:meta, [property: "og:image:width", content: 150], []},
                {:meta, [property: "og:image:height", content: 150], []}
                | acc
              ]

            "video" ->
              [
Maksim's avatar
Maksim committed
105
                {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
rinpatch's avatar
rinpatch committed
106
107
108
109
110
                | acc
              ]

            _ ->
              acc
rinpatch's avatar
rinpatch committed
111
112
113
114
115
116
          end
        end)

      acc ++ rendered_tags
    end)
  end
117
118

  defp build_attachments(_), do: []
119
end