2020-01-26 21:11:16 +01:00
|
|
|
defmodule Mobilizon.GraphQL.API.Reports do
|
2019-07-23 13:49:22 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
API for Reports.
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-23 21:59:50 +01:00
|
|
|
alias Mobilizon.{Admin, Users}
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Reports, as: ReportsAction
|
2019-09-22 18:29:13 +02:00
|
|
|
alias Mobilizon.Reports.{Note, Report, ReportStatus}
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Activity
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
|
|
|
Create a report/flag on an actor, and optionally on an event or on comments.
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec report(map()) :: {:ok, Activity.t(), Report.t()} | {:error, any()}
|
2019-11-15 18:36:47 +01:00
|
|
|
def report(args) do
|
2021-09-10 11:27:59 +02:00
|
|
|
case ActivityPub.flag(args, Map.get(args, :forward, false) == true) do
|
|
|
|
{:ok, %Activity{} = activity, %Report{} = report} ->
|
2019-11-15 18:36:47 +01:00
|
|
|
{:ok, activity, report}
|
|
|
|
|
2021-09-10 11:27:59 +02:00
|
|
|
err ->
|
2019-11-15 18:36:47 +01:00
|
|
|
{:error, err}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update the state of a report
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec update_report_status(Actor.t(), Report.t(), ReportStatus.t()) ::
|
|
|
|
{:ok, Report.t()} | {:error, String.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def update_report_status(%Actor{} = actor, %Report{} = report, state) do
|
|
|
|
with {:valid_state, true} <-
|
2019-09-22 18:29:13 +02:00
|
|
|
{:valid_state, ReportStatus.valid_value?(state)},
|
2021-09-10 11:27:59 +02:00
|
|
|
{:ok, %Report{} = report} <- ReportsAction.update_report(report, %{"status" => state}),
|
2020-01-23 21:59:50 +01:00
|
|
|
{:ok, _} <- Admin.log_action(actor, "update", report) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, report}
|
|
|
|
else
|
|
|
|
{:valid_state, false} -> {:error, "Unsupported state"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a note on a report
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec create_report_note(Report.t(), Actor.t(), String.t()) ::
|
|
|
|
{:ok, Note.t()} | {:error, String.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def create_report_note(
|
|
|
|
%Report{id: report_id},
|
|
|
|
%Actor{id: moderator_id, user_id: user_id} = moderator,
|
|
|
|
content
|
|
|
|
) do
|
|
|
|
with %User{role: role} <- Users.get_user!(user_id),
|
|
|
|
{:role, true} <- {:role, role in [:administrator, :moderator]},
|
|
|
|
{:ok, %Note{} = note} <-
|
2019-09-07 02:38:13 +02:00
|
|
|
Mobilizon.Reports.create_note(%{
|
2019-07-23 13:49:22 +02:00
|
|
|
"report_id" => report_id,
|
|
|
|
"moderator_id" => moderator_id,
|
|
|
|
"content" => content
|
|
|
|
}),
|
2020-01-23 21:59:50 +01:00
|
|
|
{:ok, _} <- Admin.log_action(moderator, "create", note) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, note}
|
|
|
|
else
|
|
|
|
{:role, false} ->
|
|
|
|
{:error, "You need to be a moderator or an administrator to create a note on a report"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Delete a report note
|
|
|
|
"""
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec delete_report_note(Note.t(), Actor.t()) :: {:ok, Note.t()} | {:error, String.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def delete_report_note(
|
|
|
|
%Note{moderator_id: note_moderator_id} = note,
|
|
|
|
%Actor{id: moderator_id, user_id: user_id} = moderator
|
|
|
|
) do
|
|
|
|
with {:same_actor, true} <- {:same_actor, note_moderator_id == moderator_id},
|
|
|
|
%User{role: role} <- Users.get_user!(user_id),
|
|
|
|
{:role, true} <- {:role, role in [:administrator, :moderator]},
|
|
|
|
{:ok, %Note{} = note} <-
|
2019-09-07 02:38:13 +02:00
|
|
|
Mobilizon.Reports.delete_note(note),
|
2020-01-23 21:59:50 +01:00
|
|
|
{:ok, _} <- Admin.log_action(moderator, "delete", note) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, note}
|
|
|
|
else
|
|
|
|
{:role, false} ->
|
|
|
|
{:error, "You need to be a moderator or an administrator to create a note on a report"}
|
|
|
|
|
|
|
|
{:same_actor, false} ->
|
|
|
|
{:error, "You can only remove your own notes"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|