b7c5dc64e8e483fb83c55f661409a06976b317f6
[akkoma] / lib / pleroma / web / metadata / opengraph.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
5 alias Pleroma.Web.Metadata.Providers.Provider
6 alias Pleroma.{HTML, Formatter, User}
7 alias Pleroma.Web.MediaProxy
8
9 @behaviour Provider
10
11 @impl Provider
12 def build_tags(%{activity: activity, user: user}) do
13 with truncated_content = scrub_html_and_truncate(activity) do
14 attachments = build_attachments(activity)
15
16 [
17 {:meta,
18 [
19 property: "og:title",
20 content: user_name_string(user)
21 ], []},
22 {:meta, [property: "og:url", content: activity.data["id"]], []},
23 {:meta, [property: "og:description", content: truncated_content], []}
24 ] ++
25 if attachments == [] or
26 Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do
27 [
28 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
29 {:meta, [property: "og:image:width", content: 120], []},
30 {:meta, [property: "og:image:height", content: 120], []}
31 ]
32 else
33 attachments
34 end
35 end
36 end
37
38 @impl Provider
39 def build_tags(%{user: user}) do
40 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
41 [
42 {:meta,
43 [
44 property: "og:title",
45 content: user_name_string(user)
46 ], []},
47 {:meta, [property: "og:url", content: User.profile_url(user)], []},
48 {:meta, [property: "og:description", content: truncated_bio], []},
49 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
50 {:meta, [property: "og:image:width", content: 120], []},
51 {:meta, [property: "og:image:height", content: 120], []}
52 ]
53 end
54 end
55
56 defp build_attachments(%{data: %{"object" => %{"attachment" => attachments}}} = _activity) do
57 Enum.reduce(attachments, [], fn attachment, acc ->
58 rendered_tags =
59 Enum.reduce(attachment["url"], [], fn url, acc ->
60 media_type =
61 Enum.find(["image", "audio", "video"], fn media_type ->
62 String.starts_with?(url["mediaType"], media_type)
63 end)
64
65 if media_type do
66 [
67 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
68 | acc
69 ]
70 else
71 acc
72 end
73 end)
74
75 acc ++ rendered_tags
76 end)
77 end
78
79 defp scrub_html_and_truncate(%{data: %{"object" => %{"content" => content}}} = activity) do
80 content
81 # html content comes from DB already encoded, decode first and scrub after
82 |> HtmlEntities.decode()
83 |> String.replace(~r/<br\s?\/?>/, " ")
84 |> HTML.get_cached_stripped_html_for_object(activity, __MODULE__)
85 |> Formatter.truncate()
86 end
87
88 defp scrub_html_and_truncate(content) when is_binary(content) do
89 content
90 # html content comes from DB already encoded, decode first and scrub after
91 |> HtmlEntities.decode()
92 |> String.replace(~r/<br\s?\/?>/, " ")
93 |> HTML.strip_tags()
94 |> Formatter.truncate()
95 end
96
97 defp attachment_url(url) do
98 MediaProxy.url(url)
99 end
100
101 defp user_name_string(user) do
102 "#{user.name} " <>
103 if user.local do
104 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
105 else
106 "(@#{user.nickname})"
107 end
108 end
109 end