Prepare create group

This commit is contained in:
Chocobozzz
2019-09-02 10:50:00 +02:00
parent 4a8f971443
commit 18b2854d99
11 changed files with 257 additions and 120 deletions

View File

@@ -3,7 +3,7 @@ defmodule MobilizonWeb.API.Groups do
API for Events
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Users.User
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
alias MobilizonWeb.API.Utils
@@ -11,24 +11,26 @@ defmodule MobilizonWeb.API.Groups do
@doc """
Create a group
"""
@spec create_group(map()) :: {:ok, Activity.t(), Group.t()} | any()
@spec create_group(User.t(), map()) :: {:ok, Activity.t(), Group.t()} | any()
def create_group(
user,
%{
preferred_username: title,
description: description,
admin_actor_username: admin_actor_username
summary: summary,
creator_actor_id: creator_actor_id,
avatar: avatar,
banner: banner
} = args
) do
with {:bad_actor, %Actor{url: url} = actor} <-
{:bad_actor, Actors.get_local_actor_by_name(admin_actor_username)},
with {:is_owned, true, actor} <- User.owns_actor(user, creator_actor_id),
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
title <- String.trim(title),
visibility <- Map.get(args, :visibility, :public),
{content_html, tags, to, cc} <-
Utils.prepare_content(actor, description, visibility, [], nil),
Utils.prepare_content(actor, summary, visibility, [], nil),
group <-
ActivityPubUtils.make_group_data(
url,
actor.url,
to,
title,
content_html,
@@ -43,10 +45,10 @@ defmodule MobilizonWeb.API.Groups do
})
else
{:existing_group, _} ->
{:error, :existing_group_name}
{:error, "A group with this name already exists"}
{:bad_actor, _} ->
{:error, :bad_admin_actor}
{:is_owned, _} ->
{:error, "Actor id is not owned by authenticated user"}
end
end
end

View File

@@ -27,8 +27,11 @@ defmodule MobilizonWeb.Resolvers.Group do
Lists all groups
"""
def list_groups(_parent, %{page: page, limit: limit}, _resolution) do
{:ok,
Actors.list_groups(page, limit) |> Enum.map(fn actor -> Person.proxify_pictures(actor) end)}
{
:ok,
Actors.list_groups(page, limit)
|> Enum.map(fn actor -> Person.proxify_pictures(actor) end)
}
end
@doc """
@@ -39,7 +42,7 @@ defmodule MobilizonWeb.Resolvers.Group do
args,
%{
context: %{
current_user: _user
current_user: user
}
}
) do
@@ -52,26 +55,22 @@ defmodule MobilizonWeb.Resolvers.Group do
},
%Actor{} = group
} <-
MobilizonWeb.API.Groups.create_group(args) do
MobilizonWeb.API.Groups.create_group(
user,
%{
preferred_username: args.preferred_username,
creator_actor_id: args.creator_actor_id,
name: Map.get(args, "name", args.preferred_username),
summary: args.summary,
avatar: Map.get(args, "avatar"),
banner: Map.get(args, "banner")
}
) do
{
:ok,
group
}
end
# 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)},
# {:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
# {:ok, group}
# else
# {:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
# {:error, :group_name_not_available}
# err ->
# Logger.error(inspect(err))
# err
# end
end
def create_group(_parent, _args, _resolution) do
@@ -138,12 +137,18 @@ defmodule MobilizonWeb.Resolvers.Group do
actor_id: actor.id,
role: role
}) do
{:ok,
%{
parent: group |> Person.proxify_pictures(),
actor: actor |> Person.proxify_pictures(),
role: role
}}
{
:ok,
%{
parent:
group
|> Person.proxify_pictures(),
actor:
actor
|> Person.proxify_pictures(),
role: role
}
}
else
{:is_owned, false} ->
{:error, "Actor id is not owned by authenticated user"}

View File

@@ -95,13 +95,14 @@ defmodule MobilizonWeb.Schema.Actors.GroupType do
@desc "Create a group"
field :create_group, :group do
arg(:preferred_username, non_null(:string), description: "The name for the group")
arg(:name, :string, description: "The displayed name for the group")
arg(:description, :string, description: "The summary for the group", default_value: "")
arg(:admin_actor_username, :string,
description: "The actor's username which will be the admin (otherwise user's default one)"
arg(:creator_actor_id, non_null(:integer),
description: "The identity that creates the group"
)
arg(:name, :string, description: "The displayed name for the group")
arg(:summary, :string, description: "The summary for the group", default_value: "")
arg(:avatar, :picture_input,
description:
"The avatar for the group, either as an object or directly the ID of an existing Picture"

View File

@@ -72,7 +72,7 @@ defmodule MobilizonWeb.Schema.EventType do
@desc "The list of visibility options for an event"
enum :event_visibility do
value(:public, description: "Publically listed and federated. Can be shared.")
value(:public, description: "Publicly listed and federated. Can be shared.")
value(:unlisted, description: "Visible only to people with the link - or invited")
value(:private,