opengraph.ex 3.2 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

rinpatch's avatar
rinpatch committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  defp build_attachments(activity) do
    Enum.reduce(activity.data["object"]["attachment"], [], fn attachment, acc ->
      rendered_tags =
        Enum.map(attachment["url"], fn url ->
          media_type =
            Enum.find(["image", "audio", "video"], fn media_type ->
              String.starts_with?(url["mediaType"], media_type)
            end)

          if media_type do
            {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
          else
            nil
          end
        end)

      Enum.reject(rendered_tags, &is_nil/1)
      acc ++ rendered_tags
    end)
  end

rinpatch's avatar
rinpatch committed
74
75
76
77
78
79
80
81
82
  defp scrub_html_and_truncate(%{data: %{ "object" => %{ "content" => content}}} = activity) do
    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

83
84
85
86
  defp scrub_html_and_truncate(content) do
    content
    # html content comes from DB already encoded, decode first and scrub after
    |> HtmlEntities.decode()
87
    |> String.replace(~r/<br\s?\/?>/, " ")
88
89
90
    |> HTML.strip_tags()
    |> Formatter.truncate()
  end
rinpatch's avatar
rinpatch committed
91
92
  defp attachment_url(url) do
    MediaProxy.url(url)
93
94
95
  end

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