join us now and share the software~. Also tests
[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
7 @behaviour Provider
8
9 @impl Provider
10 def build_tags(%{activity: activity}) do
11 if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or
12 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