Merge branch 'develop' into feature/tag_feed
[akkoma] / lib / pleroma / web / metadata / twitter_card.ex
1 # Pleroma: A lightweight social networking server
2
3 # Copyright © 2017-2019 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 # Zero width space
20 content =
21 if scrubbed_content != "" and scrubbed_content != "\u200B" do
22 "“" <> scrubbed_content <> "”"
23 else
24 ""
25 end
26
27 [
28 title_tag(user),
29 {:meta, [property: "twitter:description", content: content], []}
30 ] ++
31 if attachments == [] or Metadata.activity_nsfw?(object) do
32 [
33 image_tag(user),
34 {:meta, [property: "twitter:card", content: "summary"], []}
35 ]
36 else
37 attachments
38 end
39 end
40
41 @impl Provider
42 def build_tags(%{user: user}) do
43 with truncated_bio = Utils.scrub_html_and_truncate(user.bio || "") do
44 [
45 title_tag(user),
46 {:meta, [property: "twitter:description", content: truncated_bio], []},
47 image_tag(user),
48 {:meta, [property: "twitter:card", content: "summary"], []}
49 ]
50 end
51 end
52
53 defp title_tag(user) do
54 {:meta, [property: "twitter:title", content: Utils.user_name_string(user)], []}
55 end
56
57 def image_tag(user) do
58 {:meta, [property: "twitter:image", content: Utils.attachment_url(User.avatar_url(user))], []}
59 end
60
61 defp build_attachments(id, %{data: %{"attachment" => attachments}}) do
62 Enum.reduce(attachments, [], fn attachment, acc ->
63 rendered_tags =
64 Enum.reduce(attachment["url"], [], fn url, acc ->
65 # TODO: Add additional properties to objects when we have the data available.
66 case Utils.fetch_media_type(@media_types, url["mediaType"]) do
67 "audio" ->
68 [
69 {:meta, [property: "twitter:card", content: "player"], []},
70 {:meta, [property: "twitter:player:width", content: "480"], []},
71 {:meta, [property: "twitter:player:height", content: "80"], []},
72 {:meta, [property: "twitter:player", content: player_url(id)], []}
73 | acc
74 ]
75
76 "image" ->
77 [
78 {:meta, [property: "twitter:card", content: "summary_large_image"], []},
79 {:meta,
80 [
81 property: "twitter:player",
82 content: Utils.attachment_url(url["href"])
83 ], []}
84 | acc
85 ]
86
87 # TODO: Need the true width and height values here or Twitter renders an iFrame with
88 # a bad aspect ratio
89 "video" ->
90 [
91 {:meta, [property: "twitter:card", content: "player"], []},
92 {:meta, [property: "twitter:player", content: player_url(id)], []},
93 {:meta, [property: "twitter:player:width", content: "480"], []},
94 {:meta, [property: "twitter:player:height", content: "480"], []}
95 | acc
96 ]
97
98 _ ->
99 acc
100 end
101 end)
102
103 acc ++ rendered_tags
104 end)
105 end
106
107 defp build_attachments(_id, _object), do: []
108
109 defp player_url(id) do
110 Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice_player, id)
111 end
112 end