2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Report do
|
2019-07-23 11:49:22 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Handles the report-related GraphQL calls.
|
2019-07-23 11:49:22 +00:00
|
|
|
"""
|
2019-09-22 14:26:23 +00:00
|
|
|
|
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Actors
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-06-09 12:07:49 +00:00
|
|
|
alias Mobilizon.Config
|
2019-09-22 14:26:23 +00:00
|
|
|
alias Mobilizon.Reports
|
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2019-07-23 11:49:22 +00:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2020-01-26 20:11:16 +00:00
|
|
|
alias Mobilizon.GraphQL.API
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
def list_reports(
|
|
|
|
_parent,
|
2019-09-21 21:59:07 +00:00
|
|
|
%{page: page, limit: limit, status: status},
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: %User{role: role}}}
|
|
|
|
)
|
2019-07-23 11:49:22 +00:00
|
|
|
when is_moderator(role) do
|
2019-09-09 07:31:08 +00:00
|
|
|
{:ok, Mobilizon.Reports.list_reports(page, limit, :updated_at, :desc, status)}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_reports(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to list reports")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
def get_report(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}})
|
2019-07-23 11:49:22 +00:00
|
|
|
when is_moderator(role) do
|
2019-09-09 07:31:08 +00:00
|
|
|
case Mobilizon.Reports.get_report(id) do
|
|
|
|
%Report{} = report ->
|
|
|
|
{:ok, report}
|
|
|
|
|
|
|
|
nil ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Report not found")}
|
2019-09-09 07:31:08 +00:00
|
|
|
end
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_report(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to view a report")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a report
|
|
|
|
"""
|
|
|
|
def create_report(
|
|
|
|
_parent,
|
2019-11-15 17:36:47 +00:00
|
|
|
%{reporter_id: reporter_id} = args,
|
2020-06-09 12:07:49 +00:00
|
|
|
%{context: %{current_user: %User{} = user}} = _resolution
|
2019-07-23 11:49:22 +00:00
|
|
|
) do
|
2019-11-15 17:36:47 +00:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, reporter_id),
|
2020-01-26 20:11:16 +00:00
|
|
|
{:ok, _, %Report{} = report} <- API.Reports.report(args) do
|
2019-07-23 11:49:22 +00:00
|
|
|
{:ok, report}
|
|
|
|
else
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Reporter profile is not owned by authenticated user")}
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
_error ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Error while saving report")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-09 12:07:49 +00:00
|
|
|
@doc """
|
|
|
|
Create a report anonymously if allowed
|
|
|
|
"""
|
|
|
|
def create_report(
|
|
|
|
_parent,
|
|
|
|
%{reporter_id: reporter_id} = args,
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
with {:anonymous_reporting_allowed, true} <-
|
|
|
|
{:anonymous_reporting_allowed, Config.anonymous_reporting?()},
|
|
|
|
{:wrong_id, true} <- {:wrong_id, reporter_id == to_string(Config.anonymous_actor_id())},
|
|
|
|
{:ok, _, %Report{} = report} <- API.Reports.report(args) do
|
|
|
|
{:ok, report}
|
|
|
|
else
|
|
|
|
{:anonymous_reporting_allowed, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create reports")}
|
2020-06-09 12:07:49 +00:00
|
|
|
|
|
|
|
{:wrong_id, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Reporter ID does not match the anonymous profile id")}
|
2020-06-09 12:07:49 +00:00
|
|
|
|
|
|
|
_error ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Error while saving report")}
|
2020-06-09 12:07:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-23 11:49:22 +00:00
|
|
|
def create_report(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in to create reports")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update a report's status
|
|
|
|
"""
|
|
|
|
def update_report(
|
|
|
|
_parent,
|
|
|
|
%{report_id: report_id, moderator_id: moderator_id, status: status},
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
2019-07-23 11:49:22 +00:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-09-07 17:54:11 +00:00
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, moderator_id),
|
2019-07-23 11:49:22 +00:00
|
|
|
%Report{} = report <- Mobilizon.Reports.get_report(report_id),
|
2020-01-26 20:11:16 +00:00
|
|
|
{:ok, %Report{} = report} <- API.Reports.update_report_status(actor, report, status) do
|
2019-07-23 11:49:22 +00:00
|
|
|
{:ok, report}
|
|
|
|
else
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-07-23 11:49:22 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
_error ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Error while updating report")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_report(_parent, _args, _resolution) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You need to be logged-in and a moderator to update a report")}
|
2019-07-23 11:49:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_report_note(
|
|
|
|
_parent,
|
|
|
|
%{report_id: report_id, moderator_id: moderator_id, content: content},
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
2019-07-23 11:49:22 +00:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-09-07 17:54:11 +00:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, moderator_id),
|
2019-07-23 11:49:22 +00:00
|
|
|
%Report{} = report <- Reports.get_report(report_id),
|
2019-09-08 22:52:49 +00:00
|
|
|
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
|
2020-01-26 20:11:16 +00:00
|
|
|
{:ok, %Note{} = note} <- API.Reports.create_report_note(report, moderator, content) do
|
2019-07-23 11:49:22 +00:00
|
|
|
{:ok, note}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
def delete_report_note(
|
|
|
|
_parent,
|
|
|
|
%{note_id: note_id, moderator_id: moderator_id},
|
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
|
|
|
)
|
2019-07-23 11:49:22 +00:00
|
|
|
when is_moderator(role) do
|
2019-09-07 17:54:11 +00:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, moderator_id),
|
2019-07-23 11:49:22 +00:00
|
|
|
%Note{} = note <- Reports.get_note(note_id),
|
2019-09-08 22:52:49 +00:00
|
|
|
%Actor{} = moderator <- Actors.get_local_actor_with_preload(moderator_id),
|
2020-01-26 20:11:16 +00:00
|
|
|
{:ok, %Note{} = note} <- API.Reports.delete_report_note(note, moderator) do
|
2019-07-23 11:49:22 +00:00
|
|
|
{:ok, %{id: note.id}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|