forked from potsda.mn/mobilizon
[GraphQL] Allow to get group by ID in person memberships
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
parent
d2864a22d9
commit
7fcaa4a151
|
@ -358,11 +358,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
|
|||
Returns this person's group memberships
|
||||
"""
|
||||
@spec person_memberships(Actor.t(), map(), map()) :: {:ok, Page.t()} | {:error, String.t()}
|
||||
def person_memberships(%Actor{id: actor_id} = person, %{group: group}, %{
|
||||
def person_memberships(%Actor{id: actor_id} = person, args, %{
|
||||
context: %{current_user: %User{} = user}
|
||||
}) do
|
||||
if user_can_access_person_details?(person, user) do
|
||||
with {:group, %Actor{id: group_id}} <- {:group, Actors.get_actor_by_name(group, :Group)},
|
||||
with {:group, %Actor{id: group_id}} <- {:group, group_from_args(args)},
|
||||
{:ok, %Member{} = membership} <- Actors.get_member(actor_id, group_id) do
|
||||
{:ok,
|
||||
%Page{
|
||||
|
@ -373,6 +373,21 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
|
|||
{:error, :member_not_found} ->
|
||||
{:ok, %Page{total: 0, elements: []}}
|
||||
|
||||
{:group, :none} ->
|
||||
with {:can_get_memberships, true} <-
|
||||
{:can_get_memberships, user_can_access_person_details?(person, user)},
|
||||
memberships <-
|
||||
Actors.list_members_for_actor(
|
||||
person,
|
||||
Map.get(args, :page, 1),
|
||||
Map.get(args, :limit, 10)
|
||||
) do
|
||||
{:ok, memberships}
|
||||
else
|
||||
{:can_get_memberships, _} ->
|
||||
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
||||
end
|
||||
|
||||
{:group, nil} ->
|
||||
{:error, :group_not_found}
|
||||
end
|
||||
|
@ -381,23 +396,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
|
|||
end
|
||||
end
|
||||
|
||||
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
|
||||
{:ok, memberships}
|
||||
else
|
||||
{:can_get_memberships, _} ->
|
||||
{:error, dgettext("errors", "Profile is not owned by authenticated user")}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns this person's group follows
|
||||
"""
|
||||
|
@ -498,4 +496,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
|
|||
do: actor_user_id == user_id
|
||||
|
||||
defp user_can_access_person_details?(_, _), do: false
|
||||
|
||||
@spec group_from_args(map()) :: Actor.t() | nil
|
||||
defp group_from_args(%{group: group}) do
|
||||
Actors.get_actor_by_name(group, :Group)
|
||||
end
|
||||
|
||||
defp group_from_args(%{group_id: group_id}) do
|
||||
Actors.get_actor(group_id)
|
||||
end
|
||||
|
||||
defp group_from_args(_) do
|
||||
:none
|
||||
end
|
||||
end
|
||||
|
|
|
@ -88,11 +88,12 @@ defmodule Mobilizon.GraphQL.Schema.Actors.PersonType do
|
|||
resolve(&Person.person_participations/3)
|
||||
end
|
||||
|
||||
@desc "The list of group this person is member of"
|
||||
@desc "The list of groups this person is member of"
|
||||
field(:memberships, :paginated_member_list,
|
||||
description: "The list of group this person is member of"
|
||||
) do
|
||||
arg(:group, :string, description: "Filter by group federated username")
|
||||
arg(:group_id, :id, description: "Filter by group ID")
|
||||
|
||||
arg(:page, :integer,
|
||||
default_value: 1,
|
||||
|
|
Loading…
Reference in a new issue