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