Add mastodon admin API routes for report assigning

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-02-18 17:46:37 +01:00
parent db77571319
commit 9747233375
6 changed files with 122 additions and 3 deletions

View file

@ -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",

View file

@ -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")

View file

@ -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", %{

View file

@ -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

View file

@ -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)

View file

@ -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