Formatting
[akkoma] / lib / pleroma / web / metadata / twitter_card.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Metadata.Providers.TwitterCard 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
13 @impl Provider
14 def build_tags(%{
15 object: object,
16 user: user
17 }) do
18 attachments = build_attachments(object)
19 scrubbed_content = Utils.scrub_html_and_truncate(object)
20 # Zero width space
21 content =
22 if scrubbed_content != "" and scrubbed_content != "\u200B" do
23 "“" <> scrubbed_content <> "”"
24 else
25 ""
26 end
27
28 [
29 {:meta,
30 [
31 property: "twitter:title",
32 content: Utils.user_name_string(user)
33 ], []},
34 {:meta,
35 [
36 property: "twitter:description",
37 content: content
38 ], []}
39 ] ++
40 if attachments == [] or Metadata.activity_nsfw?(object) do
41 [
42 {:meta,
43 [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []},
44 {:meta, [property: "twitter:card", content: "summary_large_image"], []}
45 ]
46 else
47 attachments
48 end
49 end
50
51 @impl Provider
52 def build_tags(%{user: user}) do
53 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
54 [
55 {:meta,
56 [
57 property: "twitter:title",
58 content: Utils.user_name_string(user)
59 ], []},
60 {:meta, [property: "twitter:description", content: truncated_bio], []},
61 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))],
62 []},
63 {:meta, [property: "twitter:card", content: "summary"], []}
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 media_type =
73 Enum.find(["image", "audio", "video"], fn media_type ->
74 String.starts_with?(url["mediaType"], media_type)
75 end)
76
77 # TODO: Add additional properties to objects when we have the data available.
78 case media_type do
79 "audio" ->
80 [
81 {:meta, [property: "twitter:card", content: "player"], []},
82 {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])],
83 []}
84 | acc
85 ]
86
87 "image" ->
88 [
89 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
90 {:meta,
91 [
92 property: "twitter:player",
93 content: Utils.attachment_url(url["href"])
94 ], []}
95 | acc
96 ]
97
98 # TODO: Need the true width and height values here or Twitter renders an iFrame with a bad aspect ratio
99 "video" ->
100 [
101 {:meta, [property: "twitter:card", content: "player"], []},
102 {:meta, [property: "twitter:player", content: Utils.attachment_url(url["href"])],
103 []},
104 {:meta, [property: "twitter:player:width", content: "1280"], []},
105 {:meta, [property: "twitter:player:height", content: "720"], []}
106 | acc
107 ]
108
109 _ ->
110 acc
111 end
112 end)
113
114 acc ++ rendered_tags
115 end)
116 end
117 end