logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/
commit: 27d271b4efd452db791613b426c88f1fecee1b83
parent 29be5018b0a7a50708aad68f69f187184ceaa7b4
Author: tusooa <tusooa@kazv.moe>
Date:   Tue, 13 Dec 2022 21:30:10 -0500

Add maybe_anonymize_reporter/1

Diffstat:

Mconfig/config.exs4+++-
Mconfig/description.exs17+++++++++++++++++
Mlib/pleroma/web/activity_pub/utils.ex12++++++++++++
Mtest/pleroma/web/activity_pub/utils_test.exs60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 92 insertions(+), 1 deletion(-)

diff --git a/config/config.exs b/config/config.exs @@ -364,7 +364,9 @@ config :pleroma, :activitypub, note_replies_output_limit: 5, sign_object_fetches: true, authorized_fetch_mode: false, - client_api_enabled: false + client_api_enabled: false, + anonymize_reporter: false, + anonymize_reporter_local_nickname: "" config :pleroma, :streamer, workers: 3, diff --git a/config/description.exs b/config/description.exs @@ -1790,6 +1790,23 @@ config :pleroma, :config_description, [ key: :client_api_enabled, type: :boolean, description: "Allow client to server ActivityPub interactions" + }, + %{ + key: :anonymize_reporter, + type: :boolean, + label: "Anonymize local reports", + description: + "If true, replace local reporters with the designated local user for the copy to be sent to remote servers" + }, + %{ + key: :anonymize_reporter_local_nickname, + type: :string, + label: "Anonymized reporter", + description: + "The nickname of the designated local user that replaces the actual reporter in the copy to be sent to remote servers", + suggestions: [ + "lain" + ] } ] }, diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex @@ -875,6 +875,18 @@ defmodule Pleroma.Web.ActivityPub.Utils do {:ok, %{activity | data: new_data}} end + def maybe_anonymize_reporter(activity) 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 + activity + |> Map.put("actor", ap_id) + else + _ -> activity + end + end + def update_activity_visibility(activity, visibility) when visibility in @valid_visibilities do [to, cc, recipients] = activity diff --git a/test/pleroma/web/activity_pub/utils_test.exs b/test/pleroma/web/activity_pub/utils_test.exs @@ -670,4 +670,64 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do ) end end + + describe "maybe_anonymize_reporter/1" do + setup do + reporter = insert(:user) + report = %{"actor" => reporter.ap_id} + + %{ + placeholder: insert(:user), + reporter: reporter, + report: report + } + end + + test "anonymize when configured correctly", %{ + placeholder: placeholder, + report: report + } do + clear_config([:activitypub, :anonymize_reporter], true) + clear_config([:activitypub, :anonymize_reporter_local_nickname], placeholder.nickname) + + assert %{"actor" => placeholder.ap_id} == Utils.maybe_anonymize_reporter(report) + end + + test "do not anonymize when disabled", %{ + placeholder: placeholder, + reporter: reporter, + report: report + } do + clear_config([:activitypub, :anonymize_reporter], false) + clear_config([:activitypub, :anonymize_reporter_local_nickname], placeholder.nickname) + + assert %{"actor" => reporter.ap_id} == Utils.maybe_anonymize_reporter(report) + end + + test "do not anonymize when user does not exist", %{ + placeholder: placeholder, + reporter: reporter, + report: report + } do + clear_config([:activitypub, :anonymize_reporter], true) + + clear_config( + [:activitypub, :anonymize_reporter_local_nickname], + placeholder.nickname <> "MewMew" + ) + + assert %{"actor" => reporter.ap_id} == Utils.maybe_anonymize_reporter(report) + end + + test "do not anonymize when user is not local", %{ + reporter: reporter, + report: report + } do + placeholder = insert(:user, local: false) + clear_config([:activitypub, :anonymize_reporter], true) + clear_config([:activitypub, :anonymize_reporter_local_nickname], placeholder.nickname) + + assert %{"actor" => reporter.ap_id} == Utils.maybe_anonymize_reporter(report) + end + end end