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-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Users.User
|
|
|
|
alias Mobilizon.Users
|
2019-03-21 20:23:42 +01:00
|
|
|
alias Mobilizon.Events
|
2018-11-07 16:09:28 +01:00
|
|
|
alias Mobilizon.Service.ActivityPub
|
2018-11-06 10:30:27 +01:00
|
|
|
|
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
|
2019-03-05 17:23:05 +01:00
|
|
|
{:ok, Users.get_actor_for_user(user)}
|
2018-11-06 10:30:27 +01:00
|
|
|
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
|
2019-03-05 17:23:05 +01:00
|
|
|
{:ok, Users.get_actors_for_user(user)}
|
2019-01-21 15:08:22 +01:00
|
|
|
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-05-22 14:12:11 +02:00
|
|
|
def create_person(
|
|
|
|
_parent,
|
|
|
|
%{preferred_username: _preferred_username} = args,
|
|
|
|
%{
|
|
|
|
context: %{current_user: user}
|
|
|
|
} = _resolution
|
|
|
|
) do
|
2019-01-21 15:08:22 +01:00
|
|
|
args = Map.put(args, :user_id, user.id)
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
with args <- save_attached_pictures(args),
|
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
2019-01-21 15:08:22 +01:00
|
|
|
{:ok, new_person}
|
2019-01-29 11:02:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-22 18:07:20 +01:00
|
|
|
@doc """
|
|
|
|
This function is used to create more identities from an existing user
|
|
|
|
"""
|
|
|
|
def create_person(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to create a new identity"}
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
defp save_attached_pictures(args) do
|
|
|
|
Enum.reduce([:avatar, :banner], args, fn key, args ->
|
|
|
|
if Map.has_key?(args, key) do
|
|
|
|
pic = args[key][:picture]
|
|
|
|
|
|
|
|
with {:ok, %{"name" => name, "url" => [%{"href" => url, "mediaType" => content_type}]}} <-
|
|
|
|
MobilizonWeb.Upload.store(pic.file, type: key, description: pic.alt) do
|
|
|
|
Map.put(args, key, %{"name" => name, "url" => url, "mediaType" => content_type})
|
|
|
|
end
|
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
@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
|
2019-03-05 17:23:05 +01:00
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(args.email),
|
|
|
|
{:no_actor, nil} <- {:no_actor, Users.get_actor_for_user(user)},
|
2019-01-29 11:02:32 +01:00
|
|
|
args <- Map.put(args, :user_id, user.id),
|
2019-05-22 14:12:11 +02:00
|
|
|
args <- save_attached_pictures(args),
|
2019-01-29 11:02:32 +01:00
|
|
|
{: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"}
|
2019-01-30 15:54:21 +01:00
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
{:no_actor, _} ->
|
|
|
|
{:error, "You already have a profile for this user"}
|
2019-01-30 15:54:21 +01:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = e} ->
|
|
|
|
{:error, e}
|
2019-01-21 15:08:22 +01:00
|
|
|
end
|
|
|
|
end
|
2019-03-21 20:23:42 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events this person is going to
|
|
|
|
"""
|
|
|
|
def person_going_to_events(%Actor{id: actor_id}, _args, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
|
|
|
with {:is_owned, true, actor} <- User.owns_actor(user, actor_id),
|
|
|
|
events <- Events.list_event_participations_for_actor(actor) do
|
|
|
|
{:ok, events}
|
|
|
|
else
|
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events this person is going to
|
|
|
|
"""
|
|
|
|
def person_going_to_events(_parent, %{}, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
|
|
|
with %Actor{} = actor <- Users.get_actor_for_user(user),
|
|
|
|
events <- Events.list_event_participations_for_actor(actor) do
|
|
|
|
{:ok, events}
|
|
|
|
else
|
|
|
|
{:is_owned, false} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
|
|
|
end
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|