opengraph.ex 3.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
  alias Pleroma.Web.Metadata.Providers.Provider
  alias Pleroma.{HTML, Formatter, User}
  alias Pleroma.Web.MediaProxy

  @behaviour Provider

  @impl Provider
  def build_tags(%{activity: activity, user: user}) do
rinpatch's avatar
rinpatch committed
10
    with truncated_content = scrub_html_and_truncate(activity) do
rinpatch's avatar
rinpatch committed
11
      attachments = build_attachments(activity)
rinpatch's avatar
rinpatch committed
12

13
14
15
16
17
18
19
      [
        {:meta,
         [
           property: "og:title",
           content: user_name_string(user)
         ], []},
        {:meta, [property: "og:url", content: activity.data["id"]], []},
rinpatch's avatar
rinpatch committed
20
        {:meta, [property: "og:description", content: truncated_content], []}
rinpatch's avatar
rinpatch committed
21
      ] ++
22
23
        if attachments == [] or
             Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do
rinpatch's avatar
rinpatch committed
24
25
26
27
28
29
30
31
          [
            {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
            {:meta, [property: "og:image:width", content: 120], []},
            {:meta, [property: "og:image:height", content: 120], []}
          ]
        else
          attachments
        end
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    end
  end

  @impl Provider
  def build_tags(%{user: user}) do
    with truncated_bio = scrub_html_and_truncate(user.bio || "") do
      [
        {:meta,
         [
           property: "og:title",
           content: user_name_string(user)
         ], []},
        {:meta, [property: "og:url", content: User.profile_url(user)], []},
        {:meta, [property: "og:description", content: truncated_bio], []},
rinpatch's avatar
rinpatch committed
46
        {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
47
        {:meta, [property: "og:image:width", content: 120], []},
rinpatch's avatar
rinpatch committed
48
        {:meta, [property: "og:image:height", content: 120], []}
49
50
51
52
      ]
    end
  end

53
54
  defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
    Enum.reduce(attachments, [], fn attachment, acc ->
rinpatch's avatar
rinpatch committed
55
      rendered_tags =
56
        Enum.reduce(attachment["url"], [], fn url, acc ->
rinpatch's avatar
rinpatch committed
57
58
59
60
61
62
          media_type =
            Enum.find(["image", "audio", "video"], fn media_type ->
              String.starts_with?(url["mediaType"], media_type)
            end)

          if media_type do
63
64
65
66
            [
              {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
              | acc
            ]
rinpatch's avatar
rinpatch committed
67
          else
68
            acc
rinpatch's avatar
rinpatch committed
69
70
71
72
73
74
75
          end
        end)

      acc ++ rendered_tags
    end)
  end

76
  defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
rinpatch's avatar
rinpatch committed
77
78
79
80
81
82
83
84
    content
    # html content comes from DB already encoded, decode first and scrub after
    |> HtmlEntities.decode()
    |> String.replace(~r/<br\s?\/?>/, " ")
    |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
    |> Formatter.truncate()
  end

85
  defp scrub_html_and_truncate(content) when is_binary(content) do
86
87
88
    content
    # html content comes from DB already encoded, decode first and scrub after
    |> HtmlEntities.decode()
89
    |> String.replace(~r/<br\s?\/?>/, " ")
90
91
92
    |> HTML.strip_tags()
    |> Formatter.truncate()
  end
93

rinpatch's avatar
rinpatch committed
94
95
  defp attachment_url(url) do
    MediaProxy.url(url)
96
97
98
  end

  defp user_name_string(user) do
rinpatch's avatar
rinpatch committed
99
    "#{user.name} " <>
100
101
102
103
104
105
106
      if user.local do
        "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
      else
        "(@#{user.nickname})"
      end
  end
end