From 0ef0ae35abf7c1f1016175bd446436f9e5dd8fc2 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:12:23 +0100 Subject: added optional delist feature --- .../web/activity_pub/mrf/hellthread_policy.ex | 30 ++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index a3f516ae7..0b9caeb11 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -3,17 +3,37 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do + alias Pleroma.User @behaviour Pleroma.Web.ActivityPub.MRF @impl true def filter(%{"type" => "Create"} = object) do - threshold = Pleroma.Config.get([:mrf_hellthread, :threshold]) + delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold]) + reject_threshold = Pleroma.Config.get([:mrf_hellthread, :reject_threshold]) recipients = (object["to"] || []) ++ (object["cc"] || []) - if length(recipients) > threshold do - {:reject, nil} - else - {:ok, object} + cond do + length(recipients) > reject_threshold -> + {:reject, nil} + + length(recipients) > delist_threshold -> + if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or + Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do + object + |> Kernel.update_in(["object", "to"], [ + User.get_cached_by_ap_id(object["actor"].follower_address) + ]) + |> Kernel.update_in(["object", "cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + |> Kernel.update_in(["to"], [ + User.get_cached_by_ap_id(object["actor"].follower_address) + ]) + |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + else + {:ok, object} + end + + true -> + {:ok, object} end end -- cgit v1.2.3 From 10130fa7d6a2dca4250ada1144fcfcfe75c26f45 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:27:28 +0100 Subject: made toggleable, added docs --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index 0b9caeb11..53588b264 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -13,10 +13,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do recipients = (object["to"] || []) ++ (object["cc"] || []) cond do - length(recipients) > reject_threshold -> + length(recipients) > reject_threshold and reject_threshold != 0 -> {:reject, nil} - length(recipients) > delist_threshold -> + length(recipients) > delist_threshold and delist_threshold != 0 -> if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do object -- cgit v1.2.3 From 531507a635917e15e28a72a58ab0f977eefed571 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:45:32 +0100 Subject: fixed things --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index 53588b264..d95424493 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -19,15 +19,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do length(recipients) > delist_threshold and delist_threshold != 0 -> if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do + follower_collection = User.get_by_ap_id(object["actor"].follower_address) + object - |> Kernel.update_in(["object", "to"], [ - User.get_cached_by_ap_id(object["actor"].follower_address) - ]) + |> Kernel.update_in(["object", "to"], [follower_collection]) |> Kernel.update_in(["object", "cc"], ["https://www.w3.org/ns/activitystreams#Public"]) - |> Kernel.update_in(["to"], [ - User.get_cached_by_ap_id(object["actor"].follower_address) - ]) + |> Kernel.update_in(["to"], [follower_collection]) |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + + {:ok, object} else {:ok, object} end -- cgit v1.2.3 From e10cda7541f5d76a32d0bf27d90a51c5fc8e7fcf Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 22:46:06 +0100 Subject: implemented tweaks --- lib/pleroma/config/deprecation_warnings.ex | 7 +++++ .../web/activity_pub/mrf/hellthread_policy.ex | 31 +++++++++++++--------- 2 files changed, 26 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/config/deprecation_warnings.ex b/lib/pleroma/config/deprecation_warnings.ex index dc50682ee..0eb1833aa 100644 --- a/lib/pleroma/config/deprecation_warnings.ex +++ b/lib/pleroma/config/deprecation_warnings.ex @@ -12,6 +12,13 @@ defmodule Pleroma.Config.DeprecationWarnings do You are using the old configuration mechanism for the frontend. Please check config.md. """) end + + if Pleroma.Config.get(:mrf_hellthread, :threshold) do + Logger.warn(""" + !!!DEPRECATION WARNING!!! + You are using the old configuration mechanism for the hellthread filter. Please check config.md. + """) + end end def warn do diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index d95424493..cd9f9b1c4 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -6,27 +6,34 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do alias Pleroma.User @behaviour Pleroma.Web.ActivityPub.MRF + defp delist_message(object) do + follower_collection = User.get_by_ap_id(object["actor"].follower_address) + + object + |> Kernel.update_in(["to"], [follower_collection]) + |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + end + @impl true def filter(%{"type" => "Create"} = object) do delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold]) - reject_threshold = Pleroma.Config.get([:mrf_hellthread, :reject_threshold]) + + reject_threshold = + Pleroma.Config.get( + [:mrf_hellthread, :reject_threshold], + Pleroma.Config.get([:mrf_hellthread, :threshold]) + ) + recipients = (object["to"] || []) ++ (object["cc"] || []) cond do - length(recipients) > reject_threshold and reject_threshold != 0 -> + length(recipients) > reject_threshold and reject_threshold > 0 -> {:reject, nil} - length(recipients) > delist_threshold and delist_threshold != 0 -> + length(recipients) > delist_threshold and delist_threshold > 0 -> if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or - Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do - follower_collection = User.get_by_ap_id(object["actor"].follower_address) - - object - |> Kernel.update_in(["object", "to"], [follower_collection]) - |> Kernel.update_in(["object", "cc"], ["https://www.w3.org/ns/activitystreams#Public"]) - |> Kernel.update_in(["to"], [follower_collection]) - |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) - + Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") do + delist_message(object) {:ok, object} else {:ok, object} -- cgit v1.2.3 From 583c4e0f17206d77174e1eaa84bb68fc5a57f196 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 23:56:20 +0100 Subject: more tweaks, fixed silly mistakes... --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index cd9f9b1c4..1c2de555f 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -6,12 +6,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do alias Pleroma.User @behaviour Pleroma.Web.ActivityPub.MRF - defp delist_message(object) do - follower_collection = User.get_by_ap_id(object["actor"].follower_address) + defp delist_message(message) do + follower_collection = User.get_by_ap_id(message["actor"].follower_address) - object - |> Kernel.update_in(["to"], [follower_collection]) - |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + message + |> Map.put(["to"], [follower_collection]) + |> Map.put(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) end @impl true @@ -32,9 +32,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do length(recipients) > delist_threshold and delist_threshold > 0 -> if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or - Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") do - delist_message(object) - {:ok, object} + Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do + {:ok, delist_message(object)} else {:ok, object} end -- cgit v1.2.3 From 63a4f4b7be14753d0e78f8a445351b7dae008254 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 10:23:07 +0100 Subject: fixed Map,put arguments, updated nomenclature --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index 1c2de555f..667f5da03 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -10,8 +10,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do follower_collection = User.get_by_ap_id(message["actor"].follower_address) message - |> Map.put(["to"], [follower_collection]) - |> Map.put(["cc"], ["https://www.w3.org/ns/activitystreams#Public"]) + |> Map.put("to", [follower_collection]) + |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"]) end @impl true -- cgit v1.2.3 From 3cf046babb706c9539a0e4e799c578a4ed207c24 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 10:23:43 +0100 Subject: actually commited the changes --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index 667f5da03..e78c9b5f8 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do end @impl true - def filter(%{"type" => "Create"} = object) do + def filter(%{"type" => "Create"} = message) do delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold]) reject_threshold = @@ -24,25 +24,25 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do Pleroma.Config.get([:mrf_hellthread, :threshold]) ) - recipients = (object["to"] || []) ++ (object["cc"] || []) + recipients = (message["to"] || []) ++ (message["cc"] || []) cond do length(recipients) > reject_threshold and reject_threshold > 0 -> {:reject, nil} length(recipients) > delist_threshold and delist_threshold > 0 -> - if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or - Enum.member?(object["cc"], "https://www.w3.org/ns/activitystreams#Public") do - {:ok, delist_message(object)} + if Enum.member?(message["to"], "https://www.w3.org/ns/activitystreams#Public") or + Enum.member?(message["cc"], "https://www.w3.org/ns/activitystreams#Public") do + {:ok, delist_message(message)} else - {:ok, object} + {:ok, message} end true -> - {:ok, object} + {:ok, message} end end @impl true - def filter(object), do: {:ok, object} + def filter(message), do: {:ok, message} end -- cgit v1.2.3 From 4430641349a6f322be52b787cc817c0ff691b836 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 12:09:00 +0100 Subject: squished a bug --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index e78c9b5f8..dd0d6dd5f 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -7,7 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do @behaviour Pleroma.Web.ActivityPub.MRF defp delist_message(message) do - follower_collection = User.get_by_ap_id(message["actor"].follower_address) + follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address message |> Map.put("to", [follower_collection]) @@ -44,5 +44,5 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do end @impl true - def filter(message), do: {:ok, message} + def filter(message), do: {:ok_notcreate, message} end -- cgit v1.2.3 From 58262a8b8a759ecb69663a92a56f65b6e16db6ea Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 11:22:25 +0000 Subject: removed a debug thingy --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index dd0d6dd5f..4c6e612b2 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -44,5 +44,5 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do end @impl true - def filter(message), do: {:ok_notcreate, message} + def filter(message), do: {:ok, message} end -- cgit v1.2.3