1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-29 03:15:27 +00:00
mobilizon/lib/graphql/resolvers/comment.ex

112 lines
3.2 KiB
Elixir
Raw Normal View History

2020-01-26 19:34:25 +00:00
defmodule Mobilizon.GraphQL.Resolvers.Comment do
@moduledoc """
2019-09-22 14:26:23 +00:00
Handles the comment-related GraphQL calls.
"""
2019-09-12 23:35:03 +00:00
alias Mobilizon.{Actors, Admin, Discussions}
2020-01-22 01:14:42 +00:00
alias Mobilizon.Actors.Actor
alias Mobilizon.Discussions.Comment, as: CommentModel
alias Mobilizon.Users
2019-09-22 14:26:23 +00:00
alias Mobilizon.Users.User
2020-01-26 20:11:16 +00:00
alias Mobilizon.GraphQL.API.Comments
2019-09-12 23:35:03 +00:00
require Logger
def get_thread(_parent, %{id: thread_id}, _context) do
{:ok, Discussions.get_thread_replies(thread_id)}
end
2020-01-26 19:34:25 +00:00
def create_comment(
_parent,
%{actor_id: actor_id} = args,
%{
context: %{
current_user: %User{} = user
}
}
2020-01-26 19:34:25 +00:00
) do
with {:is_owned, %Actor{} = _organizer_actor} <- User.owns_actor(user, actor_id),
{: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, _context) do
{:error, "You are not allowed to create a comment if not connected"}
end
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 <-
Mobilizon.Discussions.get_comment_with_preload(comment_id),
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
{:error, "You are not allowed to update a comment if not connected"}
end
2020-01-26 19:34:25 +00:00
def delete_comment(
_parent,
%{comment_id: comment_id},
%{
context: %{
current_user: %User{role: role} = user
}
}
2020-01-26 19:34:25 +00:00
) do
with {:actor, %Actor{id: actor_id} = actor} <- {:actor, Users.get_actor_for_user(user)},
%CommentModel{deleted_at: nil} = comment <-
Discussions.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, actor)
role in [:moderator, :administrator] ->
with {:ok, res} <- do_delete_comment(comment, actor),
%Actor{} = actor <- Actors.get_actor(actor_id) do
2020-01-23 20:59:50 +00:00
Admin.log_action(actor, "delete", comment)
{:ok, res}
end
true ->
{:error, "You cannot delete this comment"}
end
else
%CommentModel{deleted_at: deleted_at} when not is_nil(deleted_at) ->
{:error, "Comment is already deleted"}
{: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, %Actor{} = actor) do
with {:ok, _, %CommentModel{} = comment} <-
Comments.delete_comment(comment, actor) do
{:ok, comment}
end
end
end