Merge branch 'cycles-config-loader-redux' into 'develop'
[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.Metadata
8 alias Pleroma.Web.Metadata.Providers.Provider
9 alias Pleroma.Web.Metadata.Utils
10
11 @behaviour Provider
12 @media_types ["image", "audio", "video"]
13
14 @impl Provider
15 def build_tags(%{
16 object: object,
17 url: url,
18 user: user
19 }) do
20 attachments = build_attachments(object)
21 scrubbed_content = Utils.scrub_html_and_truncate(object)
22
23 [
24 {:meta,
25 [
26 property: "og:title",
27 content: Utils.user_name_string(user)
28 ], []},
29 {:meta, [property: "og:url", content: url], []},
30 {:meta,
31 [
32 property: "og:description",
33 content: scrubbed_content
34 ], []},
35 {:meta, [property: "og:type", content: "website"], []}
36 ] ++
37 if attachments == [] or Metadata.activity_nsfw?(object) do
38 [
39 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
40 []},
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: "website"], []},
61 {:meta, [property: "og:image", content: Utils.attachment_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: Add additional properties to objects when we have the data available.
73 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
74 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
75 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
76 "audio" ->
77 [
78 {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
79 | acc
80 ]
81
82 "image" ->
83 [
84 {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
85 {:meta, [property: "og:image:alt", content: attachment["name"]], []}
86 | acc
87 ]
88
89 "video" ->
90 [
91 {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
92 | acc
93 ]
94
95 _ ->
96 acc
97 end
98 end)
99
100 acc ++ rendered_tags
101 end)
102 end
103
104 defp build_attachments(_), do: []
105 end