Lint
[akkoma] / lib / pleroma / web / metadata / providers / open_graph.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.User
7 alias Pleroma.Web.MediaProxy
8 alias Pleroma.Web.Metadata
9 alias Pleroma.Web.Metadata.Providers.Provider
10 alias Pleroma.Web.Metadata.Utils
11
12 @behaviour Provider
13 @media_types ["image", "audio", "video"]
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 = Utils.scrub_html_and_truncate(object)
23
24 [
25 {:meta,
26 [
27 property: "og:title",
28 content: Utils.user_name_string(user)
29 ], []},
30 {:meta, [property: "og:url", content: url], []},
31 {:meta,
32 [
33 property: "og:description",
34 content: scrubbed_content
35 ], []},
36 {:meta, [property: "og:type", content: "article"], []}
37 ] ++
38 if attachments == [] or Metadata.activity_nsfw?(object) do
39 [
40 {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
41 []},
42 {:meta, [property: "og:image:width", content: 150], []},
43 {:meta, [property: "og:image:height", content: 150], []}
44 ]
45 else
46 attachments
47 end
48 end
49
50 @impl Provider
51 def build_tags(%{user: user}) do
52 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
53 [
54 {:meta,
55 [
56 property: "og:title",
57 content: Utils.user_name_string(user)
58 ], []},
59 {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
60 {:meta, [property: "og:description", content: truncated_bio], []},
61 {:meta, [property: "og:type", content: "article"], []},
62 {:meta, [property: "og:image", content: MediaProxy.preview_url(User.avatar_url(user))],
63 []},
64 {:meta, [property: "og:image:width", content: 150], []},
65 {:meta, [property: "og:image:height", content: 150], []}
66 ]
67 end
68 end
69
70 defp build_attachments(%{data: %{"attachment" => attachments}}) do
71 Enum.reduce(attachments, [], fn attachment, acc ->
72 rendered_tags =
73 Enum.reduce(attachment["url"], [], fn url, acc ->
74 # TODO: Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
75 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
76 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
77 "audio" ->
78 [
79 {:meta, [property: "og:audio", content: MediaProxy.url(url["href"])], []}
80 | acc
81 ]
82
83 # Not using preview_url for this. It saves bandwidth, but the image dimensions will be wrong.
84 # We generate it on the fly and have no way to capture or analyze the image to get the dimensions.
85 # This can be an issue for apps/FEs rendering images in timelines too, but you can get clever with
86 # the aspect ratio metadata as a workaround.
87 "image" ->
88 [
89 {:meta, [property: "og:image", content: MediaProxy.url(url["href"])], []},
90 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
91 | acc
92 ]
93 |> maybe_add_dimensions(url)
94
95 "video" ->
96 [
97 {:meta, [property: "og:video", content: MediaProxy.url(url["href"])], []}
98 | acc
99 ]
100 |> maybe_add_dimensions(url)
101 |> maybe_add_video_thumbnail(url)
102
103 _ ->
104 acc
105 end
106 end)
107
108 acc ++ rendered_tags
109 end)
110 end
111
112 defp build_attachments(_), do: []
113
114 # We can use url["mediaType"] to dynamically fill the metadata
115 defp maybe_add_dimensions(metadata, url) do
116 type = url["mediaType"] |> String.split("/") |> List.first()
117
118 cond do
119 !is_nil(url["height"]) && !is_nil(url["width"]) ->
120 metadata ++
121 [
122 {:meta, [property: "og:#{type}:width", content: "#{url["width"]}"], []},
123 {:meta, [property: "og:#{type}:height", content: "#{url["height"]}"], []}
124 ]
125
126 true ->
127 metadata
128 end
129 end
130
131 defp maybe_add_video_thumbnail(metadata, url) do
132 cond do
133 Pleroma.Config.get([:media_preview_proxy, :enabled], false) ->
134 metadata ++
135 [
136 {:meta, [property: "og:image:width", content: "#{url["width"]}"], []},
137 {:meta, [property: "og:image:height", content: "#{url["height"]}"], []},
138 {:meta, [property: "og:image", content: MediaProxy.preview_url(url["href"])], []}
139 ]
140
141 true ->
142 metadata
143 end
144 end
145 end