From 4d5f15cd422abd3a2dce6f6022c75014c18c73cf Mon Sep 17 00:00:00 2001 From: rinpatch Date: Thu, 17 Jan 2019 11:00:02 +0300 Subject: [PATCH] Introduce optional unfurling of nsfw content --- docs/config.md | 3 ++- lib/pleroma/web/metadata.ex | 8 ++++++++ lib/pleroma/web/metadata/opengraph.ex | 3 ++- lib/pleroma/web/metadata/twitter_card.ex | 4 ++-- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/config.md b/docs/config.md index c2104a9e8..b9ad2aef7 100644 --- a/docs/config.md +++ b/docs/config.md @@ -212,6 +212,7 @@ curl "http://localhost:4000/api/pleroma/admin/invite_token?admin_token=somerando * `max_retries`: The maximum number of times a federation job is retried ## Pleroma.Web.Metadata -* `providers`: a list of metadata providers to enable. Providers avalible: +* `providers`: a list of metadata providers to enable. Providers availible: * Pleroma.Web.Metadata.Providers.OpenGraph * Pleroma.Web.Metadata.Providers.TwitterCard +* `unfurl_nsfw`: If set to `true` nsfw attachments will be shown in previews diff --git a/lib/pleroma/web/metadata.ex b/lib/pleroma/web/metadata.ex index 2164b0fe8..be3c384ae 100644 --- a/lib/pleroma/web/metadata.ex +++ b/lib/pleroma/web/metadata.ex @@ -28,4 +28,12 @@ def to_tag(data) do raise ArgumentError, message: "make_tag invalid args" end end + + def activity_nsfw?(%{data: %{"object" => %{"tag" => tags}}}) do + if(Pleroma.Config.get([__MODULE__, :unfurl_nsfw], false) == false) do + Enum.any?(tags, fn tag -> tag == "nsfw" end) + else + false + end + end end diff --git a/lib/pleroma/web/metadata/opengraph.ex b/lib/pleroma/web/metadata/opengraph.ex index a48788969..2f27a5300 100644 --- a/lib/pleroma/web/metadata/opengraph.ex +++ b/lib/pleroma/web/metadata/opengraph.ex @@ -3,6 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Metadata.Providers.OpenGraph do alias Pleroma.Web.Metadata.Providers.Provider + alias Pleroma.Web.Metadata alias Pleroma.{HTML, Formatter, User} alias Pleroma.Web.MediaProxy @@ -32,7 +33,7 @@ def build_tags(%{activity: %{data: %{"object" => %{"id" => object_id}}} = activi ], []}, {:meta, [property: "og:type", content: "website"], []} ] ++ - if attachments == [] do + if attachments == [] or Metadata.activity_nsfw?(activity) do [ {:meta, [property: "og:image", content: attachment_url(User.avatar_url(user))], []}, {:meta, [property: "og:image:width", content: 150], []}, diff --git a/lib/pleroma/web/metadata/twitter_card.ex b/lib/pleroma/web/metadata/twitter_card.ex index 853776611..9a1245e59 100644 --- a/lib/pleroma/web/metadata/twitter_card.ex +++ b/lib/pleroma/web/metadata/twitter_card.ex @@ -3,13 +3,13 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.Metadata.Providers.TwitterCard do alias Pleroma.Web.Metadata.Providers.Provider + alias Pleroma.Web.Metadata @behaviour Provider @impl Provider def build_tags(%{activity: activity}) do - if Enum.any?(activity.data["object"]["tag"], fn tag -> tag == "nsfw" end) or - activity.data["object"]["attachment"] == [] do + if Metadata.activity_nsfw?(activity) or activity.data["object"]["attachment"] == [] do build_tags(nil) else case find_first_acceptable_media_type(activity) do