Merge branch 'release/2.1.1' into 'stable'
[akkoma] / lib / pleroma / web / metadata / opengraph.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 # Zero width space
23 content =
24 if scrubbed_content != "" and scrubbed_content != "\u200B" do
25 ": “" <> scrubbed_content <> "”"
26 else
27 ""
28 end
29
30 # Most previews only show og:title which is inconvenient. Instagram
31 # hacks this by putting the description in the title and making the
32 # description longer prefixed by how many likes and shares the post
33 # has. Here we use the descriptive nickname in the title, and expand
34 # the full account & nickname in the description. We also use the cute^Wevil
35 # smart quotes around the status text like Instagram, too.
36 [
37 {:meta,
38 [
39 property: "og:title",
40 content: "#{user.name}" <> content
41 ], []},
42 {:meta, [property: "og:url", content: url], []},
43 {:meta,
44 [
45 property: "og:description",
46 content: "#{Utils.user_name_string(user)}" <> content
47 ], []},
48 {:meta, [property: "og:type", content: "website"], []}
49 ] ++
50 if attachments == [] or Metadata.activity_nsfw?(object) do
51 [
52 {:meta, [property: "og:image", content: Utils.attachment_url(User.avatar_url(user))],
53 []},
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 = Utils.scrub_html_and_truncate(user.bio || "") do
65 [
66 {:meta,
67 [
68 property: "og:title",
69 content: Utils.user_name_string(user)
70 ], []},
71 {:meta, [property: "og:url", content: user.uri || user.ap_id], []},
72 {:meta, [property: "og:description", content: truncated_bio], []},
73 {:meta, [property: "og:type", content: "website"], []},
74 {:meta, [property: "og:image", content: Utils.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 # TODO: Add additional properties to objects when we have the data available.
86 # Also, Whatsapp only wants JPEG or PNGs. It seems that if we add a second og:image
87 # object when a Video or GIF is attached it will display that in Whatsapp Rich Preview.
88 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
89 "audio" ->
90 [
91 {:meta, [property: "og:audio", content: Utils.attachment_url(url["href"])], []}
92 | acc
93 ]
94
95 "image" ->
96 [
97 {:meta, [property: "og:image", content: Utils.attachment_url(url["href"])], []},
98 {:meta, [property: "og:image:width", content: 150], []},
99 {:meta, [property: "og:image:height", content: 150], []}
100 | acc
101 ]
102
103 "video" ->
104 [
105 {:meta, [property: "og:video", content: Utils.attachment_url(url["href"])], []}
106 | acc
107 ]
108
109 _ ->
110 acc
111 end
112 end)
113
114 acc ++ rendered_tags
115 end)
116 end
117
118 defp build_attachments(_), do: []
119 end