2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Person do
|
2019-01-03 14:59:59 +01:00
|
|
|
@moduledoc """
|
|
|
|
Handles the person-related GraphQL calls
|
|
|
|
"""
|
2019-09-09 00:52:49 +02:00
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
alias Mobilizon.Actors
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-03-21 20:23:42 +01:00
|
|
|
alias Mobilizon.Events
|
2019-09-26 16:38:58 +02:00
|
|
|
alias Mobilizon.Events.Participant
|
2020-06-11 19:13:21 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Users
|
|
|
|
alias Mobilizon.Users.User
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.{MediaProxy, Upload}
|
|
|
|
|
2019-10-04 18:28:25 +02:00
|
|
|
@doc """
|
|
|
|
Get a person
|
|
|
|
"""
|
2020-06-11 19:13:21 +02:00
|
|
|
def get_person(_parent, %{id: id}, %{context: %{current_user: %User{role: role}}}) do
|
|
|
|
with %Actor{suspended: suspended} = actor <- Actors.get_actor_with_preload(id, true),
|
|
|
|
true <- suspended == false or is_moderator(role),
|
2019-10-04 18:28:25 +02:00
|
|
|
actor <- proxify_pictures(actor) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
{:error, "Person with ID #{id} not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc """
|
|
|
|
Find a person
|
|
|
|
"""
|
2019-10-04 18:28:25 +02:00
|
|
|
def fetch_person(_parent, %{preferred_username: preferred_username}, _resolution) do
|
|
|
|
with {:ok, %Actor{} = actor} <-
|
|
|
|
ActivityPub.find_or_make_actor_from_nickname(preferred_username),
|
2019-05-28 10:51:02 +02:00
|
|
|
actor <- proxify_pictures(actor) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
2018-11-23 15:03:53 +01:00
|
|
|
_ ->
|
2019-10-04 18:28:25 +02:00
|
|
|
{:error, "Person with username #{preferred_username} not found"}
|
2018-11-23 15:03:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
def list_persons(
|
|
|
|
_parent,
|
|
|
|
%{
|
|
|
|
preferred_username: preferred_username,
|
|
|
|
name: name,
|
|
|
|
domain: domain,
|
|
|
|
local: local,
|
|
|
|
suspended: suspended,
|
|
|
|
page: page,
|
|
|
|
limit: limit
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
when is_moderator(role) do
|
|
|
|
{:ok,
|
|
|
|
Actors.list_actors(:Person, preferred_username, name, domain, local, suspended, page, limit)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_persons(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in and a moderator to list persons"}
|
|
|
|
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,
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-05-22 14:12:11 +02:00
|
|
|
) 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-06-17 17:15:27 +02:00
|
|
|
@doc """
|
|
|
|
This function is used to update an existing identity
|
|
|
|
"""
|
|
|
|
def update_person(
|
|
|
|
_parent,
|
2019-10-04 18:28:25 +02:00
|
|
|
%{id: id} = args,
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-06-17 17:15:27 +02:00
|
|
|
) do
|
|
|
|
args = Map.put(args, :user_id, user.id)
|
|
|
|
|
|
|
|
with {:find_actor, %Actor{} = actor} <-
|
2019-10-04 18:28:25 +02:00
|
|
|
{:find_actor, Actors.get_actor(id)},
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
|
2019-06-17 17:15:27 +02:00
|
|
|
args <- save_attached_pictures(args),
|
2020-07-09 17:24:28 +02:00
|
|
|
{:ok, _activity, %Actor{} = actor} <- ActivityPub.update(actor, args, true) do
|
2019-06-17 17:15:27 +02:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
{:find_actor, nil} ->
|
|
|
|
{:error, "Actor not found"}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-06-17 17:15:27 +02:00
|
|
|
{:error, "Actor is not owned by authenticated user"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_person(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to update an identity"}
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
This function is used to delete an existing identity
|
|
|
|
"""
|
|
|
|
def delete_person(
|
|
|
|
_parent,
|
2019-10-04 18:28:25 +02:00
|
|
|
%{id: id} = _args,
|
2019-09-07 19:54:11 +02:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-06-17 17:15:27 +02:00
|
|
|
) do
|
|
|
|
with {:find_actor, %Actor{} = actor} <-
|
2019-10-04 18:28:25 +02:00
|
|
|
{:find_actor, Actors.get_actor(id)},
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
|
2019-06-17 17:15:27 +02:00
|
|
|
{:last_identity, false} <- {:last_identity, last_identity?(user)},
|
2019-08-26 15:44:02 +02:00
|
|
|
{:last_admin, false} <- {:last_admin, last_admin_of_a_group?(actor.id)},
|
2019-06-17 17:15:27 +02:00
|
|
|
{:ok, actor} <- Actors.delete_actor(actor) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
{:find_actor, nil} ->
|
|
|
|
{:error, "Actor not found"}
|
|
|
|
|
|
|
|
{:last_identity, true} ->
|
|
|
|
{:error, "Cannot remove the last identity of a user"}
|
|
|
|
|
2019-08-26 15:44:02 +02:00
|
|
|
{:last_admin, true} ->
|
|
|
|
{:error, "Cannot remove the last administrator of a group"}
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-06-17 17:15:27 +02:00
|
|
|
{:error, "Actor is not owned by authenticated user"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_person(_parent, _args, _resolution) do
|
|
|
|
{:error, "You need to be logged-in to delete an identity"}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp last_identity?(user) do
|
|
|
|
length(Users.get_actors_for_user(user)) <= 1
|
|
|
|
end
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
defp save_attached_pictures(args) do
|
|
|
|
Enum.reduce([:avatar, :banner], args, fn key, args ->
|
2019-06-17 17:15:27 +02:00
|
|
|
if Map.has_key?(args, key) && !is_nil(args[key][:picture]) do
|
2019-05-22 14:12:11 +02:00
|
|
|
pic = args[key][:picture]
|
|
|
|
|
2019-10-25 17:43:37 +02:00
|
|
|
with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
|
2020-01-28 19:18:33 +01:00
|
|
|
Upload.store(pic.file, type: key, description: pic.alt) do
|
2019-05-22 14:12:11 +02:00
|
|
|
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),
|
2020-06-27 19:12:45 +02:00
|
|
|
user_actor <- Users.get_actor_for_user(user),
|
|
|
|
no_actor <- is_nil(user_actor),
|
|
|
|
{:no_actor, true} <- {:no_actor, no_actor},
|
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),
|
2020-06-27 19:12:45 +02:00
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args, true) do
|
2019-01-29 11:02:32 +01:00
|
|
|
{:ok, new_person}
|
2019-01-21 15:08:22 +01:00
|
|
|
else
|
2019-01-29 11:02:32 +01:00
|
|
|
{:error, :user_not_found} ->
|
2019-10-15 18:13:05 +02:00
|
|
|
{:error, "No user with this email was 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 """
|
2019-09-26 16:38:58 +02:00
|
|
|
Returns the participation for a specific event
|
2019-03-21 20:23:42 +01:00
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
def person_participations(
|
|
|
|
%Actor{id: actor_id},
|
|
|
|
%{event_id: event_id},
|
|
|
|
%{context: %{current_user: user}}
|
|
|
|
) do
|
2019-09-26 16:38:58 +02:00
|
|
|
with {:is_owned, %Actor{} = _actor} <- User.owns_actor(user, actor_id),
|
|
|
|
{:no_participant, {:ok, %Participant{} = participant}} <-
|
|
|
|
{:no_participant, Events.get_participant(event_id, actor_id)} do
|
2020-06-11 19:13:21 +02:00
|
|
|
{:ok, %Page{elements: [participant], total: 1}}
|
2019-03-21 20:23:42 +01:00
|
|
|
else
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2019-03-21 20:23:42 +01:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2019-09-26 16:38:58 +02:00
|
|
|
|
|
|
|
{:no_participant, _} ->
|
2020-06-11 19:13:21 +02:00
|
|
|
{:ok, %Page{elements: [], total: 0}}
|
2019-03-21 20:23:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events this person is going to
|
|
|
|
"""
|
2020-06-11 19:13:21 +02:00
|
|
|
def person_participations(%Actor{id: actor_id} = actor, %{page: page, limit: limit}, %{
|
|
|
|
context: %{current_user: %User{role: role} = user}
|
|
|
|
}) do
|
|
|
|
{:is_owned, actor_found} = User.owns_actor(user, actor_id)
|
|
|
|
|
|
|
|
res =
|
|
|
|
cond do
|
|
|
|
not is_nil(actor_found) ->
|
|
|
|
true
|
|
|
|
|
|
|
|
is_moderator(role) ->
|
|
|
|
true
|
|
|
|
|
|
|
|
true ->
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
with {:is_owned, true} <- {:is_owned, res},
|
|
|
|
%Page{} = page <- Events.list_event_participations_for_actor(actor, page, limit) do
|
|
|
|
{:ok, page}
|
2019-03-21 20:23:42 +01:00
|
|
|
else
|
2020-06-11 19:13:21 +02:00
|
|
|
{:is_owned, false} ->
|
2019-03-21 20:23:42 +01:00
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of events this person is going to
|
|
|
|
"""
|
|
|
|
def person_memberships(%Actor{id: actor_id}, _args, %{context: %{current_user: user}}) do
|
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
|
|
|
participations <- Actors.list_members_for_actor(actor) do
|
|
|
|
{:ok, participations}
|
|
|
|
else
|
|
|
|
{:is_owned, nil} ->
|
|
|
|
{:error, "Actor id is not owned by authenticated user"}
|
2019-03-21 20:23:42 +01:00
|
|
|
end
|
|
|
|
end
|
2019-05-28 10:51:02 +02:00
|
|
|
|
|
|
|
def proxify_pictures(%Actor{} = actor) do
|
|
|
|
actor
|
|
|
|
|> proxify_avatar
|
|
|
|
|> proxify_banner
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
def user_for_person(%Actor{type: :Person, user_id: user_id}, _args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when is_moderator(role) do
|
|
|
|
with false <- is_nil(user_id),
|
|
|
|
%User{} = user <- Users.get_user(user_id) do
|
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
true ->
|
|
|
|
{:ok, nil}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{:error, "User not found"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_for_person(_, _args, _resolution), do: {:error, nil}
|
|
|
|
|
|
|
|
def organized_events_for_person(
|
|
|
|
%Actor{user_id: actor_user_id} = actor,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{id: user_id, role: role}}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
with true <- actor_user_id == user_id or is_moderator(role),
|
|
|
|
%Page{} = page <- Events.list_organized_events_for_actor(actor, page, limit) do
|
|
|
|
{:ok, page}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-26 15:44:02 +02:00
|
|
|
# We check that the actor is not the last administrator/creator of a group
|
|
|
|
@spec last_admin_of_a_group?(integer()) :: boolean()
|
|
|
|
defp last_admin_of_a_group?(actor_id) do
|
2019-09-11 03:16:37 +02:00
|
|
|
length(Actors.list_group_ids_where_last_administrator(actor_id)) > 0
|
2019-08-26 15:44:02 +02:00
|
|
|
end
|
|
|
|
|
2019-05-28 10:51:02 +02:00
|
|
|
@spec proxify_avatar(Actor.t()) :: Actor.t()
|
|
|
|
defp proxify_avatar(%Actor{avatar: %{url: avatar_url} = avatar} = actor) do
|
2020-01-28 19:18:33 +01:00
|
|
|
actor |> Map.put(:avatar, avatar |> Map.put(:url, MediaProxy.url(avatar_url)))
|
2019-05-28 10:51:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec proxify_avatar(Actor.t()) :: Actor.t()
|
|
|
|
defp proxify_avatar(%Actor{} = actor), do: actor
|
|
|
|
|
|
|
|
@spec proxify_banner(Actor.t()) :: Actor.t()
|
|
|
|
defp proxify_banner(%Actor{banner: %{url: banner_url} = banner} = actor) do
|
2020-01-28 19:18:33 +01:00
|
|
|
actor |> Map.put(:banner, banner |> Map.put(:url, MediaProxy.url(banner_url)))
|
2019-05-28 10:51:02 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec proxify_banner(Actor.t()) :: Actor.t()
|
|
|
|
defp proxify_banner(%Actor{} = actor), do: actor
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|