From 8a1df182cf836eafc2558be32cf58ad07839f46a Mon Sep 17 00:00:00 2001 From: scarlett Date: Tue, 4 Dec 2018 21:39:18 +0000 Subject: [PATCH 1/4] Add a MRF Policy for appending re: to identical subjects in replies. --- config/config.md | 1 + .../activity_pub/mrf/ensure_re_prepended.ex | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex diff --git a/config/config.md b/config/config.md index 47e838dd6..d90d18566 100644 --- a/config/config.md +++ b/config/config.md @@ -39,6 +39,7 @@ Note: `strip_exif` has been replaced by `Pleroma.Upload.Filter.Mogrify`. * `Pleroma.Web.ActivityPub.MRF.DropPolicy`: Drops all activities. It generally doesn’t makes sense to use in production * `Pleroma.Web.ActivityPub.MRF.SimplePolicy`: Restrict the visibility of activities from certains instances (See ``:mrf_simple`` section) * `Pleroma.Web.ActivityPub.MRF.RejectNonPublic`: Drops posts with non-public visibility settings (See ``:mrf_rejectnonpublic`` section) + * `Pleroma.Web.ActivityPub.MRF.EnsureRePrepended`: Rewrites posts to ensure that replies to posts with subjects do not have an identical subject and instead begin with re:. * `public`: Makes the client API in authentificated mode-only except for user-profiles. Useful for disabling the Local Timeline and The Whole Known Network. * `quarantined_instances`: List of ActivityPub instances where private(DMs, followers-only) activities will not be send. * `managed_config`: Whenether the config for pleroma-fe is configured in this config or in ``static/config.json`` diff --git a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex new file mode 100644 index 000000000..3f216010e --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex @@ -0,0 +1,37 @@ +defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do + alias Pleroma.Activity + + @behaviour Pleroma.Web.ActivityPub.MRF + + def filter_by_summary( + %{"summary" => parent_summary} = parent, + %{"summary" => child_summary} = child + ) + when not is_nil(child_summary) and child_summary == parent_summary and + byte_size(child_summary) > 1 do + if not String.starts_with?(child_summary, "re:") do + Map.put(child, "summary", "re: " <> child_summary) + else + child + end + end + + def filter_by_summary(parent, child), do: child + + def filter(%{"type" => activity_type} = object) when activity_type == "Create" do + child = object["object"] + in_reply_to = Activity.get_create_activity_by_object_ap_id(child["inReplyTo"]) + + child = + if(in_reply_to, + do: filter_by_summary(in_reply_to.data["object"], child), + else: child + ) + + object = Map.put(object, "object", child) + + {:ok, object} + end + + def filter(object), do: {:ok, object} +end From 6a6aaa0e1aade45bc2e3b68ee2c4686cc80f46cd Mon Sep 17 00:00:00 2001 From: scarlett Date: Thu, 6 Dec 2018 11:37:29 +0000 Subject: [PATCH 2/4] Use object.normalize. --- lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex index 3f216010e..06fafb3ee 100644 --- a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex +++ b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex @@ -1,5 +1,5 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do - alias Pleroma.Activity + alias Pleroma.Object @behaviour Pleroma.Web.ActivityPub.MRF @@ -20,11 +20,11 @@ def filter_by_summary(parent, child), do: child def filter(%{"type" => activity_type} = object) when activity_type == "Create" do child = object["object"] - in_reply_to = Activity.get_create_activity_by_object_ap_id(child["inReplyTo"]) + in_reply_to = Object.normalize(child["inReplyTo"]) child = if(in_reply_to, - do: filter_by_summary(in_reply_to.data["object"], child), + do: filter_by_summary(in_reply_to.data, child), else: child ) From 3dff61ebec4f4b216903d79261ec5cac80e70a08 Mon Sep 17 00:00:00 2001 From: scarlett Date: Thu, 6 Dec 2018 13:48:12 +0000 Subject: [PATCH 3/4] Harden re: detection. --- lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex index 06fafb3ee..a3b9c4616 100644 --- a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex +++ b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex @@ -3,13 +3,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do @behaviour Pleroma.Web.ActivityPub.MRF + @have_re Regex.compile!("^re:[[:space:]]*", [:caseless]) def filter_by_summary( %{"summary" => parent_summary} = parent, %{"summary" => child_summary} = child ) - when not is_nil(child_summary) and child_summary == parent_summary and - byte_size(child_summary) > 1 do - if not String.starts_with?(child_summary, "re:") do + when not is_nil(child_summary) and byte_size(child_summary) > 0 and + not is_nil(parent_summary) and byte_size(parent_summary) > 0 do + if (child_summary == parent_summary and not Regex.match?(@have_re, child_summary)) or + (Regex.match?(@have_re, parent_summary) && + Regex.replace(@have_re, parent_summary, "") == child_summary) do Map.put(child, "summary", "re: " <> child_summary) else child From d27e3f269f5e93efd91397ada72656120be3db41 Mon Sep 17 00:00:00 2001 From: scarlett Date: Thu, 6 Dec 2018 14:00:41 +0000 Subject: [PATCH 4/4] Rename regular expression 'have_re' to 'reply_prefix'. --- lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex index a3b9c4616..c8c74ede6 100644 --- a/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex +++ b/lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex @@ -3,16 +3,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do @behaviour Pleroma.Web.ActivityPub.MRF - @have_re Regex.compile!("^re:[[:space:]]*", [:caseless]) + @reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless]) def filter_by_summary( %{"summary" => parent_summary} = parent, %{"summary" => child_summary} = child ) when not is_nil(child_summary) and byte_size(child_summary) > 0 and not is_nil(parent_summary) and byte_size(parent_summary) > 0 do - if (child_summary == parent_summary and not Regex.match?(@have_re, child_summary)) or - (Regex.match?(@have_re, parent_summary) && - Regex.replace(@have_re, parent_summary, "") == child_summary) do + if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or + (Regex.match?(@reply_prefix, parent_summary) && + Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do Map.put(child, "summary", "re: " <> child_summary) else child