2020-01-26 19:34:25 +00:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Person do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the person-related GraphQL calls
|
|
|
|
"""
|
2019-09-08 22:52:49 +00:00
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
import Mobilizon.Users.Guards
|
|
|
|
|
2020-11-19 16:06:28 +00:00
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2019-09-08 22:52:49 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-26 14:38:58 +00:00
|
|
|
alias Mobilizon.Events.Participant
|
2020-06-11 17:13:21 +00:00
|
|
|
alias Mobilizon.Storage.Page
|
2019-09-08 22:52:49 +00:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 07:53:48 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-01-22 01:14:42 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-11-26 10:41:13 +00:00
|
|
|
require Logger
|
2020-01-22 01:14:42 +00:00
|
|
|
|
2020-12-15 16:17:42 +00:00
|
|
|
alias Mobilizon.Web.Upload
|
2020-01-28 18:18:33 +00:00
|
|
|
|
2019-10-04 16:28:25 +00:00
|
|
|
@doc """
|
|
|
|
Get a person
|
|
|
|
"""
|
2020-06-11 17:13:21 +00: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),
|
2020-12-15 16:17:42 +00:00
|
|
|
true <- suspended == false or is_moderator(role) do
|
2019-10-04 16:28:25 +00:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
_ ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Person with ID %{id} not found", id: id)}
|
2019-10-04 16:28:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 16:17:42 +00:00
|
|
|
def get_person(_parent, _args, _resolution), do: {:error, :unauthorized}
|
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
@doc """
|
|
|
|
Find a person
|
|
|
|
"""
|
2020-10-02 07:52:47 +00:00
|
|
|
def fetch_person(_parent, %{preferred_username: preferred_username}, %{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}) do
|
|
|
|
with {:ok, %Actor{id: actor_id} = actor} <-
|
2019-10-04 16:28:25 +00:00
|
|
|
ActivityPub.find_or_make_actor_from_nickname(preferred_username),
|
2020-12-15 16:17:42 +00:00
|
|
|
{:own, {:is_owned, _}} <- {:own, User.owns_actor(user, actor_id)} do
|
2019-05-28 08:51:02 +00:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
2020-10-02 07:52:47 +00:00
|
|
|
{:own, nil} ->
|
|
|
|
{:error, :unauthorized}
|
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
_ ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "Person with username %{username} not found",
|
|
|
|
username: preferred_username
|
|
|
|
)}
|
2018-11-23 14:03:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
def fetch_person(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
2020-06-11 17:13:21 +00: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
|
|
|
|
|
2020-10-02 07:52:47 +00:00
|
|
|
def list_persons(_parent, _args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when not is_moderator(role) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
2020-06-11 17:13:21 +00:00
|
|
|
def list_persons(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2020-06-11 17:13:21 +00:00
|
|
|
end
|
|
|
|
|
2018-11-06 09:30:27 +00:00
|
|
|
@doc """
|
|
|
|
Returns the current actor for the currently logged-in user
|
|
|
|
"""
|
2018-11-23 14:03:53 +00:00
|
|
|
def get_current_person(_parent, _args, %{context: %{current_user: user}}) do
|
2019-03-05 16:23:05 +00:00
|
|
|
{:ok, Users.get_actor_for_user(user)}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
|
|
|
|
2018-11-23 14:03:53 +00:00
|
|
|
def get_current_person(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|
2019-01-21 14:08:22 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the list of identities for the logged-in user
|
|
|
|
"""
|
|
|
|
def identities(_parent, _args, %{context: %{current_user: user}}) do
|
2019-03-05 16:23:05 +00:00
|
|
|
{:ok, Users.get_actors_for_user(user)}
|
2019-01-21 14:08:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def identities(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2019-01-21 14:08:22 +00:00
|
|
|
end
|
|
|
|
|
2019-01-29 10:02:32 +00:00
|
|
|
@doc """
|
|
|
|
This function is used to create more identities from an existing user
|
|
|
|
"""
|
2019-05-22 12:12:11 +00:00
|
|
|
def create_person(
|
|
|
|
_parent,
|
|
|
|
%{preferred_username: _preferred_username} = args,
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-05-22 12:12:11 +00:00
|
|
|
) do
|
2019-01-21 14:08:22 +00:00
|
|
|
args = Map.put(args, :user_id, user.id)
|
|
|
|
|
2020-11-17 18:14:55 +00:00
|
|
|
with args <- Map.update(args, :preferred_username, "", &String.downcase/1),
|
2020-12-15 16:17:42 +00:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
|
2019-05-22 12:12:11 +00:00
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
2019-01-21 14:08:22 +00:00
|
|
|
{:ok, new_person}
|
2020-12-15 16:17:42 +00:00
|
|
|
else
|
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
2019-01-29 10:02:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-22 17:07:20 +00:00
|
|
|
def create_person(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2019-02-22 17:07:20 +00:00
|
|
|
end
|
|
|
|
|
2019-06-17 15:15:27 +00:00
|
|
|
@doc """
|
|
|
|
This function is used to update an existing identity
|
|
|
|
"""
|
|
|
|
def update_person(
|
|
|
|
_parent,
|
2019-10-04 16:28:25 +00:00
|
|
|
%{id: id} = args,
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-06-17 15:15:27 +00:00
|
|
|
) do
|
2020-11-26 10:41:13 +00:00
|
|
|
require Logger
|
2019-06-17 15:15:27 +00:00
|
|
|
args = Map.put(args, :user_id, user.id)
|
|
|
|
|
|
|
|
with {:find_actor, %Actor{} = actor} <-
|
2019-10-04 16:28:25 +00:00
|
|
|
{:find_actor, Actors.get_actor(id)},
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
|
2020-12-15 16:17:42 +00:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
|
2020-07-09 15:24:28 +00:00
|
|
|
{:ok, _activity, %Actor{} = actor} <- ActivityPub.update(actor, args, true) do
|
2019-06-17 15:15:27 +00:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
2020-12-15 16:17:42 +00:00
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
|
2019-06-17 15:15:27 +00:00
|
|
|
{:find_actor, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile not found")}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_person(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
This function is used to delete an existing identity
|
|
|
|
"""
|
|
|
|
def delete_person(
|
|
|
|
_parent,
|
2019-10-04 16:28:25 +00:00
|
|
|
%{id: id} = _args,
|
2019-09-07 17:54:11 +00:00
|
|
|
%{context: %{current_user: user}} = _resolution
|
2019-06-17 15:15:27 +00:00
|
|
|
) do
|
|
|
|
with {:find_actor, %Actor{} = actor} <-
|
2019-10-04 16:28:25 +00:00
|
|
|
{:find_actor, Actors.get_actor(id)},
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id),
|
2019-06-17 15:15:27 +00:00
|
|
|
{:last_identity, false} <- {:last_identity, last_identity?(user)},
|
2019-08-26 13:44:02 +00:00
|
|
|
{:last_admin, false} <- {:last_admin, last_admin_of_a_group?(actor.id)},
|
2019-06-17 15:15:27 +00:00
|
|
|
{:ok, actor} <- Actors.delete_actor(actor) do
|
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
{:find_actor, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile not found")}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
|
|
|
{:last_identity, true} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Cannot remove the last identity of a user")}
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2019-08-26 13:44:02 +00:00
|
|
|
{:last_admin, true} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Cannot remove the last administrator of a group")}
|
2019-08-26 13:44:02 +00:00
|
|
|
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_person(_parent, _args, _resolution) do
|
2020-10-02 07:52:47 +00:00
|
|
|
{:error, :unauthenticated}
|
2019-06-17 15:15:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp last_identity?(user) do
|
|
|
|
length(Users.get_actors_for_user(user)) <= 1
|
|
|
|
end
|
|
|
|
|
2019-05-22 12:12:11 +00:00
|
|
|
defp save_attached_pictures(args) do
|
2020-12-15 16:17:42 +00:00
|
|
|
with args when is_map(args) <- save_attached_picture(args, :avatar),
|
|
|
|
args when is_map(args) <- save_attached_picture(args, :banner) do
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_attached_picture(args, key) do
|
|
|
|
if Map.has_key?(args, key) && !is_nil(args[key][:media]) do
|
|
|
|
with media when is_map(media) <- save_picture(args[key][:media], key) do
|
|
|
|
Map.put(args, key, media)
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
2020-12-15 16:17:42 +00:00
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_picture(media, key) do
|
|
|
|
with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
|
|
|
|
Upload.store(media.file, type: key, description: media.alt) do
|
|
|
|
%{"name" => name, "url" => url, "mediaType" => content_type}
|
|
|
|
end
|
2019-05-22 12:12:11 +00:00
|
|
|
end
|
|
|
|
|
2019-01-29 10:02:32 +00: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 16:23:05 +00:00
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(args.email),
|
2020-06-27 17:12:45 +00:00
|
|
|
user_actor <- Users.get_actor_for_user(user),
|
|
|
|
no_actor <- is_nil(user_actor),
|
|
|
|
{:no_actor, true} <- {:no_actor, no_actor},
|
2020-11-17 18:14:55 +00:00
|
|
|
args <- Map.update(args, :preferred_username, "", &String.downcase/1),
|
2019-01-29 10:02:32 +00:00
|
|
|
args <- Map.put(args, :user_id, user.id),
|
2020-12-15 16:17:42 +00:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
|
2020-06-27 17:12:45 +00:00
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args, true) do
|
2019-01-29 10:02:32 +00:00
|
|
|
{:ok, new_person}
|
2019-01-21 14:08:22 +00:00
|
|
|
else
|
2020-12-15 16:17:42 +00:00
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
|
2019-01-29 10:02:32 +00:00
|
|
|
{:error, :user_not_found} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "No user with this email was found")}
|
2019-01-30 14:54:21 +00:00
|
|
|
|
2019-01-29 10:02:32 +00:00
|
|
|
{:no_actor, _} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "You already have a profile for this user")}
|
2019-01-30 14:54:21 +00:00
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = e} ->
|
|
|
|
{:error, e}
|
2019-01-21 14:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-21 19:23:42 +00:00
|
|
|
|
|
|
|
@doc """
|
2020-11-06 14:43:38 +00:00
|
|
|
Returns the participations, optionally restricted to an event
|
2019-03-21 19:23:42 +00:00
|
|
|
"""
|
2020-01-26 19:34:25 +00:00
|
|
|
def person_participations(
|
|
|
|
%Actor{id: actor_id},
|
|
|
|
%{event_id: event_id},
|
|
|
|
%{context: %{current_user: user}}
|
|
|
|
) do
|
2019-09-26 14:38:58 +00: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 17:13:21 +00:00
|
|
|
{:ok, %Page{elements: [participant], total: 1}}
|
2019-03-21 19:23:42 +00:00
|
|
|
else
|
2019-09-07 17:54:11 +00:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-09-26 14:38:58 +00:00
|
|
|
|
|
|
|
{:no_participant, _} ->
|
2020-06-11 17:13:21 +00:00
|
|
|
{:ok, %Page{elements: [], total: 0}}
|
2019-03-21 19:23:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 17:13:21 +00: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 19:23:42 +00:00
|
|
|
else
|
2020-06-11 17:13:21 +00:00
|
|
|
{:is_owned, false} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2020-02-18 07:57:00 +00: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} ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-03-21 19:23:42 +00:00
|
|
|
end
|
|
|
|
end
|
2019-05-28 08:51:02 +00:00
|
|
|
|
2020-06-11 17:13:21 +00: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}
|
|
|
|
|
|
|
|
_ ->
|
2020-09-29 07:53:48 +00:00
|
|
|
{:error, dgettext("errors", "User not found")}
|
2020-06-11 17:13:21 +00:00
|
|
|
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
|
|
|
|
|
2020-10-01 17:10:18 +00:00
|
|
|
def organized_events_for_person(_parent, _args, _resolution),
|
|
|
|
do: {:ok, %Page{elements: [], total: 0}}
|
|
|
|
|
2019-08-26 13:44:02 +00: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 01:16:37 +00:00
|
|
|
length(Actors.list_group_ids_where_last_administrator(actor_id)) > 0
|
2019-08-26 13:44:02 +00:00
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|