a952d0a05a71db56375d70bf66a176235bc0db39
[akkoma] / lib / pleroma / web / metadata / providers / twitter_card.ex
1 # Pleroma: A lightweight social networking server
2
3 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
4 # SPDX-License-Identifier: AGPL-3.0-only
5
6 defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
7 alias Pleroma.User
8 alias Pleroma.Web.MediaProxy
9 alias Pleroma.Web.Metadata
10 alias Pleroma.Web.Metadata.Providers.Provider
11 alias Pleroma.Web.Metadata.Utils
12
13 @behaviour Provider
14 @media_types ["image", "audio", "video"]
15
16 @impl Provider
17 def build_tags(%{activity_id: id, object: object, user: user}) do
18 attachments = build_attachments(id, object)
19 scrubbed_content = Utils.scrub_html_and_truncate(object)
20
21 [
22 title_tag(user),
23 {:meta, [property: "twitter:description", content: scrubbed_content], []}
24 ] ++
25 if attachments == [] or Metadata.activity_nsfw?(object) do
26 [
27 image_tag(user),
28 {:meta, [property: "twitter:card", content: "summary"], []}
29 ]
30 else
31 attachments
32 end
33 end
34
35 @impl Provider
36 def build_tags(%{user: user}) do
37 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
38 [
39 title_tag(user),
40 {:meta, [property: "twitter:description", content: truncated_bio], []},
41 image_tag(user),
42 {:meta, [property: "twitter:card", content: "summary"], []}
43 ]
44 end
45 end
46
47 defp title_tag(user) do
48 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
49 end
50
51 def image_tag(user) do
52 {:meta, [property: "twitter:image", content: MediaProxy.preview_url(User.avatar_url(user))],
53 []}
54 end
55
56 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
57 Enum.reduce(attachments, [], fn attachment, acc ->
58 rendered_tags =
59 Enum.reduce(attachment["url"], [], fn url, acc ->
60 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
61 "audio" ->
62 [
63 {:meta, [property: "twitter:card", content: "player"], []},
64 {:meta, [property: "twitter:player:width", content: "480"], []},
65 {:meta, [property: "twitter:player:height", content: "80"], []},
66 {:meta, [property: "twitter:player", content: player_url(id)], []}
67 | acc
68 ]
69
70 # Not using preview_url for this. It saves bandwidth, but the image dimensions will be wrong.
71 # We generate it on the fly and have no way to capture or analyze the image to get the dimensions.
72 # This can be an issue for apps/FEs rendering images in timelines too, but you can get clever with
73 # the aspect ratio metadata as a workaround.
74 "image" ->
75 [
76 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
77 {:meta,
78 [
79 property: "twitter:player",
80 content: MediaProxy.url(url["href"])
81 ], []}
82 | acc
83 ]
84 |> maybe_add_dimensions(url)
85
86 "video" ->
87 # fallback to old placeholder values
88 height = url["height"] || 480
89 width = url["width"] || 480
90
91 [
92 {:meta, [property: "twitter:card", content: "player"], []},
93 {:meta, [property: "twitter:player", content: player_url(id)], []},
94 {:meta, [property: "twitter:player:width", content: "#{width}"], []},
95 {:meta, [property: "twitter:player:height", content: "#{height}"], []},
96 {:meta, [property: "twitter:player:stream", content: MediaProxy.url(url["href"])], []},
97 {:meta,
98 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
99 | acc
100 ]
101
102 _ ->
103 acc
104 end
105 end)
106
107 acc ++ rendered_tags
108 end)
109 end
110
111 defp build_attachments(_id, _object), do: []
112
113 defp player_url(id) do
114 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
115 end
116
117 # Videos have problems without dimensions, but we used to not provide WxH for images.
118 # A default (read: incorrect) fallback for images is likely to cause rendering bugs.
119 defp maybe_add_dimensions(metadata, url) do
120 cond do
121 !is_nil(url["height"]) && !is_nil(url["width"]) ->
122 metadata ++
123 [
124 {:meta, [property: "twitter:player:width", content: "#{url["width"]}"], []},
125 {:meta, [property: "twitter:player:height", content: "#{url["height"]}"], []}
126 ]
127
128 true ->
129 metadata
130 end
131 end
132 end