Introduce comments below events

Also add tomstones

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-11-15 18:36:47 +01:00
parent 45155a3bde
commit dc07f34d78
71 changed files with 2642 additions and 879 deletions

View File

@@ -7,7 +7,7 @@ defmodule MobilizonWeb.Resolvers.Admin do
alias Mobilizon.Admin.ActionLog
alias Mobilizon.Events
alias Mobilizon.Events.Event
alias Mobilizon.Events.{Event, Comment}
alias Mobilizon.Reports.{Note, Report}
alias Mobilizon.Service.Statistics
alias Mobilizon.Users.User
@@ -90,6 +90,15 @@ defmodule MobilizonWeb.Resolvers.Admin do
}
end
defp transform_action_log(Comment, :delete, %ActionLog{
changes: changes
}) do
%{
action: :comment_deletion,
object: convert_changes_to_struct(Comment, changes)
}
end
# Changes are stored as %{"key" => "value"} so we need to convert them back as struct
defp convert_changes_to_struct(struct, %{"report_id" => _report_id} = changes) do
with data <- for({key, val} <- changes, into: %{}, do: {String.to_atom(key), val}),

View File

@@ -3,25 +3,73 @@ defmodule MobilizonWeb.Resolvers.Comment do
Handles the comment-related GraphQL calls.
"""
alias Mobilizon.Events.Comment
alias Mobilizon.Events
alias Mobilizon.Events.Comment, as: CommentModel
alias Mobilizon.Users.User
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors
alias MobilizonWeb.API.Comments
import Mobilizon.Service.Admin.ActionLogService
require Logger
def create_comment(_parent, %{text: text, actor_id: actor_id}, %{
def get_thread(_parent, %{id: thread_id}, _context) do
{:ok, Events.get_thread_replies(thread_id)}
end
def create_comment(_parent, %{actor_id: actor_id} = args, %{
context: %{current_user: %User{} = user}
}) do
with {:is_owned, %Actor{} = _organizer_actor} <- User.owns_actor(user, actor_id),
{:ok, _, %Comment{} = comment} <-
Comments.create_comment(%{actor_id: actor_id, text: text}) do
{:ok, _, %CommentModel{} = comment} <-
Comments.create_comment(args) do
{:ok, comment}
else
{:is_owned, nil} ->
{:error, "Actor id is not owned by authenticated user"}
end
end
def create_comment(_parent, _args, %{}) do
def create_comment(_parent, _args, _context) do
{:error, "You are not allowed to create a comment if not connected"}
end
def delete_comment(_parent, %{actor_id: actor_id, comment_id: comment_id}, %{
context: %{current_user: %User{role: role} = user}
}) do
with {actor_id, ""} <- Integer.parse(actor_id),
{:is_owned, %Actor{} = _organizer_actor} <- User.owns_actor(user, actor_id),
%CommentModel{} = comment <- Events.get_comment_with_preload(comment_id) do
cond do
{:comment_can_be_managed, true} == CommentModel.can_be_managed_by(comment, actor_id) ->
do_delete_comment(comment)
role in [:moderator, :administrator] ->
with {:ok, res} <- do_delete_comment(comment),
%Actor{} = actor <- Actors.get_actor(actor_id) do
log_action(actor, "delete", comment)
{:ok, res}
end
true ->
{:error, "You cannot delete this comment"}
end
else
{:is_owned, nil} ->
{:error, "Actor id is not owned by authenticated user"}
end
end
def delete_comment(_parent, _args, %{}) do
{:error, "You are not allowed to delete a comment if not connected"}
end
defp do_delete_comment(%CommentModel{} = comment) do
with {:ok, _, %CommentModel{} = comment} <-
Comments.delete_comment(comment) do
{:ok, comment}
end
end
end

View File

@@ -46,10 +46,10 @@ defmodule MobilizonWeb.Resolvers.Report do
"""
def create_report(
_parent,
%{reporter_actor_id: reporter_actor_id} = args,
%{reporter_id: reporter_id} = args,
%{context: %{current_user: user}} = _resolution
) do
with {:is_owned, %Actor{}} <- User.owns_actor(user, reporter_actor_id),
with {:is_owned, %Actor{}} <- User.owns_actor(user, reporter_id),
{:ok, _, %Report{} = report} <- ReportsAPI.report(args) do
{:ok, report}
else