From 6f047cc308352cb3437f95e31e73487bba194abe Mon Sep 17 00:00:00 2001 From: tusooa Date: Wed, 9 Nov 2022 22:36:42 -0500 Subject: [PATCH 1/5] Do not strip reported statuses when configured not to --- config/config.exs | 1 + config/description.exs | 6 +++++ lib/pleroma/web/activity_pub/utils.ex | 25 ++++++++++---------- lib/pleroma/web/admin_api/report.ex | 33 +++++++++++++++++++++++++-- test/pleroma/web/common_api_test.exs | 24 +++++++++++++++++++ 5 files changed, 74 insertions(+), 15 deletions(-) diff --git a/config/config.exs b/config/config.exs index a84b15a37..f7564036a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -228,6 +228,7 @@ config :pleroma, :instance, max_pinned_statuses: 1, attachment_links: false, max_report_comment_size: 1000, + report_strip_status: true, safe_dm_mentions: false, healthcheck: false, remote_post_retention_days: 90, diff --git a/config/description.exs b/config/description.exs index 3a2a65272..99a2f8030 100644 --- a/config/description.exs +++ b/config/description.exs @@ -815,6 +815,12 @@ config :pleroma, :config_description, [ 1_000 ] }, + %{ + key: :report_strip_status, + label: "Report strip status", + type: :boolean, + description: "Strip status when closing or resolving a report." + }, %{ key: :safe_dm_mentions, label: "Safe DM mentions", diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index d3b7d804f..57a2f53c4 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -748,22 +748,21 @@ defmodule Pleroma.Web.ActivityPub.Utils do ActivityPub.fetch_activities([], params, :offset) end - def update_report_state(%Activity{} = activity, state) - when state in @strip_status_report_states do - {:ok, stripped_activity} = strip_report_status_data(activity) - - new_data = - activity.data - |> Map.put("state", state) - |> Map.put("object", stripped_activity.data["object"]) - - activity - |> Changeset.change(data: new_data) - |> Repo.update() + defp maybe_strip_report_status(data, state) do + with true <- Config.get([:instance, :report_strip_status]), + true <- state in @strip_status_report_states, + {:ok, stripped_activity} = strip_report_status_data(%Activity{data: data}) do + data |> Map.put("object", stripped_activity.data["object"]) + else + _ -> data + end end def update_report_state(%Activity{} = activity, state) when state in @supported_report_states do - new_data = Map.put(activity.data, "state", state) + new_data = + activity.data + |> Map.put("state", state) + |> maybe_strip_report_status(state) activity |> Changeset.change(data: new_data) diff --git a/lib/pleroma/web/admin_api/report.ex b/lib/pleroma/web/admin_api/report.ex index 8d1abfa56..f377e1804 100644 --- a/lib/pleroma/web/admin_api/report.ex +++ b/lib/pleroma/web/admin_api/report.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.AdminAPI.Report do alias Pleroma.Activity + alias Pleroma.Object alias Pleroma.User def extract_report_info( @@ -16,10 +17,38 @@ defmodule Pleroma.Web.AdminAPI.Report do status_ap_ids |> Enum.reject(&is_nil(&1)) |> Enum.map(fn - act when is_map(act) -> Activity.get_by_ap_id_with_object(act["id"]) - act when is_binary(act) -> Activity.get_by_ap_id_with_object(act) + act when is_map(act) -> + Activity.get_by_ap_id_with_object(act["id"]) || make_fake_activity(act, user) + + act when is_binary(act) -> + Activity.get_by_ap_id_with_object(act) end) %{report: report, user: user, account: account, statuses: statuses} end + + defp make_fake_activity(act, user) do + %Activity{ + id: "pleroma:fake", + data: %{ + "actor" => user.ap_id, + "type" => "Create", + "to" => [], + "cc" => [], + "object" => act["id"], + "published" => act["published"] + }, + recipients: [user.ap_id], + object: %Object{ + data: %{ + "actor" => user.ap_id, + "type" => "Note", + "content" => act["content"], + "published" => act["published"], + "to" => [], + "cc" => [] + } + } + } + end end diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index ee01548f9..8eb4e38e4 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -1154,6 +1154,30 @@ defmodule Pleroma.Web.CommonAPITest do assert activity_id == activity.data["id"] end + test "updates report state, don't strip when report_strip_status is false" do + clear_config([:instance, :report_strip_status], false) + + [reporter, target_user] = insert_pair(:user) + activity = insert(:note_activity, user: target_user) + + {:ok, %Activity{id: report_id, data: report_data}} = + CommonAPI.report(reporter, %{ + account_id: target_user.id, + comment: "I feel offended", + status_ids: [activity.id] + }) + + {:ok, report} = CommonAPI.update_report_state(report_id, "resolved") + + assert report.data["state"] == "resolved" + + [reported_user, reported_activity] = report.data["object"] + + assert reported_user == target_user.ap_id + assert is_map(reported_activity) + assert reported_activity["content"] == report_data["object"] |> Enum.at(1) |> Map.get("content") + end + test "does not update report state when state is unsupported" do [reporter, target_user] = insert_pair(:user) activity = insert(:note_activity, user: target_user) From 717c5901f893527b059201d1ce7899060a18a1a5 Mon Sep 17 00:00:00 2001 From: tusooa Date: Wed, 9 Nov 2022 23:02:27 -0500 Subject: [PATCH 2/5] Render a generated reported activity properly --- lib/pleroma/web/admin_api/report.ex | 8 ++++-- .../controllers/report_controller_test.exs | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/admin_api/report.ex b/lib/pleroma/web/admin_api/report.ex index f377e1804..6856bfcb3 100644 --- a/lib/pleroma/web/admin_api/report.ex +++ b/lib/pleroma/web/admin_api/report.ex @@ -36,7 +36,9 @@ defmodule Pleroma.Web.AdminAPI.Report do "to" => [], "cc" => [], "object" => act["id"], - "published" => act["published"] + "published" => act["published"], + "id" => act["id"], + "context" => "pleroma:fake" }, recipients: [user.ap_id], object: %Object{ @@ -46,7 +48,9 @@ defmodule Pleroma.Web.AdminAPI.Report do "content" => act["content"], "published" => act["published"], "to" => [], - "cc" => [] + "cc" => [], + "id" => act["id"], + "context" => "pleroma:fake" } } } diff --git a/test/pleroma/web/admin_api/controllers/report_controller_test.exs b/test/pleroma/web/admin_api/controllers/report_controller_test.exs index 30dcb87e2..164cbb95b 100644 --- a/test/pleroma/web/admin_api/controllers/report_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/report_controller_test.exs @@ -54,6 +54,32 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do assert notes["content"] == "this is an admin note" end + test "renders reported content even if the status is deleted", %{conn: conn} do + [reporter, target_user] = insert_pair(:user) + activity = insert(:note_activity, user: target_user) + activity = Activity.normalize(activity) + + {:ok, %{id: report_id}} = + CommonAPI.report(reporter, %{ + account_id: target_user.id, + comment: "I feel offended", + status_ids: [activity.id] + }) + + CommonAPI.delete(activity.id, target_user) + + response = + conn + |> get("/api/pleroma/admin/reports/#{report_id}") + |> json_response_and_validate_schema(:ok) + + assert response["id"] == report_id + + assert [status] = response["statuses"] + assert activity.data["id"] == status["uri"] + assert activity.object.data["content"] == status["content"] + end + test "returns 404 when report id is invalid", %{conn: conn} do conn = get(conn, "/api/pleroma/admin/reports/test") From 7991364380f56f9892d99805bcae6ced7f180cff Mon Sep 17 00:00:00 2001 From: tusooa Date: Fri, 11 Nov 2022 18:32:08 -0500 Subject: [PATCH 3/5] Lint --- test/pleroma/web/common_api_test.exs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index 8eb4e38e4..4dc0d9cbe 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -1175,7 +1175,9 @@ defmodule Pleroma.Web.CommonAPITest do assert reported_user == target_user.ap_id assert is_map(reported_activity) - assert reported_activity["content"] == report_data["object"] |> Enum.at(1) |> Map.get("content") + + assert reported_activity["content"] == + report_data["object"] |> Enum.at(1) |> Map.get("content") end test "does not update report state when state is unsupported" do From c2cfe0c690d1524f13e4e7eb5590d382c71b1c56 Mon Sep 17 00:00:00 2001 From: Haelwenn Date: Sat, 12 Nov 2022 17:44:31 +0000 Subject: [PATCH 4/5] Clarify config description --- config/description.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/description.exs b/config/description.exs index 99a2f8030..b2072420a 100644 --- a/config/description.exs +++ b/config/description.exs @@ -819,7 +819,7 @@ config :pleroma, :config_description, [ key: :report_strip_status, label: "Report strip status", type: :boolean, - description: "Strip status when closing or resolving a report." + description: "Strip associated statuses in reports to ids when closed/resolved, otherwise keep a copy" }, %{ key: :safe_dm_mentions, From e3e68b93774ffb3b45e395e7ea5cea2467b4395f Mon Sep 17 00:00:00 2001 From: tusooa Date: Sat, 12 Nov 2022 12:54:41 -0500 Subject: [PATCH 5/5] Update config cheatsheet --- docs/configuration/cheatsheet.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 4c083c336..0dbf71aba 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -49,6 +49,7 @@ To add configuration to your config file, you can copy it from the base config. * `autofollowing_nicknames`: Set to nicknames of (local) users that automatically follows every newly registered user. * `attachment_links`: Set to true to enable automatically adding attachment link text to statuses. * `max_report_comment_size`: The maximum size of the report comment (Default: `1000`). +* `report_strip_status`: Strip associated statuses in reports to ids when closed/resolved, otherwise keep a copy. * `safe_dm_mentions`: If set to true, only mentions at the beginning of a post will be used to address people in direct messages. This is to prevent accidental mentioning of people when talking about them (e.g. "@friend hey i really don't like @enemy"). Default: `false`. * `healthcheck`: If set to true, system data will be shown on ``/api/v1/pleroma/healthcheck``. * `remote_post_retention_days`: The default amount of days to retain remote posts when pruning the database.