[GraphQL] Allow to get group by ID in person memberships

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-03 17:39:57 +02:00
parent d2864a22d9
commit 7fcaa4a151
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 32 additions and 20 deletions

View File

@ -358,11 +358,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
Returns this person's group memberships Returns this person's group memberships
""" """
@spec person_memberships(Actor.t(), map(), map()) :: {:ok, Page.t()} | {:error, String.t()} @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} context: %{current_user: %User{} = user}
}) do }) do
if user_can_access_person_details?(person, 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, %Member{} = membership} <- Actors.get_member(actor_id, group_id) do
{:ok, {:ok,
%Page{ %Page{
@ -373,6 +373,21 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
{:error, :member_not_found} -> {:error, :member_not_found} ->
{:ok, %Page{total: 0, elements: []}} {: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} -> {:group, nil} ->
{:error, :group_not_found} {:error, :group_not_found}
end end
@ -381,23 +396,6 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
end end
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 """ @doc """
Returns this person's group follows Returns this person's group follows
""" """
@ -498,4 +496,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
do: actor_user_id == user_id do: actor_user_id == user_id
defp user_can_access_person_details?(_, _), do: false 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 end

View File

@ -88,11 +88,12 @@ defmodule Mobilizon.GraphQL.Schema.Actors.PersonType do
resolve(&Person.person_participations/3) resolve(&Person.person_participations/3)
end 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, field(:memberships, :paginated_member_list,
description: "The list of group this person is member of" description: "The list of group this person is member of"
) do ) do
arg(:group, :string, description: "Filter by group federated username") arg(:group, :string, description: "Filter by group federated username")
arg(:group_id, :id, description: "Filter by group ID")
arg(:page, :integer, arg(:page, :integer,
default_value: 1, default_value: 1,