2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Discussion do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the group-related GraphQL calls.
|
|
|
|
"""
|
|
|
|
|
2021-09-10 11:35:32 +02:00
|
|
|
alias Mobilizon.{Actors, Discussions}
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Discussions.{Comment, Discussion}
|
2021-09-28 19:40:37 +02:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actions
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.GraphQL.API.Comments
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec find_discussions_for_actor(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Discussion.t())} | {:error, :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
def find_discussions_for_actor(
|
|
|
|
%Actor{id: group_id},
|
2021-05-20 16:51:41 +02:00
|
|
|
%{page: page, limit: limit},
|
2020-07-09 17:24:28 +02:00
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: actor_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:member, true} <- {:member, Actors.is_member?(actor_id, group_id)},
|
2020-08-27 11:53:24 +02:00
|
|
|
{:ok, %Actor{type: :Group} = group} <- Actors.get_group_by_actor_id(group_id) do
|
2021-05-20 16:51:41 +02:00
|
|
|
{:ok, Discussions.find_discussions_for_actor(group, page, limit)}
|
2020-07-09 17:24:28 +02:00
|
|
|
else
|
|
|
|
{:member, false} ->
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-20 11:50:22 +02:00
|
|
|
def find_discussions_for_actor(%Actor{}, _args, _resolution) do
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_discussion(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Discussion.t()} | {:error, :unauthorized | :discussion_not_found | String.t()}
|
2020-07-09 17:24:28 +02:00
|
|
|
def get_discussion(_parent, %{id: id}, %{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: creator_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}) do
|
2021-09-28 19:40:37 +02:00
|
|
|
case Discussions.get_discussion(id) do
|
|
|
|
%Discussion{actor_id: actor_id} = discussion ->
|
|
|
|
if Actors.is_member?(creator_id, actor_id) do
|
|
|
|
{:ok, discussion}
|
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :discussion_not_found}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_discussion(_parent, %{slug: slug}, %{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: creator_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with %Discussion{actor_id: actor_id} = discussion <-
|
2020-07-09 17:24:28 +02:00
|
|
|
Discussions.get_discussion_by_slug(slug),
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)} do
|
|
|
|
{:ok, discussion}
|
|
|
|
else
|
2020-09-29 09:53:48 +02:00
|
|
|
nil -> {:error, dgettext("errors", "Discussion not found")}
|
2021-03-31 10:06:13 +02:00
|
|
|
{:member, false} -> {:error, :unauthorized}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
def get_discussion(_parent, _args, %{
|
|
|
|
context: %{
|
|
|
|
current_user: %User{} = _user
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
do:
|
|
|
|
{:error,
|
|
|
|
dgettext("errors", "You must provide either an ID or a slug to access a discussion")}
|
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
def get_discussion(_parent, _args, _resolution),
|
2020-09-29 09:53:48 +02:00
|
|
|
do: {:error, dgettext("errors", "You need to be logged-in to access discussions")}
|
2020-07-09 17:24:28 +02:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec get_comments_for_discussion(Discussion.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Discussion.t())}
|
2020-07-09 17:24:28 +02:00
|
|
|
def get_comments_for_discussion(
|
|
|
|
%Discussion{id: discussion_id},
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
_resolution
|
|
|
|
) do
|
|
|
|
{:ok, Discussions.get_comments_for_discussion(discussion_id, page, limit)}
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec create_discussion(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Discussion.t()}
|
|
|
|
| {:error, Ecto.Changeset.t() | String.t() | :unauthorized | :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
def create_discussion(
|
|
|
|
_parent,
|
2020-11-06 11:34:32 +01:00
|
|
|
%{title: title, text: text, actor_id: group_id},
|
2020-07-09 17:24:28 +02:00
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: creator_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2021-09-28 19:40:37 +02:00
|
|
|
if Actors.is_member?(creator_id, group_id) do
|
|
|
|
case Comments.create_discussion(%{
|
2020-11-26 11:41:13 +01:00
|
|
|
title: title,
|
|
|
|
text: text,
|
|
|
|
actor_id: group_id,
|
|
|
|
creator_id: creator_id,
|
|
|
|
attributed_to_id: group_id
|
|
|
|
}) do
|
2021-09-28 19:40:37 +02:00
|
|
|
{:ok, _activity, %Discussion{} = discussion} ->
|
|
|
|
{:ok, discussion}
|
2021-03-24 15:47:03 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
{:error, %Ecto.Changeset{} = err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
|
|
|
{:error, _err} ->
|
|
|
|
{:error, dgettext("errors", "Error while creating a discussion")}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, :unauthorized}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-04 11:43:35 +01:00
|
|
|
def create_discussion(_, _, _), do: {:error, :unauthenticated}
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec reply_to_discussion(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Discussion.t()} | {:error, :discussion_not_found | :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
def reply_to_discussion(
|
|
|
|
_parent,
|
|
|
|
%{text: text, discussion_id: discussion_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: creator_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:no_discussion,
|
2020-07-09 17:24:28 +02:00
|
|
|
%Discussion{
|
|
|
|
actor_id: actor_id,
|
|
|
|
last_comment: %Comment{
|
|
|
|
id: last_comment_id,
|
|
|
|
origin_comment_id: origin_comment_id,
|
|
|
|
in_reply_to_comment_id: previous_in_reply_to_comment_id
|
|
|
|
}
|
|
|
|
} = _discussion} <-
|
|
|
|
{:no_discussion, Discussions.get_discussion(discussion_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)},
|
|
|
|
{:ok, _activity, %Discussion{} = discussion} <-
|
2020-11-26 11:41:13 +01:00
|
|
|
Comments.create_discussion(%{
|
|
|
|
text: text,
|
|
|
|
discussion_id: discussion_id,
|
|
|
|
actor_id: creator_id,
|
|
|
|
attributed_to_id: actor_id,
|
|
|
|
in_reply_to_comment_id: last_comment_id,
|
|
|
|
origin_comment_id:
|
|
|
|
origin_comment_id || previous_in_reply_to_comment_id || last_comment_id
|
|
|
|
}) do
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, discussion}
|
2021-03-04 11:43:35 +01:00
|
|
|
else
|
|
|
|
{:no_discussion, _} ->
|
|
|
|
{:error, :discussion_not_found}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-04 11:43:35 +01:00
|
|
|
def reply_to_discussion(_, _, _), do: {:error, :unauthenticated}
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec update_discussion(map(), map(), map()) ::
|
|
|
|
{:ok, Discussion.t()} | {:error, :unauthorized | :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
def update_discussion(
|
|
|
|
_parent,
|
|
|
|
%{title: title, discussion_id: discussion_id},
|
|
|
|
%{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_actor: %Actor{id: creator_id}
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
|
2020-07-09 17:24:28 +02:00
|
|
|
{:no_discussion, Discussions.get_discussion(discussion_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)},
|
|
|
|
{:ok, _activity, %Discussion{} = discussion} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Update.update(
|
2020-07-09 17:24:28 +02:00
|
|
|
discussion,
|
|
|
|
%{
|
|
|
|
title: title
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
{:ok, discussion}
|
2021-03-04 11:43:35 +01:00
|
|
|
else
|
|
|
|
{:member, false} ->
|
|
|
|
{:error, :unauthorized}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-04 11:43:35 +01:00
|
|
|
def update_discussion(_, _, _), do: {:error, :unauthenticated}
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec delete_discussion(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Discussion.t()} | {:error, String.t() | :unauthorized | :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
def delete_discussion(_parent, %{discussion_id: discussion_id}, %{
|
|
|
|
context: %{
|
2021-09-10 11:35:32 +02:00
|
|
|
current_user: %User{},
|
|
|
|
current_actor: %Actor{id: creator_id} = actor
|
2020-07-09 17:24:28 +02:00
|
|
|
}
|
|
|
|
}) do
|
2021-09-10 11:35:32 +02:00
|
|
|
with {:no_discussion, %Discussion{actor_id: actor_id} = discussion} <-
|
2020-07-09 17:24:28 +02:00
|
|
|
{:no_discussion, Discussions.get_discussion(discussion_id)},
|
|
|
|
{:member, true} <- {:member, Actors.is_member?(creator_id, actor_id)},
|
|
|
|
{:ok, _activity, %Discussion{} = discussion} <-
|
2021-09-28 19:40:37 +02:00
|
|
|
Actions.Delete.delete(discussion, actor) do
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, discussion}
|
2020-08-27 11:53:24 +02:00
|
|
|
else
|
|
|
|
{:no_discussion, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "No discussion with ID %{id}", id: discussion_id)}
|
2020-08-27 11:53:24 +02:00
|
|
|
|
|
|
|
{:member, _} ->
|
2021-03-04 11:43:35 +01:00
|
|
|
{:error, :unauthorized}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|
|
|
|
end
|
2021-03-04 11:43:35 +01:00
|
|
|
|
|
|
|
def delete_discussion(_, _, _), do: {:error, :unauthenticated}
|
2020-07-09 17:24:28 +02:00
|
|
|
end
|