diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 62c7a7b31..7bf501f77 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -415,6 +415,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do with flag_data <- make_flag_data(params, additional), {:ok, activity} <- insert(flag_data, local), {:ok, stripped_activity} <- strip_report_status_data(activity), + stripped_activity <- maybe_anonymize_reporter(stripped_activity), _ <- notify_and_stream(activity), :ok <- maybe_federate(stripped_activity) do diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 08c0d8e6a..f230730ab 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -875,15 +875,29 @@ defmodule Pleroma.Web.ActivityPub.Utils do {:ok, %{activity | data: new_data}} end - def maybe_anonymize_reporter(activity) do + def get_anonymized_reporter do with true <- Pleroma.Config.get([:activitypub, :anonymize_reporter]), nickname when is_binary(nickname) <- Pleroma.Config.get([:activitypub, :anonymize_reporter_local_nickname]), %User{ap_id: ap_id, local: true} <- User.get_cached_by_nickname(nickname) do + ap_id + else + _ -> nil + end + end + + def maybe_anonymize_reporter(%Activity{data: data} = activity) do + %Activity{activity | data: maybe_anonymize_reporter(data)} + end + + def maybe_anonymize_reporter(activity) do + ap_id = get_anonymized_reporter() + + if is_binary(ap_id) do activity |> Map.put("actor", ap_id) else - _ -> activity + activity end end diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index c16f081f6..a4030ff03 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -1707,6 +1707,41 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do assert_called(Utils.maybe_federate(%{activity | data: new_data})) end + test "anonymize reporter from Flag, before federating it, if configured so", + %{ + reporter: reporter, + context: context, + target_account: target_account, + reported_activity: reported_activity, + object_ap_id: object_ap_id, + content: content + } do + placeholder = insert(:user) + clear_config([:activitypub, :anonymize_reporter], true) + clear_config([:activitypub, :anonymize_reporter_local_nickname], placeholder.nickname) + # Workaround "could not checkout connection" problem + # https://elixirforum.com/t/ecto-timeout-errors-when-wrapping-into-cachex-calls-in-tests/19078/11 + %User{} = User.get_cached_by_nickname(placeholder.nickname) + + with_mock Utils, [:passthrough], [] do + {:ok, activity} = + ActivityPub.flag(%{ + actor: reporter, + context: context, + account: target_account, + statuses: [reported_activity], + content: content + }) + + new_data = + activity.data + |> put_in(["object"], [target_account.ap_id, object_ap_id]) + |> Map.put("actor", placeholder.ap_id) + + assert_called(Utils.maybe_federate(%{activity | data: new_data})) + end + end + test_with_mock "reverts on error", %{ reporter: reporter,