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