Add mastodon admin API routes for report assigning
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
db77571319
commit
9747233375
6 changed files with 122 additions and 3 deletions
|
@ -103,6 +103,42 @@ def reopen_operation do
|
||||||
}
|
}
|
||||||
end
|
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
|
defp report do
|
||||||
%Schema{
|
%Schema{
|
||||||
title: "Report",
|
title: "Report",
|
||||||
|
|
|
@ -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: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
|
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
|
||||||
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: true}), do: Map.put(opts, :state, "resolved")
|
||||||
|
|
||||||
defp restrict_state(opts, %{resolved: false}), do: Map.put(opts, :state, "open")
|
defp restrict_state(opts, %{resolved: false}), do: Map.put(opts, :state, "open")
|
||||||
|
|
|
@ -21,6 +21,7 @@ def render("show.json", %{
|
||||||
report: report,
|
report: report,
|
||||||
user: account,
|
user: account,
|
||||||
account: target_account,
|
account: target_account,
|
||||||
|
assigned_account: assigned_account,
|
||||||
statuses: statuses
|
statuses: statuses
|
||||||
}) do
|
}) do
|
||||||
created_at = Utils.to_masto_date(report.data["published"])
|
created_at = Utils.to_masto_date(report.data["published"])
|
||||||
|
@ -32,6 +33,13 @@ def render("show.json", %{
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
assigned_account =
|
||||||
|
if assigned_account do
|
||||||
|
AccountView.render("show.json", %{user: assigned_account})
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
|
||||||
%{
|
%{
|
||||||
id: report.id,
|
id: report.id,
|
||||||
action_taken: report.data["state"] != "open",
|
action_taken: report.data["state"] != "open",
|
||||||
|
@ -41,7 +49,7 @@ def render("show.json", %{
|
||||||
updated_at: created_at,
|
updated_at: created_at,
|
||||||
account: AccountView.render("show.json", %{user: account}),
|
account: AccountView.render("show.json", %{user: account}),
|
||||||
target_account: AccountView.render("show.json", %{user: target_account}),
|
target_account: AccountView.render("show.json", %{user: target_account}),
|
||||||
assigned_account: nil,
|
assigned_account: assigned_account,
|
||||||
action_taken_by_account: nil,
|
action_taken_by_account: nil,
|
||||||
statuses:
|
statuses:
|
||||||
StatusView.render("index.json", %{
|
StatusView.render("index.json", %{
|
||||||
|
|
|
@ -316,6 +316,8 @@ defmodule Pleroma.Web.Router do
|
||||||
get("/reports/:id", ReportController, :show)
|
get("/reports/:id", ReportController, :show)
|
||||||
post("/reports/:id/resolve", ReportController, :resolve)
|
post("/reports/:id/resolve", ReportController, :resolve)
|
||||||
post("/reports/:id/reopen", ReportController, :reopen)
|
post("/reports/:id/reopen", ReportController, :reopen)
|
||||||
|
post("/reports/:id/assign_to_self", ReportController, :assign_to_self)
|
||||||
|
post("/reports/:id/unassign", ReportController, :unassign)
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
scope "/api/v1/pleroma/emoji", Pleroma.Web.PleromaAPI do
|
||||||
|
|
|
@ -362,7 +362,7 @@ test "assigns account to report", %{conn: conn, admin: admin} do
|
||||||
|> put_req_header("content-type", "application/json")
|
|> put_req_header("content-type", "application/json")
|
||||||
|> post("/api/pleroma/admin/reports/assign_account", %{
|
|> post("/api/pleroma/admin/reports/assign_account", %{
|
||||||
"reports" => [
|
"reports" => [
|
||||||
%{"assigned_account" => admin.id, "id" => report_id}
|
%{"assigned_account" => admin.nickname, "id" => report_id}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|> json_response_and_validate_schema(:no_content)
|
|> json_response_and_validate_schema(:no_content)
|
||||||
|
|
|
@ -115,4 +115,38 @@ test "reopen a report", %{conn: conn} do
|
||||||
|> json_response_and_validate_schema(200)
|
|> json_response_and_validate_schema(200)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue