diff --git a/lib/pleroma/web/api_spec/operations/mastodon_admin/report_operation.ex b/lib/pleroma/web/api_spec/operations/mastodon_admin/report_operation.ex index f5ae0a4b0f..00c06bb9b7 100644 --- a/lib/pleroma/web/api_spec/operations/mastodon_admin/report_operation.ex +++ b/lib/pleroma/web/api_spec/operations/mastodon_admin/report_operation.ex @@ -103,6 +103,42 @@ def reopen_operation do } end + def assign_to_self_operation do + %Operation{ + tags: ["Report methods"], + summary: "Assign report to self", + operationId: "MastodonAdmin.ReportController.assign_to_self", + description: "Claim the handling of this report to yourself.", + security: [%{"oAuth" => ["admin:write:reports"]}], + parameters: [ + Operation.parameter(:id, :path, :string, "ID of the report") + ], + responses: %{ + 200 => Operation.response("Account", "application/json", report()), + 400 => Operation.response("Error", "application/json", ApiError), + 401 => Operation.response("Error", "application/json", ApiError) + } + } + end + + def unassign_operation do + %Operation{ + tags: ["Report methods"], + summary: "Unassign report", + operationId: "MastodonAdmin.ReportController.unassign", + description: "Unassign a report so that someone else can claim it.", + security: [%{"oAuth" => ["admin:write:reports"]}], + parameters: [ + Operation.parameter(:id, :path, :string, "ID of the report") + ], + responses: %{ + 200 => Operation.response("Account", "application/json", report()), + 400 => Operation.response("Error", "application/json", ApiError), + 401 => Operation.response("Error", "application/json", ApiError) + } + } + end + defp report do %Schema{ title: "Report", diff --git a/lib/pleroma/web/mastodon_api/admin/controllers/report_controller.ex b/lib/pleroma/web/mastodon_api/admin/controllers/report_controller.ex index 217a97cc98..35a10fa8c9 100644 --- a/lib/pleroma/web/mastodon_api/admin/controllers/report_controller.ex +++ b/lib/pleroma/web/mastodon_api/admin/controllers/report_controller.ex @@ -24,7 +24,11 @@ defmodule Pleroma.Web.MastodonAPI.Admin.ReportController do plug(OAuthScopesPlug, %{scopes: ["admin:read:reports"]} when action in [:index, :show]) - plug(OAuthScopesPlug, %{scopes: ["admin:write:reports"]} when action in [:resolve, :reopen]) + plug( + OAuthScopesPlug, + %{scopes: ["admin:write:reports"]} + when action in [:resolve, :reopen, :assign_to_self, :unassign] + ) defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MastodonAdmin.ReportOperation @@ -89,6 +93,41 @@ def reopen(%{assigns: %{user: admin}} = conn, %{id: id}) do end end + def assign_to_self(%{assigns: %{user: admin}} = conn, %{id: id}) do + with {:ok, activity} <- CommonAPI.assign_report_to_account(id, admin.id), + report <- Activity.get_by_id_with_user_actor(activity.id) do + ModerationLog.insert_log(%{ + action: "report_assigned", + actor: admin, + subject: activity, + subject_actor: report.user_actor, + assigned_account: admin.nickname + }) + + render(conn, "show.json", Report.extract_report_info(report)) + else + {:error, error} -> + json_response(conn, :bad_request, %{error: error}) + end + end + + def unassign(%{assigns: %{user: admin}} = conn, %{id: id}) do + with {:ok, activity} <- CommonAPI.assign_report_to_account(id, nil), + report <- Activity.get_by_id_with_user_actor(activity.id) do + ModerationLog.insert_log(%{ + action: "report_unassigned", + actor: admin, + subject: activity, + subject_actor: report.user_actor + }) + + render(conn, "show.json", Report.extract_report_info(report)) + else + {:error, error} -> + json_response(conn, :bad_request, %{error: error}) + end + end + defp restrict_state(opts, %{resolved: true}), do: Map.put(opts, :state, "resolved") defp restrict_state(opts, %{resolved: false}), do: Map.put(opts, :state, "open") diff --git a/lib/pleroma/web/mastodon_api/admin/views/report_view.ex b/lib/pleroma/web/mastodon_api/admin/views/report_view.ex index b966bc0c7e..c0b44ae82f 100644 --- a/lib/pleroma/web/mastodon_api/admin/views/report_view.ex +++ b/lib/pleroma/web/mastodon_api/admin/views/report_view.ex @@ -21,6 +21,7 @@ def render("show.json", %{ report: report, user: account, account: target_account, + assigned_account: assigned_account, statuses: statuses }) do created_at = Utils.to_masto_date(report.data["published"]) @@ -32,6 +33,13 @@ def render("show.json", %{ nil end + assigned_account = + if assigned_account do + AccountView.render("show.json", %{user: assigned_account}) + else + nil + end + %{ id: report.id, action_taken: report.data["state"] != "open", @@ -41,7 +49,7 @@ def render("show.json", %{ updated_at: created_at, account: AccountView.render("show.json", %{user: account}), target_account: AccountView.render("show.json", %{user: target_account}), - assigned_account: nil, + assigned_account: assigned_account, action_taken_by_account: nil, statuses: StatusView.render("index.json", %{ diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index db24aa46c8..8c7ef2401a 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -316,6 +316,8 @@ defmodule Pleroma.Web.Router do get("/reports/:id", ReportController, :show) post("/reports/:id/resolve", ReportController, :resolve) post("/reports/:id/reopen", ReportController, :reopen) + post("/reports/:id/assign_to_self", ReportController, :assign_to_self) + post("/reports/:id/unassign", ReportController, :unassign) end scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do 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 c28d849419..0a3ab082e1 100644 --- a/test/pleroma/web/admin_api/controllers/report_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/report_controller_test.exs @@ -362,7 +362,7 @@ test "assigns account to report", %{conn: conn, admin: admin} do |> put_req_header("content-type", "application/json") |> post("/api/pleroma/admin/reports/assign_account", %{ "reports" => [ - %{"assigned_account" => admin.id, "id" => report_id} + %{"assigned_account" => admin.nickname, "id" => report_id} ] }) |> json_response_and_validate_schema(:no_content) diff --git a/test/pleroma/web/mastodon_api/admin/controllers/report_controller_test.exs b/test/pleroma/web/mastodon_api/admin/controllers/report_controller_test.exs index a0cc5f459b..ad658bb130 100644 --- a/test/pleroma/web/mastodon_api/admin/controllers/report_controller_test.exs +++ b/test/pleroma/web/mastodon_api/admin/controllers/report_controller_test.exs @@ -115,4 +115,38 @@ test "reopen a report", %{conn: conn} do |> json_response_and_validate_schema(200) end end + + describe "POST /api/v1/admin/reports/:id/assign_to_self" do + test "assign a report to self", %{conn: conn, admin: %{id: admin_id}} do + [reporter, target_user] = insert_pair(:user) + + {:ok, %{id: report_id}} = + CommonAPI.report(reporter, %{ + account_id: target_user.id + }) + + assert %{"id" => ^report_id, "assigned_account" => %{"id" => ^admin_id}} = + conn + |> post("/api/v1/admin/reports/#{report_id}/assign_to_self") + |> json_response_and_validate_schema(200) + end + end + + describe "POST /api/v1/admin/reports/:id/unassign" do + test "unassign a report", %{conn: conn, admin: %{id: admin_id}} do + [reporter, target_user] = insert_pair(:user) + + {:ok, %{id: report_id}} = + CommonAPI.report(reporter, %{ + account_id: target_user.id + }) + + CommonAPI.assign_report_to_account(report_id, admin_id) + + assert %{"id" => ^report_id, "assigned_account" => nil} = + conn + |> post("/api/v1/admin/reports/#{report_id}/unassign") + |> json_response_and_validate_schema(200) + end + end end