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}
|
2021-10-25 11:18:46 +00:00
|
|
|
alias Mobilizon.Actors.{Actor, Follower, Member}
|
2019-09-26 14:38:58 +00:00
|
|
|
alias Mobilizon.Events.Participant
|
2021-03-05 10:23:17 +00:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
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
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actions
|
2021-04-22 10:17:56 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
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
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec get_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t() | :unauthorized}
|
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
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec fetch_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t() | :unauthorized | :unauthenticated}
|
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} <-
|
2021-04-22 10:17:56 +00:00
|
|
|
ActivityPubActor.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}
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec list_persons(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Actor.t())} | {:error, :unauthorized | :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
|
|
|
|
"""
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec get_current_person(any, any, Absinthe.Resolution.t()) ::
|
2021-09-28 17:40:37 +00:00
|
|
|
{:error, :unauthenticated | :no_current_person} | {:ok, Actor.t()}
|
2021-09-10 09:35:32 +00:00
|
|
|
def get_current_person(_parent, _args, %{context: %{current_actor: %Actor{} = actor}}) do
|
|
|
|
{:ok, actor}
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_current_person(_parent, _args, %{context: %{current_user: %User{}}}) do
|
|
|
|
{:error, :no_current_person}
|
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
|
|
|
|
"""
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec identities(any, any, Absinthe.Resolution.t()) ::
|
|
|
|
{:error, :unauthenticated} | {:ok, list(Actor.t())}
|
2019-01-21 14:08:22 +00:00
|
|
|
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
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec create_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t() | :unauthenticated}
|
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
|
2021-03-24 10:38:31 +00:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
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
|
|
|
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
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec update_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t() | :unauthenticated}
|
2019-06-17 15:15:27 +00:00
|
|
|
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)
|
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
case owned_actor(user, id) do
|
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
case save_attached_pictures(args) do
|
|
|
|
args when is_map(args) ->
|
2021-09-28 17:40:37 +00:00
|
|
|
case Actions.Update.update(actor, args, true) do
|
2021-09-10 09:35:32 +00:00
|
|
|
{:ok, _activity, %Actor{} = actor} ->
|
|
|
|
{:ok, actor}
|
2020-12-15 16:17:42 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
2019-06-17 15:15:27 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
{:error, :file_too_large} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
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
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec delete_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t() | :unauthenticated}
|
2019-06-17 15:15:27 +00:00
|
|
|
def delete_person(
|
|
|
|
_parent,
|
2019-10-04 16:28:25 +00:00
|
|
|
%{id: id} = _args,
|
2021-09-10 09:35:32 +00:00
|
|
|
%{context: %{current_user: %User{} = user}} = _resolution
|
2019-06-17 15:15:27 +00:00
|
|
|
) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case owned_actor(user, id) do
|
|
|
|
{:ok, %Actor{} = actor} ->
|
|
|
|
if last_identity?(user) do
|
|
|
|
{:error, dgettext("errors", "Cannot remove the last identity of a user")}
|
|
|
|
else
|
|
|
|
if last_admin_of_a_group?(actor.id) do
|
|
|
|
{:error, dgettext("errors", "Cannot remove the last administrator of a group")}
|
|
|
|
else
|
|
|
|
Actors.delete_actor(actor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_person(_parent, _args, _resolution) do
|
|
|
|
{:error, :unauthenticated}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec owned_actor(User.t(), integer() | String.t()) :: {:error, String.t()} | {:ok, Actor.t()}
|
|
|
|
defp owned_actor(%User{} = user, actor_id) do
|
2019-06-17 15:15:27 +00:00
|
|
|
with {:find_actor, %Actor{} = actor} <-
|
2021-09-10 09:35:32 +00:00
|
|
|
{:find_actor, Actors.get_actor(actor_id)},
|
|
|
|
{:is_owned, %Actor{}} <- User.owns_actor(user, actor.id) do
|
2019-06-17 15:15:27 +00:00
|
|
|
{: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
|
|
|
|
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
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec last_identity?(User.t()) :: boolean
|
2019-06-17 15:15:27 +00:00
|
|
|
defp last_identity?(user) do
|
|
|
|
length(Users.get_actors_for_user(user)) <= 1
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec save_attached_pictures(map()) :: map() | {:error, any()}
|
2019-05-22 12:12:11 +00:00
|
|
|
defp save_attached_pictures(args) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case save_attached_picture(args, :avatar) do
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
|
|
|
args when is_map(args) ->
|
|
|
|
case save_attached_picture(args, :banner) do
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
|
|
|
args when is_map(args) ->
|
|
|
|
args
|
|
|
|
end
|
2020-12-15 16:17:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec save_attached_picture(map(), :avatar | :banner) :: map() | {:error, any}
|
2020-12-15 16:17:42 +00:00
|
|
|
defp save_attached_picture(args, key) do
|
|
|
|
if Map.has_key?(args, key) && !is_nil(args[key][:media]) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case save_picture(args[key][:media], key) do
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
|
|
|
|
|
|
|
media when is_map(media) ->
|
|
|
|
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
|
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec save_picture(map(), :avatar | :banner) :: {:ok, map()} | {:error, any()}
|
2020-12-15 16:17:42 +00:00
|
|
|
defp save_picture(media, key) do
|
2021-09-10 09:35:32 +00:00
|
|
|
case Upload.store(media.file, type: key, description: media.alt) do
|
|
|
|
{:ok, %{name: name, url: url, content_type: content_type, size: size}} ->
|
|
|
|
%{"name" => name, "url" => url, "content_type" => content_type, "size" => size}
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
{:error, err}
|
2020-12-15 16:17:42 +00:00
|
|
|
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)
|
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec register_person(any(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, String.t()}
|
2019-01-29 10:02:32 +00:00
|
|
|
def register_person(_parent, args, _resolution) do
|
2021-03-25 09:22:40 +00:00
|
|
|
# When registering, email is assumed confirmed (unlike changing email)
|
2021-09-10 09:35:32 +00:00
|
|
|
case Users.get_user_by_email(args.email, unconfirmed: false) do
|
|
|
|
{:ok, %User{} = user} ->
|
|
|
|
if is_nil(Users.get_actor_for_user(user)) do
|
|
|
|
# No profile yet, we can create one
|
|
|
|
case prepare_args(args, user) do
|
|
|
|
args when is_map(args) ->
|
|
|
|
Actors.new_person(args, true)
|
|
|
|
|
|
|
|
{:error, :file_too_large} ->
|
|
|
|
{:error, dgettext("errors", "The provided picture is too heavy")}
|
|
|
|
|
|
|
|
{:error, _err} ->
|
|
|
|
{:error, dgettext("errors", "Error while uploading pictures")}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "You already have a profile for this user")}
|
|
|
|
end
|
2020-12-15 16:17:42 +00:00
|
|
|
|
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-21 14:08:22 +00:00
|
|
|
end
|
|
|
|
end
|
2019-03-21 19:23:42 +00:00
|
|
|
|
2021-09-10 09:35:32 +00:00
|
|
|
@spec prepare_args(map(), User.t()) :: map() | {:error, any()}
|
|
|
|
defp prepare_args(args, %User{} = user) do
|
|
|
|
args
|
|
|
|
|> Map.update(:preferred_username, "", &String.downcase/1)
|
|
|
|
|> Map.put(:user_id, user.id)
|
|
|
|
|> save_attached_pictures()
|
|
|
|
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
|
|
|
"""
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec person_participations(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Participant.t())} | {:error, :unauthorized | String.t()}
|
2020-01-26 19:34:25 +00:00
|
|
|
def person_participations(
|
2021-05-12 16:15:28 +00:00
|
|
|
%Actor{id: actor_id} = person,
|
2020-01-26 19:34:25 +00:00
|
|
|
%{event_id: event_id},
|
2021-05-12 16:15:28 +00:00
|
|
|
%{context: %{current_user: %User{} = user}}
|
2020-01-26 19:34:25 +00:00
|
|
|
) do
|
2021-09-10 09:35:32 +00:00
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
case Events.get_participant(event_id, actor_id) do
|
|
|
|
{:ok, %Participant{} = participant} -> {:ok, %Page{elements: [participant], total: 1}}
|
|
|
|
{:error, :participant_not_found} -> {:ok, %Page{elements: [], total: 0}}
|
|
|
|
end
|
2019-03-21 19:23:42 +00:00
|
|
|
else
|
2021-09-10 09:35:32 +00:00
|
|
|
{:error, :unauthorized}
|
2019-03-21 19:23:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-12 16:15:28 +00:00
|
|
|
def person_participations(%Actor{} = person, %{page: page, limit: limit}, %{
|
|
|
|
context: %{current_user: %User{} = user}
|
2020-06-11 17:13:21 +00:00
|
|
|
}) do
|
2021-09-28 17:40:37 +00:00
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
%Page{} = page = Events.list_event_participations_for_actor(person, page, limit)
|
2020-06-11 17:13:21 +00:00
|
|
|
{:ok, page}
|
2019-03-21 19:23:42 +00:00
|
|
|
else
|
2021-09-28 17:40:37 +00:00
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2021-10-25 11:18:46 +00:00
|
|
|
Returns this person's group memberships
|
2020-02-18 07:57:00 +00:00
|
|
|
"""
|
2021-03-05 10:23:17 +00:00
|
|
|
@spec person_memberships(Actor.t(), map(), map()) :: {:ok, Page.t()} | {:error, String.t()}
|
2021-05-12 16:15:28 +00:00
|
|
|
def person_memberships(%Actor{id: actor_id} = person, %{group: group}, %{
|
|
|
|
context: %{current_user: %User{} = user}
|
2021-03-05 10:23:17 +00:00
|
|
|
}) do
|
2021-09-28 17:40:37 +00:00
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
with {:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(group, :Group)},
|
|
|
|
{:ok, %Member{} = membership} <- Actors.get_member(actor_id, group_id) do
|
|
|
|
{:ok,
|
|
|
|
%Page{
|
2021-03-05 10:23:17 +00:00
|
|
|
total: 1,
|
|
|
|
elements: [Repo.preload(membership, [:actor, :parent, :invited_by])]
|
2021-09-28 17:40:37 +00:00
|
|
|
}}
|
|
|
|
else
|
|
|
|
{:error, :member_not_found} ->
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
2021-03-29 08:33:19 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
{:group, nil} ->
|
|
|
|
{:error, :group_not_found}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
2021-03-05 10:23:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-12 16:15:28 +00:00
|
|
|
def person_memberships(
|
|
|
|
%Actor{} = person,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
with {:can_get_memberships, true} <-
|
|
|
|
{:can_get_memberships, user_can_access_person_details?(person, user)},
|
|
|
|
memberships <- Actors.list_members_for_actor(person, page, limit) do
|
2021-03-05 10:23:17 +00:00
|
|
|
{:ok, memberships}
|
2020-02-18 07:57:00 +00:00
|
|
|
else
|
2021-05-12 16:15:28 +00:00
|
|
|
{:can_get_memberships, _} ->
|
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
|
|
|
|
2021-10-25 11:18:46 +00:00
|
|
|
@doc """
|
|
|
|
Returns this person's group follows
|
|
|
|
"""
|
|
|
|
@spec person_follows(Actor.t(), map(), map()) :: {:ok, Page.t()} | {:error, String.t()}
|
2021-10-25 14:53:46 +00:00
|
|
|
def person_follows(%Actor{} = person, %{group: group}, %{
|
2021-10-25 11:18:46 +00:00
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}) do
|
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
with {:group, %Actor{} = group} <- {:group, Actors.get_actor_by_name(group, :Group)},
|
|
|
|
%Follower{} = follow <-
|
|
|
|
Actors.get_follower_by_followed_and_following(group, person) do
|
|
|
|
{:ok,
|
|
|
|
%Page{
|
|
|
|
total: 1,
|
|
|
|
elements: [follow]
|
|
|
|
}}
|
|
|
|
else
|
|
|
|
nil ->
|
|
|
|
{:ok, %Page{total: 0, elements: []}}
|
|
|
|
|
|
|
|
{:group, nil} ->
|
|
|
|
{:error, :group_not_found}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def person_follows(
|
|
|
|
%Actor{} = person,
|
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{
|
|
|
|
context: %{current_user: %User{} = user}
|
|
|
|
}
|
|
|
|
) do
|
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
follows = Actors.list_paginated_follows_for_actor(person, page, limit)
|
|
|
|
{:ok, follows}
|
|
|
|
else
|
|
|
|
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec user_for_person(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, User.t() | nil} | {:error, String.t() | nil}
|
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}
|
|
|
|
|
|
|
|
_ ->
|
2021-12-15 08:46:38 +00:00
|
|
|
{:error, :user_not_found}
|
2020-06-11 17:13:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_for_person(_, _args, _resolution), do: {:error, nil}
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec organized_events_for_person(Actor.t(), map(), Absinthe.Resolution.t()) ::
|
|
|
|
{:ok, Page.t(Event.t())} | {:error, :unauthorized}
|
2020-06-11 17:13:21 +00:00
|
|
|
def organized_events_for_person(
|
2021-05-12 16:15:28 +00:00
|
|
|
%Actor{} = person,
|
2020-06-11 17:13:21 +00:00
|
|
|
%{page: page, limit: limit},
|
|
|
|
%{
|
2021-05-12 16:15:28 +00:00
|
|
|
context: %{current_user: %User{} = user}
|
2020-06-11 17:13:21 +00:00
|
|
|
}
|
|
|
|
) do
|
2021-09-28 17:40:37 +00:00
|
|
|
if user_can_access_person_details?(person, user) do
|
|
|
|
%Page{} = page = Events.list_organized_events_for_actor(person, page, limit)
|
2020-06-11 17:13:21 +00:00
|
|
|
{:ok, page}
|
2021-03-25 09:46:45 +00:00
|
|
|
else
|
2021-09-28 17:40:37 +00:00
|
|
|
{:error, :unauthorized}
|
2020-06-11 17:13:21 +00:00
|
|
|
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
|
2021-05-12 16:15:28 +00:00
|
|
|
|
|
|
|
@spec user_can_access_person_details?(Actor.t(), User.t()) :: boolean()
|
|
|
|
defp user_can_access_person_details?(%Actor{}, %User{role: role}) when is_moderator(role),
|
|
|
|
do: true
|
|
|
|
|
|
|
|
defp user_can_access_person_details?(%Actor{type: :Person, user_id: actor_user_id}, %User{
|
|
|
|
id: user_id
|
|
|
|
})
|
|
|
|
when not is_nil(user_id),
|
|
|
|
do: actor_user_id == user_id
|
|
|
|
|
|
|
|
defp user_can_access_person_details?(_, _), do: false
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|