Introduce optional unfurling of nsfw content
[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 defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
5 alias Pleroma.Web.Metadata.Providers.Provider
6 alias Pleroma.Web.Metadata
7
8 @behaviour Provider
9
10 @impl Provider
11 def build_tags(%{activity: activity}) do
12 if Metadata.activity_nsfw?(activity) or activity.data["object"]["attachment"] == [] do
13 build_tags(nil)
14 else
15 case find_first_acceptable_media_type(activity) do
16 "image" ->
17 [{:meta, [property: "twitter:card", content: "summary_large_image"], []}]
18
19 "audio" ->
20 [{:meta, [property: "twitter:card", content: "player"], []}]
21
22 "video" ->
23 [{:meta, [property: "twitter:card", content: "player"], []}]
24
25 _ ->
26 build_tags(nil)
27 end
28 end
29 end
30
31 @impl Provider
32 def build_tags(_) do
33 [{:meta, [property: "twitter:card", content: "summary"], []}]
34 end
35
36 def find_first_acceptable_media_type(%{data: %{"object" => %{"attachment" => attachment}}}) do
37 Enum.find_value(attachment, fn attachment ->
38 Enum.find_value(attachment["url"], fn url ->
39 Enum.find(["image", "audio", "video"], fn media_type ->
40 String.starts_with?(url["mediaType"], media_type)
41 end)
42 end)
43 end)
44 end
45 end