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