2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Comment do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Handles the comment-related GraphQL calls.
|
2019-01-03 14:59:59 +01:00
|
|
|
"""
|
2019-09-13 01:35:03 +02:00
|
|
|
|
2020-01-23 21:59:50 +01:00
|
|
|
alias Mobilizon.{Actors, Admin, Events}
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-11-15 18:36:47 +01:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.Comment, as: CommentModel
|
2019-09-22 16:26:23 +02:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
2020-01-26 21:11:16 +01:00
|
|
|
alias Mobilizon.GraphQL.API.Comments
|
2018-12-14 17:41:55 +01:00
|
|
|
|
2019-09-13 01:35:03 +02:00
|
|
|
require Logger
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
def get_thread(_parent, %{id: thread_id}, _context) do
|
|
|
|
{:ok, Events.get_thread_replies(thread_id)}
|
|
|
|
end
|
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def create_comment(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: actor_id} = args,
|
|
|
|
%{context: %{current_user: %User{} = user}}
|
|
|
|
) do
|
2019-10-25 17:43:37 +02:00
|
|
|
with {:is_owned, %Actor{} = _organizer_actor} <- User.owns_actor(user, actor_id),
|
2019-11-15 18:36:47 +01:00
|
|
|
{:ok, _, %CommentModel{} = comment} <-
|
|
|
|
Comments.create_comment(args) do
|
2019-07-30 10:35:29 +02:00
|
|
|
{:ok, comment}
|
2019-11-15 18:36:47 +01:00
|
|
|
else
|
|
|
|
{:is_owned, nil} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
def create_comment(_parent, _args, _context) do
|
2018-12-14 17:41:55 +01:00
|
|
|
{:error, "You are not allowed to create a comment if not connected"}
|
|
|
|
end
|
2019-11-15 18:36:47 +01:00
|
|
|
|
2020-01-26 20:34:25 +01:00
|
|
|
def delete_comment(
|
|
|
|
_parent,
|
|
|
|
%{actor_id: actor_id, comment_id: comment_id},
|
|
|
|
%{context: %{current_user: %User{role: role} = user}}
|
|
|
|
) do
|
2019-11-15 18:36:47 +01:00
|
|
|
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
|
2020-01-23 21:59:50 +01:00
|
|
|
Admin.log_action(actor, "delete", comment)
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
{: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
|
2018-12-14 17:41:55 +01:00
|
|
|
end
|