12c372d77f7d349d6c7f0fd3c54301cefc7028e2
[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 # TODO: Add additional properties to objects when we have the data available.
59 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
60 "audio" ->
61 [
62 {:meta, [property: "twitter:card", content: "player"], []},
63 {:meta, [property: "twitter:player:width", content: "480"], []},
64 {:meta, [property: "twitter:player:height", content: "80"], []},
65 {:meta, [property: "twitter:player", content: player_url(id)], []}
66 | acc
67 ]
68
69 "image" ->
70 [
71 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
72 {:meta,
73 [
74 property: "twitter:player",
75 content: Utils.attachment_url(url["href"])
76 ], []}
77 | acc
78 ]
79
80 # TODO: Need the true width and height values here or Twitter renders an iFrame with
81 # a bad aspect ratio
82 "video" ->
83 [
84 {:meta, [property: "twitter:card", content: "player"], []},
85 {:meta, [property: "twitter:player", content: player_url(id)], []},
86 {:meta, [property: "twitter:player:width", content: "480"], []},
87 {:meta, [property: "twitter:player:height", content: "480"], []},
88 {:meta, [property: "twitter:player:stream", content: url["href"]], []},
89 {:meta,
90 [property: "twitter:player:stream:content_type", content: url["mediaType"]], []}
91 | acc
92 ]
93
94 _ ->
95 acc
96 end
97 end)
98
99 acc ++ rendered_tags
100 end)
101 end
102
103 defp build_attachments(_id, _object), do: []
104
105 defp player_url(id) do
106 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
107 end
108 end