2018-12-03 10:58:57 +00:00
|
|
|
defmodule MobilizonWeb.Resolvers.Group do
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor}
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Find a group
|
|
|
|
"""
|
|
|
|
def find_group(_parent, %{preferred_username: name}, _resolution) do
|
|
|
|
case ActivityPub.find_or_make_group_from_nickname(name) do
|
|
|
|
{:ok, actor} ->
|
|
|
|
{:ok, actor}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, "Group with name #{name} not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Lists all groups
|
|
|
|
"""
|
2018-12-14 10:23:36 +00:00
|
|
|
def list_groups(_parent, %{page: page, limit: limit}, _resolution) do
|
|
|
|
{:ok, Actors.list_groups(page, limit)}
|
2018-12-03 10:58:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a new group. The creator is automatically added as admin
|
|
|
|
"""
|
|
|
|
def create_group(
|
|
|
|
_parent,
|
|
|
|
%{preferred_username: preferred_username, creator_username: actor_username},
|
|
|
|
%{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(actor_username),
|
|
|
|
{:user_actor, true} <-
|
|
|
|
{:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
|
2018-12-03 11:08:18 +00:00
|
|
|
{:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
|
|
|
|
{:ok, group}
|
2018-12-03 10:58:57 +00:00
|
|
|
else
|
|
|
|
{:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
|
|
|
|
{:error, :group_name_not_available}
|
2018-12-03 11:08:18 +00:00
|
|
|
|
2018-12-03 10:58:57 +00:00
|
|
|
err ->
|
|
|
|
Logger.error(inspect(err))
|
|
|
|
err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_group(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to create a group"}
|
|
|
|
end
|
|
|
|
end
|