2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Report do
|
2019-07-23 13:49:22 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the report-related GraphQL calls.
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
alias Mobilizon.{Actors, Config, Reports}
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2020-01-26 21:11:16 +01:00
|
|
|
alias Mobilizon.GraphQL.API
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec list_reports(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Report.t())} | {:error, String.t()}
|
2019-09-07 19:54:11 +02:00
|
|
|
def list_reports(
|
|
|
|
_parent,
|
2022-01-10 10:17:50 +01:00
|
|
|
%{page: page, limit: limit} = args,
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
when is_moderator(role) do
|
2022-01-10 10:17:50 +01:00
|
|
|
{:ok,
|
|
|
|
Mobilizon.Reports.list_reports(
|
|
|
|
page: page,
|
|
|
|
limit: limit,
|
|
|
|
sort: :updated_at,
|
|
|
|
direction: :desc,
|
|
|
|
status: Map.get(args, :status),
|
|
|
|
domain: Map.get(args, :domain)
|
|
|
|
)}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_reports(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to list reports")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_report(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Report.t()} | {:error, String.t()}
|
2019-09-07 19:54:11 +02:00
|
|
|
def get_report(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}})
|
2019-07-23 13:49:22 +02:00
|
|
|
when is_moderator(role) do
|
2019-09-09 09:31:08 +02:00
|
|
|
case Mobilizon.Reports.get_report(id) do
|
|
|
|
%Report{} = report ->
|
|
|
|
{:ok, report}
|
|
|
|
|
|
|
|
nil ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Report not found")}
|
2019-09-09 09:31:08 +02:00
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_report(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to view a report")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2020-11-06 15:43:38 +01:00
|
|
|
Create a report, either logged-in or anonymously
|
2019-07-23 13:49:22 +02:00
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_report(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Report.t()} | {:error, String.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def create_report(
|
|
|
|
_parent,
|
2020-11-19 17:06:28 +01:00
|
|
|
args,
|
2021-09-10 11:35:32 +02:00
|
|
|
%{context: %{current_actor: %Actor{id: reporter_id}}} = _resolution
|
2019-07-23 13:49:22 +02:00
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
case args |> Map.put(:reporter_id, reporter_id) |> API.Reports.report() do
|
|
|
|
{:ok, _, %Report{} = report} ->
|
|
|
|
{:ok, report}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
_error ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Error while saving report")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-09 14:07:49 +02:00
|
|
|
def create_report(
|
|
|
|
_parent,
|
2020-11-19 17:06:28 +01:00
|
|
|
args,
|
2020-06-09 14:07:49 +02:00
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
with {:anonymous_reporting_allowed, true} <-
|
|
|
|
{:anonymous_reporting_allowed, Config.anonymous_reporting?()},
|
2020-11-19 17:06:28 +01:00
|
|
|
{:ok, _, %Report{} = report} <-
|
|
|
|
args |> Map.put(:reporter_id, Config.anonymous_actor_id()) |> API.Reports.report() do
|
2020-06-09 14:07:49 +02:00
|
|
|
{:ok, report}
|
|
|
|
else
|
|
|
|
{:anonymous_reporting_allowed, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create reports")}
|
2020-06-09 14:07:49 +02:00
|
|
|
|
|
|
|
_error ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Error while saving report")}
|
2020-06-09 14:07:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
|
|
|
Update a report's status
|
|
|
|
"""
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec update_report(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Report.t()} | {:error, String.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def update_report(
|
|
|
|
_parent,
|
2023-01-31 19:35:29 +01:00
|
|
|
%{report_id: report_id, status: status} = args,
|
2021-09-10 11:35:32 +02:00
|
|
|
%{context: %{current_user: %User{role: role}, current_actor: %Actor{} = actor}}
|
2019-07-23 13:49:22 +02:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Report{} = report <- Mobilizon.Reports.get_report(report_id),
|
2023-01-31 19:35:29 +01:00
|
|
|
{:ok, %Report{} = report} <-
|
|
|
|
API.Reports.update_report_status(
|
|
|
|
actor,
|
|
|
|
report,
|
|
|
|
status,
|
|
|
|
Map.get(args, :antispam_feedback)
|
|
|
|
) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, report}
|
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
_error ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Error while updating report")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_report(_parent, _args, _resolution) do
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to update a report")}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_report_note(any(), map(), Absinthe.Resolution.t()) :: {:ok, Note.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def create_report_note(
|
|
|
|
_parent,
|
2020-11-19 17:06:28 +01:00
|
|
|
%{report_id: report_id, content: content},
|
2021-09-10 11:35:32 +02:00
|
|
|
%{context: %{current_user: %User{role: role}, current_actor: %Actor{id: moderator_id}}}
|
2019-07-23 13:49:22 +02:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Report{} = report <- Reports.get_report(report_id),
|
2019-09-09 00:52:49 +02:00
|
|
|
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
|
2020-01-26 21:11:16 +01:00
|
|
|
{:ok, %Note{} = note} <- API.Reports.create_report_note(report, moderator, content) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, note}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec delete_report_note(any(), map(), Absinthe.Resolution.t()) :: {:ok, map()}
|
2019-09-07 19:54:11 +02:00
|
|
|
def delete_report_note(
|
|
|
|
_parent,
|
2020-11-19 17:06:28 +01:00
|
|
|
%{note_id: note_id},
|
2021-09-10 11:35:32 +02:00
|
|
|
%{context: %{current_user: %User{role: role}, current_actor: %Actor{id: moderator_id}}}
|
2019-09-07 19:54:11 +02:00
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
when is_moderator(role) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Note{} = note <- Reports.get_note(note_id),
|
2019-09-09 00:52:49 +02:00
|
|
|
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
|
2020-01-26 21:11:16 +01:00
|
|
|
{:ok, %Note{} = note} <- API.Reports.delete_report_note(note, moderator) do
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %{id: note.id}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|