Add behaviours to TwitterCard, remove some dumb stuff in Formatter.truncate
[akkoma] / lib / pleroma / web / metadata / twitter_card.ex
1 defmodule Pleroma.Web.Metadata.Providers.TwitterCard do
2 alias Pleroma.Web.Metadata.Providers.Provider
3
4 @behaviour Provider
5
6 @impl Provider
7 def build_tags(%{activity: activity}) do
8 if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or
9 activity.data["object"]["attachment"] == [] do
10 build_tags(nil)
11 else
12 case find_first_acceptable_media_type(activity) do
13 "image" ->
14 [{:meta, [property: "twitter:card", content: "summary_large_image"], []}]
15
16 "audio" ->
17 [{:meta, [property: "twitter:card", content: "player"], []}]
18
19 "video" ->
20 [{:meta, [property: "twitter:card", content: "player"], []}]
21
22 _ ->
23 build_tags(nil)
24 end
25 end
26 end
27
28 @impl Provider
29 def build_tags(_) do
30 [{:meta, [property: "twitter:card", content: "summary"], []}]
31 end
32
33 def find_first_acceptable_media_type(%{data: %{"object" => %{"attachment" => attachment}}}) do
34 Enum.find_value(attachment, fn attachment ->
35 Enum.find_value(attachment["url"], fn url ->
36 Enum.find(["image", "audio", "video"], fn media_type ->
37 String.starts_with?(url["mediaType"], media_type)
38 end)
39 end)
40 end)
41 end
42 end