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