From 0ef0ae35abf7c1f1016175bd446436f9e5dd8fc2 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:12:23 +0100 Subject: [PATCH 1/9] added optional delist feature --- config/config.exs | 4 ++- .../web/activity_pub/mrf/hellthread_policy.ex | 30 +++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/config/config.exs b/config/config.exs index d0d53a64a..8b42a5351 100644 --- a/config/config.exs +++ b/config/config.exs @@ -227,7 +227,9 @@ config :pleroma, :mrf_rejectnonpublic, allow_followersonly: false, allow_direct: false -config :pleroma, :mrf_hellthread, threshold: 10 +config :pleroma, :mrf_hellthread, + delist_threshold: 5, + reject_threshold: 10 config :pleroma, :mrf_simple, media_removal: [], 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 From 10130fa7d6a2dca4250ada1144fcfcfe75c26f45 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:27:28 +0100 Subject: [PATCH 2/9] made toggleable, added docs --- docs/config.md | 3 ++- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/config.md b/docs/config.md index c14746d93..a1fd8e3f4 100644 --- a/docs/config.md +++ b/docs/config.md @@ -148,7 +148,8 @@ This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:i * `allow_direct`: whether to allow direct messages ## :mrf_hellthread -* `threshold`: Number of mentioned users after which the message gets discarded as spam +* `delist_threshold`: Number of mentioned users after which the message gets delisted. Set to 0 to disable. +* `reject_threshold`: Number of mentioned users after which the messaged gets rejected. Set to 0 to disable. ## :media_proxy * `enabled`: Enables proxying of remote media to the instance’s proxy 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 From 531507a635917e15e28a72a58ab0f977eefed571 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 20:45:32 +0100 Subject: [PATCH 3/9] fixed things --- .../web/activity_pub/mrf/hellthread_policy.ex | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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 From e10cda7541f5d76a32d0bf27d90a51c5fc8e7fcf Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 22:46:06 +0100 Subject: [PATCH 4/9] implemented tweaks --- docs/config.md | 2 +- lib/pleroma/config/deprecation_warnings.ex | 7 +++++ .../web/activity_pub/mrf/hellthread_policy.ex | 31 ++++++++++++------- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/docs/config.md b/docs/config.md index a1fd8e3f4..ed253e906 100644 --- a/docs/config.md +++ b/docs/config.md @@ -148,7 +148,7 @@ This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:i * `allow_direct`: whether to allow direct messages ## :mrf_hellthread -* `delist_threshold`: Number of mentioned users after which the message gets delisted. Set to 0 to disable. +* `delist_threshold`: Number of mentioned users after which the message gets delisted (the message can still be seen, but it will not show up in public timelines and mentioned users won't get notifications about it). Set to 0 to disable. * `reject_threshold`: Number of mentioned users after which the messaged gets rejected. Set to 0 to disable. ## :media_proxy 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} From 583c4e0f17206d77174e1eaa84bb68fc5a57f196 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Sun, 3 Feb 2019 23:56:20 +0100 Subject: [PATCH 5/9] more tweaks, fixed silly mistakes... --- .../web/activity_pub/mrf/hellthread_policy.ex | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) 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 From 63a4f4b7be14753d0e78f8a445351b7dae008254 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 10:23:07 +0100 Subject: [PATCH 6/9] fixed Map,put arguments, updated nomenclature --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 3cf046babb706c9539a0e4e799c578a4ed207c24 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 10:23:43 +0100 Subject: [PATCH 7/9] actually commited the changes --- .../web/activity_pub/mrf/hellthread_policy.ex | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 From 4430641349a6f322be52b787cc817c0ff691b836 Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 12:09:00 +0100 Subject: [PATCH 8/9] squished a bug --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 58262a8b8a759ecb69663a92a56f65b6e16db6ea Mon Sep 17 00:00:00 2001 From: Karen Konou Date: Mon, 4 Feb 2019 11:22:25 +0000 Subject: [PATCH 9/9] removed a debug thingy --- lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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