2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Comment do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
Handles the comment-related GraphQL calls.
|
2019-01-03 13:59:59 +00:00
|
|
|
"""
|
2019-09-12 23:35:03 +00:00
|
|
|
|
2020-11-19 16:06:28 +00:00
|
|
|
alias Mobilizon.{Actors, Admin, Discussions, Events, Users}
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Discussions.Comment, as: CommentModel
|
2020-10-05 15:42:53 +00:00
|
|
|
alias Mobilizon.Events.{Event, EventOptions}
|
2019-09-22 14:26:23 +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.Comments
|
2018-12-14 16:41:55 +00:00
|
|
|
|
2019-09-12 23:35:03 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-11-15 17:36:47 +00:00
|
|
|
def get_thread(_parent, %{id: thread_id}, _context) do
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, Discussions.get_thread_replies(thread_id)}
|
2019-11-15 17:36:47 +00:00
|
|
|
end
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
def create_comment(
|
|
|
|
_parent,
|
2020-11-19 16:06:28 +00:00
|
|
|
%{event_id: event_id} = args,
|
2020-02-18 07:57:00 +00:00
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = user
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 19:34:25 +00:00
|
|
|
) do
|
2020-11-19 16:06:28 +00:00
|
|
|
with %Actor{id: actor_id} <- Users.get_actor_for_user(user),
|
2020-10-05 15:42:53 +00:00
|
|
|
{:find_event,
|
|
|
|
{:ok,
|
|
|
|
%Event{
|
|
|
|
options: %EventOptions{comment_moderation: comment_moderation},
|
|
|
|
organizer_actor_id: organizer_actor_id
|
|
|
|
}}} <-
|
|
|
|
{:find_event, Events.get_event(event_id)},
|
|
|
|
{:allowed, true} <-
|
|
|
|
{:allowed, comment_moderation != :closed || actor_id == organizer_actor_id},
|
2020-11-19 16:06:28 +00:00
|
|
|
args <- Map.put(args, :actor_id, actor_id),
|
2019-11-15 17:36:47 +00:00
|
|
|
{:ok, _, %CommentModel{} = comment} <-
|
|
|
|
Comments.create_comment(args) do
|
2019-07-30 08:35:29 +00:00
|
|
|
{:ok, comment}
|
2019-11-15 17:36:47 +00:00
|
|
|
else
|
2021-03-25 11:11:49 +00:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
2020-10-05 15:42:53 +00:00
|
|
|
{:allowed, false} ->
|
|
|
|
{:error, :unauthorized}
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-15 17:36:47 +00:00
|
|
|
def create_comment(_parent, _args, _context) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You are not allowed to create a comment if not connected")}
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
2019-11-15 17:36:47 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
def update_comment(
|
|
|
|
_parent,
|
|
|
|
%{text: text, comment_id: comment_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
with {:actor, %Actor{id: actor_id} = _actor} <- {:actor, Users.get_actor_for_user(user)},
|
|
|
|
%CommentModel{actor_id: comment_actor_id} = comment <-
|
2020-08-14 09:32:23 +00:00
|
|
|
Mobilizon.Discussions.get_comment_with_preload(comment_id),
|
2020-02-18 07:57:00 +00:00
|
|
|
true <- actor_id === comment_actor_id,
|
|
|
|
{:ok, _, %CommentModel{} = comment} <- Comments.update_comment(comment, %{text: text}) do
|
|
|
|
{:ok, comment}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit_comment(_parent, _args, _context) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You are not allowed to update a comment if not connected")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2020-01-26 19:34:25 +00:00
|
|
|
def delete_comment(
|
|
|
|
_parent,
|
2020-08-14 09:32:23 +00:00
|
|
|
%{comment_id: comment_id},
|
2020-02-18 07:57:00 +00:00
|
|
|
%{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{role: role} = user
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 19:34:25 +00:00
|
|
|
) do
|
2020-08-14 09:32:23 +00:00
|
|
|
with {:actor, %Actor{id: actor_id} = actor} <- {:actor, Users.get_actor_for_user(user)},
|
2020-06-15 14:20:58 +00:00
|
|
|
%CommentModel{deleted_at: nil} = comment <-
|
2020-07-09 15:24:28 +00:00
|
|
|
Discussions.get_comment_with_preload(comment_id) do
|
2019-11-15 17:36:47 +00:00
|
|
|
cond do
|
|
|
|
{:comment_can_be_managed, true} == CommentModel.can_be_managed_by(comment, actor_id) ->
|
2020-07-09 15:24:28 +00:00
|
|
|
do_delete_comment(comment, actor)
|
2019-11-15 17:36:47 +00:00
|
|
|
|
|
|
|
role in [:moderator, :administrator] ->
|
2020-07-09 15:24:28 +00:00
|
|
|
with {:ok, res} <- do_delete_comment(comment, actor),
|
2019-11-15 17:36:47 +00:00
|
|
|
%Actor{} = actor <- Actors.get_actor(actor_id) do
|
2020-01-23 20:59:50 +00:00
|
|
|
Admin.log_action(actor, "delete", comment)
|
2019-11-15 17:36:47 +00:00
|
|
|
|
|
|
|
{:ok, res}
|
|
|
|
end
|
|
|
|
|
|
|
|
true ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You cannot delete this comment")}
|
2019-11-15 17:36:47 +00:00
|
|
|
end
|
|
|
|
else
|
2020-06-15 14:20:58 +00:00
|
|
|
%CommentModel{deleted_at: deleted_at} when not is_nil(deleted_at) ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Comment is already deleted")}
|
2019-11-15 17:36:47 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_comment(_parent, _args, %{}) do
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You are not allowed to delete a comment if not connected")}
|
2019-11-15 17:36:47 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
defp do_delete_comment(%CommentModel{} = comment, %Actor{} = actor) do
|
2019-11-15 17:36:47 +00:00
|
|
|
with {:ok, _, %CommentModel{} = comment} <-
|
2020-07-09 15:24:28 +00:00
|
|
|
Comments.delete_comment(comment, actor) do
|
2019-11-15 17:36:47 +00:00
|
|
|
{:ok, comment}
|
|
|
|
end
|
|
|
|
end
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|