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