Twittercard metadata for images should also include dimensions if available
[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.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(%{activity_id: id, object: object, user: user}) do
17 attachments = build_attachments(id, object)
18 scrubbed_content = Utils.scrub_html_and_truncate(object)
19
20 [
21 title_tag(user),
22 {:meta, [property: "twitter:description", content: scrubbed_content], []}
23 ] ++
24 if attachments == [] or Metadata.activity_nsfw?(object) do
25 [
26 image_tag(user),
27 {:meta, [property: "twitter:card", content: "summary"], []}
28 ]
29 else
30 attachments
31 end
32 end
33
34 @impl Provider
35 def build_tags(%{user: user}) do
36 with truncated_bio = Utils.scrub_html_and_truncate(user.bio) do
37 [
38 title_tag(user),
39 {:meta, [property: "twitter:description", content: truncated_bio], []},
40 image_tag(user),
41 {:meta, [property: "twitter:card", content: "summary"], []}
42 ]
43 end
44 end
45
46 defp title_tag(user) do
47 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
48 end
49
50 def image_tag(user) do
51 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
52 end
53
54 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
55 Enum.reduce(attachments, [], fn attachment, acc ->
56 rendered_tags =
57 Enum.reduce(attachment["url"], [], fn url, acc ->
58 height = url["height"] || 480
59 width = url["width"] || 480
60
61 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
62 "audio" ->
63 [
64 {:meta, [property: "twitter:card", content: "player"], []},
65 {:meta, [property: "twitter:player:width", content: "480"], []},
66 {:meta, [property: "twitter:player:height", content: "80"], []},
67 {:meta, [property: "twitter:player", content: player_url(id)], []}
68 | acc
69 ]
70
71 "image" ->
72 [
73 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
74 {:meta,
75 [
76 property: "twitter:player",
77 content: Utils.attachment_url(url["href"])
78 ], []},
79 {:meta, [property: "twitter:player:width", content: "#{width}"], []},
80 {:meta, [property: "twitter:player:height", content: "#{height}"], []}
81 | acc
82 ]
83
84 "video" ->
85 [
86 {:meta, [property: "twitter:card", content: "player"], []},
87 {:meta, [property: "twitter:player", content: player_url(id)], []},
88 {:meta, [property: "twitter:player:width", content: "#{width}"], []},
89 {:meta, [property: "twitter:player:height", content: "#{height}"], []},
90 {:meta, [property: "twitter:player:stream", content: url["href"]], []},
91 {:meta,
92 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
93 | acc
94 ]
95
96 _ ->
97 acc
98 end
99 end)
100
101 acc ++ rendered_tags
102 end)
103 end
104
105 defp build_attachments(_id, _object), do: []
106
107 defp player_url(id) do
108 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
109 end
110 end