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
|
|
|
|
|
2020-11-19 17:06:28 +01:00
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2021-03-05 11:23:17 +01:00
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2019-09-26 16:38:58 +02:00
|
|
|
alias Mobilizon.Events.Participant
|
2021-03-05 11:23:17 +01:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2019-09-09 00:52:49 +02:00
|
|
|
alias Mobilizon.Users.User
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Federation.ActivityPub
|
2020-11-26 11:41:13 +01:00
|
|
|
require Logger
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2020-12-15 17:17:42 +01:00
|
|
|
alias Mobilizon.Web.Upload
|
2020-01-28 19:18:33 +01:00
|
|
|
|
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),
|
2020-12-15 17:17:42 +01:00
|
|
|
true <- suspended == false or is_moderator(role) do
|
2019-10-04 18:28:25 +02:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
|
|
|
_ ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Person with ID %{id} not found", id: id)}
|
2019-10-04 18:28:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 17:17:42 +01:00
|
|
|
def get_person(_parent, _args, _resolution), do: {:error, :unauthorized}
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
@doc """
|
|
|
|
Find a person
|
|
|
|
"""
|
2020-10-02 09:52:47 +02: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 18:28:25 +02:00
|
|
|
ActivityPub.find_or_make_actor_from_nickname(preferred_username),
|
2020-12-15 17:17:42 +01:00
|
|
|
{:own, {:is_owned, _}} <- {:own, User.owns_actor(user, actor_id)} do
|
2019-05-28 10:51:02 +02:00
|
|
|
{:ok, actor}
|
|
|
|
else
|
2020-10-02 09:52:47 +02:00
|
|
|
{:own, nil} ->
|
|
|
|
{:error, :unauthorized}
|
|
|
|
|
2018-11-23 15:03:53 +01:00
|
|
|
_ ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error,
|
|
|
|
dgettext("errors", "Person with username %{username} not found",
|
|
|
|
username: preferred_username
|
|
|
|
)}
|
2018-11-23 15:03:53 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-02 09:52:47 +02:00
|
|
|
def fetch_person(_parent, _args, _resolution), do: {:error, :unauthenticated}
|
|
|
|
|
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
|
|
|
|
|
2020-10-02 09:52:47 +02:00
|
|
|
def list_persons(_parent, _args, %{
|
|
|
|
context: %{current_user: %User{role: role}}
|
|
|
|
})
|
|
|
|
when not is_moderator(role) do
|
|
|
|
{:error, :unauthorized}
|
|
|
|
end
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
def list_persons(_parent, _args, _resolution) do
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
2020-06-11 19:13:21 +02:00
|
|
|
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
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
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
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
2019-01-21 15:08:22 +01:00
|
|
|
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)
|
|
|
|
|
2020-11-17 19:14:55 +01:00
|
|
|
with args <- Map.update(args, :preferred_username, "", &String.downcase/1),
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, save_attached_pictures(args)},
|
2019-05-22 14:12:11 +02:00
|
|
|
{:ok, %Actor{} = new_person} <- Actors.new_person(args) do
|
2019-01-21 15:08:22 +01:00
|
|
|
{:ok, new_person}
|
2020-12-15 17:17:42 +01:00
|
|
|
else
|
2021-03-24 11:38:31 +01:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
2019-01-29 11:02:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-22 18:07:20 +01:00
|
|
|
def create_person(_parent, _args, _resolution) do
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
2019-02-22 18:07:20 +01:00
|
|
|
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
|
2020-11-26 11:41:13 +01:00
|
|
|
require Logger
|
2019-06-17 17:15:27 +02:00
|
|
|
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),
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, 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
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
|
2019-06-17 17:15:27 +02:00
|
|
|
{:find_actor, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile not found")}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-06-17 17:15:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_person(_parent, _args, _resolution) do
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
2019-06-17 17:15:27 +02:00
|
|
|
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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile not found")}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
|
|
|
{:last_identity, true} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Cannot remove the last identity of a user")}
|
2019-06-17 17:15:27 +02:00
|
|
|
|
2019-08-26 15:44:02 +02:00
|
|
|
{:last_admin, true} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Cannot remove the last administrator of a group")}
|
2019-08-26 15:44:02 +02:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
{:is_owned, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-06-17 17:15:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_person(_parent, _args, _resolution) do
|
2020-10-02 09:52:47 +02:00
|
|
|
{:error, :unauthenticated}
|
2019-06-17 17:15:27 +02:00
|
|
|
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
|
2020-12-15 17:17:42 +01: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 14:12:11 +02:00
|
|
|
end
|
2020-12-15 17:17:42 +01:00
|
|
|
else
|
|
|
|
args
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_picture(media, key) do
|
2021-04-09 10:43:34 +02:00
|
|
|
with {:ok, %{name: name, url: url, content_type: content_type, size: size}} <-
|
2020-12-15 17:17:42 +01:00
|
|
|
Upload.store(media.file, type: key, description: media.alt) do
|
2021-04-09 10:43:34 +02:00
|
|
|
%{"name" => name, "url" => url, "content_type" => content_type, "size" => size}
|
2020-12-15 17:17:42 +01:00
|
|
|
end
|
2019-05-22 14:12:11 +02:00
|
|
|
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
|
2021-03-25 10:22:40 +01:00
|
|
|
# When registering, email is assumed confirmed (unlike changing email)
|
|
|
|
with {:ok, %User{} = user} <- Users.get_user_by_email(args.email, unconfirmed: false),
|
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},
|
2020-11-17 19:14:55 +01:00
|
|
|
args <- Map.update(args, :preferred_username, "", &String.downcase/1),
|
2019-01-29 11:02:32 +01:00
|
|
|
args <- Map.put(args, :user_id, user.id),
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, args} when is_map(args) <- {:picture, 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
|
2020-12-15 17:17:42 +01:00
|
|
|
{:picture, {:error, :file_too_large}} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
|
2019-01-29 11:02:32 +01:00
|
|
|
{:error, :user_not_found} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "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, _} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "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 """
|
2020-11-06 15:43:38 +01:00
|
|
|
Returns the participations, optionally restricted to an 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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile 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
|
|
|
|
|
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} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile 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
|
|
|
|
"""
|
2021-03-05 11:23:17 +01:00
|
|
|
@spec person_memberships(Actor.t(), map(), map()) :: {:ok, Page.t()} | {:error, String.t()}
|
|
|
|
def person_memberships(%Actor{id: actor_id}, %{group: group}, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
|
|
|
with {:is_owned, %Actor{id: actor_id}} <- User.owns_actor(user, actor_id),
|
2021-03-29 10:33:19 +02:00
|
|
|
{:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(group, :Group)},
|
2021-03-05 11:23:17 +01:00
|
|
|
{:ok, %Member{} = membership} <- Actors.get_member(actor_id, group_id),
|
|
|
|
memberships <- %Page{
|
|
|
|
total: 1,
|
|
|
|
elements: [Repo.preload(membership, [:actor, :parent, :invited_by])]
|
|
|
|
} do
|
|
|
|
{:ok, memberships}
|
|
|
|
else
|
|
|
|
{:error, :member_not_found} ->
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
|
2021-03-29 10:33:19 +02:00
|
|
|
{:group, nil} ->
|
|
|
|
{:error, :group_not_found}
|
|
|
|
|
2021-03-05 11:23:17 +01:00
|
|
|
{:is_owned, nil} ->
|
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def person_memberships(%Actor{id: actor_id}, %{page: page, limit: limit}, %{
|
|
|
|
context: %{current_user: user}
|
|
|
|
}) do
|
2020-02-18 08:57:00 +01:00
|
|
|
with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id),
|
2021-03-05 11:23:17 +01:00
|
|
|
memberships <- Actors.list_members_for_actor(actor, page, limit) do
|
|
|
|
{:ok, memberships}
|
2020-02-18 08:57:00 +01:00
|
|
|
else
|
|
|
|
{:is_owned, nil} ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2019-03-21 20:23:42 +01:00
|
|
|
end
|
|
|
|
end
|
2019-05-28 10:51:02 +02:00
|
|
|
|
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}
|
|
|
|
|
|
|
|
_ ->
|
2020-09-29 09:53:48 +02:00
|
|
|
{:error, dgettext("errors", "User not found")}
|
2020-06-11 19:13:21 +02: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
|
2021-03-25 10:46:45 +01:00
|
|
|
with {:can_get_events, true} <-
|
|
|
|
{:can_get_events, actor_user_id == user_id or is_moderator(role)},
|
2020-06-11 19:13:21 +02:00
|
|
|
%Page{} = page <- Events.list_organized_events_for_actor(actor, page, limit) do
|
|
|
|
{:ok, page}
|
2021-03-25 10:46:45 +01:00
|
|
|
else
|
|
|
|
{:can_get_events, false} ->
|
|
|
|
{:error, :unauthorized}
|
2020-06-11 19:13:21 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-01 19:10:18 +02:00
|
|
|
def organized_events_for_person(_parent, _args, _resolution),
|
|
|
|
do: {:ok, %Page{elements: [], total: 0}}
|
|
|
|
|
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
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|