2020-01-26 20:11:16 +00:00
|
|
|
defmodule Mobilizon.GraphQL.API.Reports do
|
2019-07-23 11:49:22 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
API for Reports.
|
2019-07-23 11:49:22 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-23 20:59:50 +00:00
|
|
|
alias Mobilizon.{Admin, Users}
|
2023-01-31 18:35:29 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Reports, as: ReportsAction
|
2019-09-22 16:29:13 +00:00
|
|
|
alias Mobilizon.Reports.{Note, Report, ReportStatus}
|
2023-06-01 12:48:42 +00:00
|
|
|
alias Mobilizon.Service.AntiSpam.Akismet
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Users.User
|
2023-01-31 18:35:29 +00:00
|
|
|
import Mobilizon.Web.Gettext, only: [dgettext: 2]
|
|
|
|
require Logger
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
@doc """
|
|
|
|
Create a report/flag on an actor, and optionally on an event or on comments.
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec report(map()) :: {:ok, Activity.t(), Report.t()} | {:error, Ecto.Changeset.t()}
|
2019-11-15 17:36:47 +00:00
|
|
|
def report(args) do
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Flag.flag(args, Map.get(args, :forward, false) == true)
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2023-01-31 18:35:29 +00:00
|
|
|
Update the status of a report
|
2019-07-23 11:49:22 +00:00
|
|
|
"""
|
2023-01-31 18:35:29 +00:00
|
|
|
@spec update_report_status(Actor.t(), Report.t(), atom(), atom() | nil) ::
|
2021-09-28 17:40:37 +00:00
|
|
|
{:ok, Report.t()} | {:error, Ecto.Changeset.t() | String.t()}
|
2023-01-31 18:35:29 +00:00
|
|
|
def update_report_status(
|
|
|
|
%Actor{} = actor,
|
|
|
|
%Report{status: old_status} = report,
|
|
|
|
status,
|
|
|
|
antispam_feedback \\ nil
|
|
|
|
) do
|
|
|
|
if ReportStatus.valid_value?(status) do
|
|
|
|
with {:ok, %Report{} = report} <-
|
|
|
|
ReportsAction.update_report(report, %{
|
|
|
|
"status" => status,
|
|
|
|
"antispam_feedback" => antispam_feedback
|
|
|
|
}) do
|
|
|
|
if old_status != status do
|
|
|
|
Admin.log_action(actor, "update", report)
|
|
|
|
end
|
|
|
|
|
|
|
|
antispam_response =
|
|
|
|
case antispam_feedback do
|
|
|
|
:ham ->
|
|
|
|
Logger.debug("Reporting a report details as ham")
|
|
|
|
Akismet.report_ham(report)
|
|
|
|
|
|
|
|
:spam ->
|
|
|
|
Logger.debug("Reporting a report details as spam")
|
|
|
|
Akismet.report_spam(report)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
if antispam_response != :ok do
|
2023-08-02 07:59:09 +00:00
|
|
|
Logger.warning("Antispam response has been #{inspect(antispam_response)}")
|
2023-01-31 18:35:29 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
{:ok, report}
|
|
|
|
end
|
2019-07-23 11:49:22 +00:00
|
|
|
else
|
2023-01-31 18:35:29 +00:00
|
|
|
{:error, dgettext("errors", "Unsupported status for a report")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a note on a report
|
|
|
|
"""
|
2021-09-10 09:27:59 +00:00
|
|
|
@spec create_report_note(Report.t(), Actor.t(), String.t()) ::
|
2021-09-28 17:40:37 +00:00
|
|
|
{:ok, Note.t()} | {:error, String.t() | Ecto.Changeset.t()}
|
2019-07-23 11:49:22 +00:00
|
|
|
def create_report_note(
|
|
|
|
%Report{id: report_id},
|
|
|
|
%Actor{id: moderator_id, user_id: user_id} = moderator,
|
|
|
|
content
|
|
|
|
) do
|
2021-09-28 17:40:37 +00:00
|
|
|
%User{role: role} = Users.get_user!(user_id)
|
|
|
|
|
|
|
|
if role in [:administrator, :moderator] do
|
|
|
|
with {:ok, %Note{} = note} <-
|
|
|
|
Mobilizon.Reports.create_note(%{
|
|
|
|
"report_id" => report_id,
|
|
|
|
"moderator_id" => moderator_id,
|
|
|
|
"content" => content
|
|
|
|
}),
|
|
|
|
{:ok, _} <- Admin.log_action(moderator, "create", note) do
|
|
|
|
{:ok, note}
|
|
|
|
end
|
2019-07-23 11:49:22 +00:00
|
|
|
else
|
2023-01-31 18:35:29 +00:00
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"You need to be a moderator or an administrator to create a note on a report"
|
|
|
|
)}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Delete a report note
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec delete_report_note(Note.t(), Actor.t()) ::
|
|
|
|
{:ok, Note.t()} | {:error, Ecto.Changeset.t() | String.t()}
|
2019-07-23 11:49:22 +00:00
|
|
|
def delete_report_note(
|
|
|
|
%Note{moderator_id: note_moderator_id} = note,
|
|
|
|
%Actor{id: moderator_id, user_id: user_id} = moderator
|
|
|
|
) do
|
2021-09-28 17:40:37 +00:00
|
|
|
if note_moderator_id == moderator_id do
|
|
|
|
%User{role: role} = Users.get_user!(user_id)
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
if role in [:administrator, :moderator] do
|
|
|
|
with {:ok, %Note{} = note} <-
|
|
|
|
Mobilizon.Reports.delete_note(note),
|
|
|
|
{:ok, _} <- Admin.log_action(moderator, "delete", note) do
|
|
|
|
{:ok, note}
|
|
|
|
end
|
|
|
|
else
|
2023-01-31 18:35:29 +00:00
|
|
|
{:error,
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"You need to be a moderator or an administrator to create a note on a report"
|
|
|
|
)}
|
2021-09-28 17:40:37 +00:00
|
|
|
end
|
|
|
|
else
|
2023-01-31 18:35:29 +00:00
|
|
|
{:error, dgettext("errors", "You can only remove your own notes")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|