Redirect to profile creation when user has no identities

Also load persons by ID instead of preferred_username

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2019-10-04 18:28:25 +02:00
parent e34f304b76
commit 471e8ac472
15 changed files with 269 additions and 170 deletions

View File

@@ -12,15 +12,29 @@ defmodule MobilizonWeb.Resolvers.Person do
alias Mobilizon.Users.User
@doc """
Find a person
Get a person
"""
def find_person(_parent, %{preferred_username: name}, _resolution) do
with {:ok, actor} <- ActivityPub.find_or_make_person_from_nickname(name),
def get_person(_parent, %{id: id}, _resolution) do
with %Actor{} = actor <- Actors.get_actor_with_preload(id),
actor <- proxify_pictures(actor) do
{:ok, actor}
else
_ ->
{:error, "Person with name #{name} not found"}
{:error, "Person with ID #{id} not found"}
end
end
@doc """
Find a person
"""
def fetch_person(_parent, %{preferred_username: preferred_username}, _resolution) do
with {:ok, %Actor{} = actor} <-
ActivityPub.find_or_make_actor_from_nickname(preferred_username),
actor <- proxify_pictures(actor) do
{:ok, actor}
else
_ ->
{:error, "Person with username #{preferred_username} not found"}
end
end
@@ -74,13 +88,13 @@ defmodule MobilizonWeb.Resolvers.Person do
"""
def update_person(
_parent,
%{preferred_username: preferred_username} = args,
%{id: id} = args,
%{context: %{current_user: user}} = _resolution
) do
args = Map.put(args, :user_id, user.id)
with {:find_actor, %Actor{} = actor} <-
{:find_actor, Actors.get_actor_by_name(preferred_username)},
{:find_actor, Actors.get_actor(id)},
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
args <- save_attached_pictures(args),
{:ok, actor} <- Actors.update_actor(actor, args) do
@@ -103,11 +117,11 @@ defmodule MobilizonWeb.Resolvers.Person do
"""
def delete_person(
_parent,
%{preferred_username: preferred_username} = _args,
%{id: id} = _args,
%{context: %{current_user: user}} = _resolution
) do
with {:find_actor, %Actor{} = actor} <-
{:find_actor, Actors.get_actor_by_name(preferred_username)},
{:find_actor, Actors.get_actor(id)},
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
{:last_identity, false} <- {:last_identity, last_identity?(user)},
{:last_admin, false} <- {:last_admin, last_admin_of_a_group?(actor.id)},

View File

@@ -70,10 +70,16 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
resolve(&Person.get_current_person/3)
end
@desc "Get a person by it's preferred username"
field :person, :person do
@desc "Get a person by it's (federated) username"
field :fetch_person, :person do
arg(:preferred_username, non_null(:string))
resolve(&Person.find_person/3)
resolve(&Person.fetch_person/3)
end
@desc "Get a person by it's ID"
field :person, :person do
arg(:id, non_null(:id))
resolve(&Person.get_person/3)
end
@desc "Get the persons for an user"
@@ -106,7 +112,7 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
@desc "Update an identity"
field :update_person, :person do
arg(:preferred_username, non_null(:string))
arg(:id, non_null(:id))
arg(:name, :string, description: "The displayed name for this profile")
@@ -127,7 +133,7 @@ defmodule MobilizonWeb.Schema.Actors.PersonType do
@desc "Delete an identity"
field :delete_person, :person do
arg(:preferred_username, non_null(:string))
arg(:id, non_null(:id))
resolve(handle_errors(&Person.delete_person/3))
end