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