Credo fixes: alias grouping/ordering
[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
5 defmodule Pleroma.Web.Metadata.Providers.OpenGraph do
6 alias Pleroma.{HTML, Formatter, User}
7 alias Pleroma.Web.{Metadata, MediaProxy}
8 alias Pleroma.Web.Metadata.Providers.Provider
9
10 @behaviour Provider
11
12 @impl Provider
13 def build_tags(%{
14 object: object,
15 url: url,
16 user: user
17 }) do
18 attachments = build_attachments(object)
19 scrubbed_content = scrub_html_and_truncate(object)
20 # Zero width space
21 content =
22 if scrubbed_content != "" and scrubbed_content != "\u200B" do
23 ": “" <> scrubbed_content <> "”"
24 else
25 ""
26 end
27
28 # Most previews only show og:title which is inconvenient. Instagram
29 # hacks this by putting the description in the title and making the
30 # description longer prefixed by how many likes and shares the post
31 # has. Here we use the descriptive nickname in the title, and expand
32 # the full account & nickname in the description. We also use the cute^Wevil
33 # smart quotes around the status text like Instagram, too.
34 [
35 {:meta,
36 [
37 property: "og:title",
38 content: "#{user.name}" <> content
39 ], []},
40 {:meta, [property: "og:url", content: url], []},
41 {:meta,
42 [
43 property: "og:description",
44 content: "#{user_name_string(user)}" <> content
45 ], []},
46 {:meta, [property: "og:type", content: "website"], []}
47 ] ++
48 if attachments == [] or Metadata.activity_nsfw?(object) do
49 [
50 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
51 {:meta, [property: "og:image:width", content: 150], []},
52 {:meta, [property: "og:image:height", content: 150], []}
53 ]
54 else
55 attachments
56 end
57 end
58
59 @impl Provider
60 def build_tags(%{user: user}) do
61 with truncated_bio = scrub_html_and_truncate(user.bio || "") do
62 [
63 {:meta,
64 [
65 property: "og:title",
66 content: user_name_string(user)
67 ], []},
68 {:meta, [property: "og:url", content: User.profile_url(user)], []},
69 {:meta, [property: "og:description", content: truncated_bio], []},
70 {:meta, [property: "og:type", content: "website"], []},
71 {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []},
72 {:meta, [property: "og:image:width", content: 150], []},
73 {:meta, [property: "og:image:height", content: 150], []}
74 ]
75 end
76 end
77
78 defp build_attachments(%{data: %{"attachment" => attachments}}) do
79 Enum.reduce(attachments, [], fn attachment, acc ->
80 rendered_tags =
81 Enum.reduce(attachment["url"], [], fn url, acc ->
82 media_type =
83 Enum.find(["image", "audio", "video"], fn media_type ->
84 String.starts_with?(url["mediaType"], media_type)
85 end)
86
87 # TODO: Add additional properties to objects when we have the data available.
88 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
89 # object when a Video or GIF is attached it will display that in the Whatsapp Rich Preview.
90 case media_type do
91 "audio" ->
92 [
93 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
94 | acc
95 ]
96
97 "image" ->
98 [
99 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])],
100 []},
101 {:meta, [property: "og:image:width", content: 150], []},
102 {:meta, [property: "og:image:height", content: 150], []}
103 | acc
104 ]
105
106 "video" ->
107 [
108 {:meta, [property: "og:" <> media_type, content: attachment_url(url["href"])], []}
109 | acc
110 ]
111
112 _ ->
113 acc
114 end
115 end)
116
117 acc ++ rendered_tags
118 end)
119 end
120
121 defp scrub_html_and_truncate(%{data: %{"content" => content}} = object) do
122 content
123 # html content comes from DB already encoded, decode first and scrub after
124 |> HtmlEntities.decode()
125 |> String.replace(~r/<br\s?\/?>/, " ")
126 |> HTML.get_cached_stripped_html_for_object(object, __MODULE__)
127 |> Formatter.demojify()
128 |> Formatter.truncate()
129 end
130
131 defp scrub_html_and_truncate(content) when is_binary(content) do
132 content
133 # html content comes from DB already encoded, decode first and scrub after
134 |> HtmlEntities.decode()
135 |> String.replace(~r/<br\s?\/?>/, " ")
136 |> HTML.strip_tags()
137 |> Formatter.demojify()
138 |> Formatter.truncate()
139 end
140
141 defp attachment_url(url) do
142 MediaProxy.url(url)
143 end
144
145 defp user_name_string(user) do
146 "#{user.name} " <>
147 if user.local do
148 "(@#{user.nickname}@#{Pleroma.Web.Endpoint.host()})"
149 else
150 "(@#{user.nickname})"
151 end
152 end
153 end