Implement report notes destruction

This commit is contained in:
Maxim Filippov 2019-12-08 11:27:23 +03:00
parent 08c89fd2b8
commit a7f77785c2
7 changed files with 88 additions and 18 deletions

View File

@ -128,17 +128,35 @@ def insert_log(%{
{:ok, ModerationLog} | {:error, any} {:ok, ModerationLog} | {:error, any}
def insert_log(%{ def insert_log(%{
actor: %User{} = actor, actor: %User{} = actor,
action: "report_response", action: "report_note",
subject: %Activity{} = subject, subject: %Activity{} = subject,
text: text text: text
}) do }) do
%ModerationLog{ %ModerationLog{
data: %{ data: %{
"actor" => user_to_map(actor), "actor" => user_to_map(actor),
"action" => "report_response", "action" => "report_note",
"subject" => report_to_map(subject), "subject" => report_to_map(subject),
"text" => text, "text" => text
"message" => "" }
}
|> insert_log_entry_with_message()
end
@spec insert_log(%{actor: User, subject: Activity, action: String.t(), text: String.t()}) ::
{:ok, ModerationLog} | {:error, any}
def insert_log(%{
actor: %User{} = actor,
action: "report_note_delete",
subject: %Activity{} = subject,
text: text
}) do
%ModerationLog{
data: %{
"actor" => user_to_map(actor),
"action" => "report_note_delete",
"subject" => report_to_map(subject),
"text" => text
} }
} }
|> insert_log_entry_with_message() |> insert_log_entry_with_message()
@ -556,12 +574,24 @@ def get_log_entry_message(%ModerationLog{
def get_log_entry_message(%ModerationLog{ def get_log_entry_message(%ModerationLog{
data: %{ data: %{
"actor" => %{"nickname" => actor_nickname}, "actor" => %{"nickname" => actor_nickname},
"action" => "report_response", "action" => "report_note",
"subject" => %{"id" => subject_id, "type" => "report"}, "subject" => %{"id" => subject_id, "type" => "report"},
"text" => text "text" => text
} }
}) do }) do
"@#{actor_nickname} responded with '#{text}' to report ##{subject_id}" "@#{actor_nickname} added note '#{text}' to report ##{subject_id}"
end
@spec get_log_entry_message(ModerationLog) :: String.t()
def get_log_entry_message(%ModerationLog{
data: %{
"actor" => %{"nickname" => actor_nickname},
"action" => "report_note_delete",
"subject" => %{"id" => subject_id, "type" => "report"},
"text" => text
}
}) do
"@#{actor_nickname} deleted note '#{text}' from report ##{subject_id}"
end end
@spec get_log_entry_message(ModerationLog) :: String.t() @spec get_log_entry_message(ModerationLog) :: String.t()

View File

@ -38,9 +38,11 @@ def create(user_id, activity_id, content) do
|> Repo.insert() |> Repo.insert()
end end
def get_all_for_status(status_id) do @spec destroy(FlakeId.Ecto.CompatType.t()) ::
ReportNote {:ok, ReportNote.t()} | {:error, Changeset.t()}
|> where(activity_id: ^status_id) def destroy(id) do
|> Repo.all() from(r in ReportNote, where: r.id == ^id)
|> Repo.one()
|> Repo.delete()
end end
end end

View File

@ -691,14 +691,14 @@ def reports_update(%{assigns: %{user: admin}} = conn, %{"reports" => reports}) d
end end
def report_notes_create(%{assigns: %{user: user}} = conn, %{ def report_notes_create(%{assigns: %{user: user}} = conn, %{
"id" => status_id, "id" => report_id,
"content" => content "content" => content
}) do }) do
with {:ok, _} <- ReportNote.create(user.id, status_id, content) do with {:ok, _} <- ReportNote.create(user.id, report_id, content) do
ModerationLog.insert_log(%{ ModerationLog.insert_log(%{
action: "report_response", action: "report_note",
actor: user, actor: user,
subject: Activity.get_by_id(status_id), subject: Activity.get_by_id(report_id),
text: content text: content
}) })
@ -708,6 +708,24 @@ def report_notes_create(%{assigns: %{user: user}} = conn, %{
end end
end end
def report_notes_delete(%{assigns: %{user: user}} = conn, %{
"id" => note_id,
"report_id" => report_id
}) do
with {:ok, note} <- ReportNote.destroy(note_id) do
ModerationLog.insert_log(%{
action: "report_note_delete",
actor: user,
subject: Activity.get_by_id(report_id),
text: note.content
})
json_response(conn, :no_content, "")
else
_ -> json_response(conn, :bad_request, "")
end
end
def status_update(%{assigns: %{user: admin}} = conn, %{"id" => id} = params) do def status_update(%{assigns: %{user: admin}} = conn, %{"id" => id} = params) do
with {:ok, activity} <- CommonAPI.update_activity_scope(id, params) do with {:ok, activity} <- CommonAPI.update_activity_scope(id, params) do
{:ok, sensitive} = Ecto.Type.cast(:boolean, params["sensitive"]) {:ok, sensitive} = Ecto.Type.cast(:boolean, params["sensitive"])

View File

@ -69,13 +69,19 @@ def render("index_notes.json", %{notes: notes}) when is_list(notes) do
def render("index_notes.json", _), do: [] def render("index_notes.json", _), do: []
def render("show_note.json", %{content: content, user_id: user_id, inserted_at: inserted_at}) do def render("show_note.json", %{
id: id,
content: content,
user_id: user_id,
inserted_at: inserted_at
}) do
user = User.get_by_id(user_id) user = User.get_by_id(user_id)
%{ %{
id: id,
content: content, content: content,
user: merge_account_views(user), user: merge_account_views(user),
created_at: inserted_at created_at: Utils.to_masto_date(inserted_at)
} }
end end

View File

@ -188,6 +188,7 @@ defmodule Pleroma.Web.Router do
get("/reports/:id", AdminAPIController, :report_show) get("/reports/:id", AdminAPIController, :report_show)
patch("/reports", AdminAPIController, :reports_update) patch("/reports", AdminAPIController, :reports_update)
post("/reports/:id/notes", AdminAPIController, :report_notes_create) post("/reports/:id/notes", AdminAPIController, :report_notes_create)
delete("/reports/:report_id/notes/:id", AdminAPIController, :report_notes_delete)
put("/statuses/:id", AdminAPIController, :status_update) put("/statuses/:id", AdminAPIController, :status_update)
delete("/statuses/:id", AdminAPIController, :status_delete) delete("/statuses/:id", AdminAPIController, :status_delete)

View File

@ -214,7 +214,7 @@ test "logging report response", %{moderator: moderator} do
{:ok, _} = {:ok, _} =
ModerationLog.insert_log(%{ ModerationLog.insert_log(%{
actor: moderator, actor: moderator,
action: "report_response", action: "report_note",
subject: report, subject: report,
text: "look at this" text: "look at this"
}) })

View File

@ -10,6 +10,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
alias Pleroma.HTML alias Pleroma.HTML
alias Pleroma.ModerationLog alias Pleroma.ModerationLog
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.ReportNote
alias Pleroma.Tests.ObanHelpers alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.UserInviteToken alias Pleroma.UserInviteToken
@ -2940,7 +2941,7 @@ test "it resend emails for two users", %{admin: admin} do
end end
test "it creates report note", %{admin_id: admin_id, report_id: report_id} do test "it creates report note", %{admin_id: admin_id, report_id: report_id} do
[note, _] = Repo.all(Pleroma.ReportNote) [note, _] = Repo.all(ReportNote)
assert %{ assert %{
activity_id: ^report_id, activity_id: ^report_id,
@ -2964,6 +2965,18 @@ test "it returns reports with notes", %{admin: admin} do
assert note["created_at"] assert note["created_at"]
assert response["total"] == 1 assert response["total"] == 1
end end
test "it deletes the note", %{admin: admin, report_id: report_id} do
assert ReportNote |> Repo.all() |> length() == 2
[note, _] = Repo.all(ReportNote)
build_conn()
|> assign(:user, admin)
|> delete("/api/pleroma/admin/reports/#{report_id}/notes/#{note.id}")
assert ReportNote |> Repo.all() |> length() == 1
end
end end
end end