2018-12-03 11:58:57 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.Person do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
|
|
|
Handles the person-related GraphQL calls
|
|
|
|
"""
|
2018-11-06 10:30:27 +01:00
|
|
|
alias Mobilizon.Actors
|
2019-01-29 11:02:32 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, User}
|
2018-11-07 16:09:28 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-12-03 11:58:57 +01:00
|
|
|
@deprecated "Use find_person/3 or find_group/3 instead"
|
2018-11-06 10:30:27 +01:00
|
|
|
def find_actor(_parent, %{preferred_username: name}, _resolution) do
|
2018-11-07 16:09:28 +01:00
|
|
|
case ActivityPub.find_or_make_actor_from_nickname(name) do
|
|
|
|
{:ok, actor} ->
|
2018-11-06 10:30:27 +01:00
|
|
|
{:ok, actor}
|
2018-11-07 16:09:42 +01:00
|
|
|
|
2018-11-07 16:09:28 +01:00
|
|
|
_ ->
|
|
|
|
{:error, "Actor with name #{name} not found"}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc """
|
|
|
|
Find a person
|
|
|
|
"""
|
|
|
|
def find_person(_parent, %{preferred_username: name}, _resolution) do
|
|
|
|
case ActivityPub.find_or_make_person_from_nickname(name) do
|
|
|
|
{:ok, actor} ->
|
|
|
|
{:ok, actor}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, "Person with name #{name} not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
@doc """
|
|
|
|
Returns the current actor for the currently logged-in user
|
|
|
|
"""
|
2018-11-23 15:03:53 +01:00
|
|
|
def get_current_person(_parent, _args, %{context: %{current_user: user}}) do
|
2018-11-06 10:30:27 +01:00
|
|
|
{:ok, Actors.get_actor_for_user(user)}
|
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
def get_current_person(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to view current person"}
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|
2019-01-21 15:08:22 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of identities for the logged-in user
|
|
|
|
"""
|
|
|
|
def identities(_parent, _args, %{context: %{current_user: user}}) do
|
|
|
|
{:ok, Actors.get_actors_for_user(user)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def identities(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to view your list of identities"}
|
|
|
|
end
|
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
@doc """
|
|
|
|
This function is used to create more identities from an existing user
|
|
|
|
"""
|
2019-01-24 15:23:27 +01:00
|
|
|
def create_person(_parent, %{preferred_username: _preferred_username} = args, %{
|
2019-01-21 15:08:22 +01:00
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
|
|
|
args = Map.put(args, :user_id, user.id)
|
|
|
|
|
|
|
|
with {:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
|
|
|
{:ok, new_person}
|
2019-01-29 11:02:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
This function is used to register a person afterwards the user has been created (but not activated)
|
|
|
|
"""
|
|
|
|
def register_person(_parent, args, _resolution) do
|
|
|
|
with {:ok, %User{} = user} <- Actors.get_user_by_email(args.email),
|
|
|
|
{:no_actor, nil} <- {:no_actor, Actors.get_actor_for_user(user)},
|
|
|
|
args <- Map.put(args, :user_id, user.id),
|
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
|
|
|
{:ok, new_person}
|
2019-01-21 15:08:22 +01:00
|
|
|
else
|
2019-01-29 11:02:32 +01:00
|
|
|
{:error, :user_not_found} ->
|
|
|
|
{:error, "User with email not found"}
|
|
|
|
{:no_actor, _} ->
|
|
|
|
{:error, "You already have a profile for this user"}
|
2019-01-21 15:08:22 +01:00
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|