2019-07-23 13:49:22 +02:00
|
|
|
defmodule MobilizonWeb.Resolvers.Report do
|
|
|
|
@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
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Actors
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Reports
|
|
|
|
alias Mobilizon.Reports.{Note, Report}
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Users.User
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
alias MobilizonWeb.API.Reports, as: ReportsAPI
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
def list_reports(
|
|
|
|
_parent,
|
2019-09-21 23:59:07 +02:00
|
|
|
%{page: page, limit: limit, status: status},
|
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
|
2019-09-09 09:31:08 +02:00
|
|
|
{:ok, Mobilizon.Reports.list_reports(page, limit, :updated_at, :desc, status)}
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_reports(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in and a moderator to list reports"}
|
|
|
|
end
|
|
|
|
|
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 ->
|
|
|
|
{:error, "Report not found"}
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_report(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in and a moderator to view a report"}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a report
|
|
|
|
"""
|
|
|
|
def create_report(
|
|
|
|
_parent,
|
2019-11-15 18:36:47 +01:00
|
|
|
%{reporter_id: reporter_id} = args,
|
2019-07-23 13:49:22 +02:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
|
|
|
) do
|
2019-11-15 18:36:47 +01:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, reporter_id),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, _, %Report{} = report} <- ReportsAPI.report(args) do
|
|
|
|
{:ok, report}
|
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-07-23 13:49:22 +02:00
|
|
|
{:error, "Reporter actor id is not owned by authenticated user"}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
_error ->
|
2019-07-23 13:49:22 +02:00
|
|
|
{:error, "Error while saving report"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_report(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to create reports"}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Update a report's status
|
|
|
|
"""
|
|
|
|
def update_report(
|
|
|
|
_parent,
|
|
|
|
%{report_id: report_id, moderator_id: moderator_id, status: status},
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
2019-07-23 13:49:22 +02:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-09-07 19:54:11 +02:00
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, moderator_id),
|
2019-07-23 13:49:22 +02:00
|
|
|
%Report{} = report <- Mobilizon.Reports.get_report(report_id),
|
|
|
|
{:ok, %Report{} = report} <-
|
|
|
|
MobilizonWeb.API.Reports.update_report_status(actor, report, status) do
|
|
|
|
{:ok, report}
|
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-07-23 13:49:22 +02:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
_error ->
|
2019-07-23 13:49:22 +02:00
|
|
|
{:error, "Error while updating report"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_report(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in and a moderator to update a report"}
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_report_note(
|
|
|
|
_parent,
|
|
|
|
%{report_id: report_id, moderator_id: moderator_id, content: content},
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
2019-07-23 13:49:22 +02:00
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
2019-09-07 19:54:11 +02:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, moderator_id),
|
2019-07-23 13:49:22 +02:00
|
|
|
%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),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Note{} = note} <-
|
|
|
|
MobilizonWeb.API.Reports.create_report_note(report, moderator, content) do
|
|
|
|
{:ok, note}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
def delete_report_note(
|
|
|
|
_parent,
|
|
|
|
%{note_id: note_id, moderator_id: moderator_id},
|
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
|
|
|
)
|
2019-07-23 13:49:22 +02:00
|
|
|
when is_moderator(role) do
|
2019-09-07 19:54:11 +02:00
|
|
|
with {:is_owned, %Actor{}} <- User.owns_actor(user, moderator_id),
|
2019-07-23 13:49:22 +02:00
|
|
|
%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),
|
2019-07-23 13:49:22 +02:00
|
|
|
{:ok, %Note{} = note} <-
|
|
|
|
MobilizonWeb.API.Reports.delete_report_note(note, moderator) do
|
|
|
|
{:ok, %{id: note.id}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|