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