diff options
author | rinpatch <rinpatch@sdf.org> | 2019-01-15 23:00:22 +0300 |
---|---|---|
committer | rinpatch <rinpatch@sdf.org> | 2019-01-15 23:00:22 +0300 |
commit | 2e630bea0da6452d3342a335da3ca642dc61c1b3 (patch) | |
tree | 3b7f32adabfb78c129a2f88646d7e2b9af10a648 | |
parent | 70f140681f90d92169b9774ad048100a003fd5a0 (diff) | |
download | pleroma-2e630bea0da6452d3342a335da3ca642dc61c1b3.tar.gz |
Add twitter card, filter nsfw
-rw-r--r-- | lib/pleroma/web/metadata/opengraph.ex | 8 | ||||
-rw-r--r-- | lib/pleroma/web/metadata/twitter_card.ex | 36 |
2 files changed, 39 insertions, 5 deletions
diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index 8046c13ba..fcc960311 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -17,10 +17,9 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do content: user_name_string(user) ], []}, {:meta, [property: "og:url", content: activity.data["id"]], []}, - {:meta, [property: "og:description", content: truncated_content], []}, - {:meta, [property: "twitter:card", content: "summary"], []} + {:meta, [property: "og:description", content: truncated_content], []} ] ++ - if attachments == [] do + if attachments == [] or Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) do [ {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image:width", content: 120], []}, @@ -45,8 +44,7 @@ defmodule Pleroma.Web.Metadata.Providers.OpenGraph do {:meta, [property: "og:description", content: truncated_bio], []}, {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image:width", content: 120], []}, - {:meta, [property: "og:image:height", content: 120], []}, - {:meta, [property: "twitter:card", content: "summary"], []} + {:meta, [property: "og:image:height", content: 120], []} ] end end diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/twitter_card.ex new file mode 100644 index 000000000..6424f84b3 --- /dev/null +++ b/lib/pleroma/web/metadata/twitter_card.ex @@ -0,0 +1,36 @@ +defmodule Pleroma.Web.Metadata.Providers.TwitterCard do + def build_tags(%{activity: activity}) do + if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or + activity.data["object"]["attachment"] == [] do + build_tags(nil) + else + case find_first_acceptable_media_type(activity) do + "image" -> + [{:meta, [property: "twitter:card", content: "summary_large_image"], []}] + + "audio" -> + [{:meta, [property: "twitter:card", content: "player"], []}] + + "video" -> + [{:meta, [property: "twitter:card", content: "player"], []}] + + _ -> + build_tags(nil) + end + end + end + + def build_tags(_) do + [{:meta, [property: "twitter:card", content: "summary"], []}] + end + + def find_first_acceptable_media_type(%{data: %{"object" => %{"attachment" => attachment}}}) do + Enum.find_value(attachment, fn attachment -> + Enum.find_value(attachment["url"], fn url -> + Enum.find(["image", "audio", "video"], fn media_type -> + String.starts_with?(url["mediaType"], media_type) + end) + end) + end) + end +end |