From 7fcaa4a15129a259522a7e56581e1f13b4f7aca6 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 3 Apr 2022 17:39:57 +0200 Subject: [PATCH] [GraphQL] Allow to get group by ID in person memberships Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/person.ex | 49 ++++++++++++++++++----------- lib/graphql/schema/actors/person.ex | 3 +- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/graphql/resolvers/person.ex b/lib/graphql/resolvers/person.ex index 31a296d6..060ae2d4 100644 --- a/lib/graphql/resolvers/person.ex +++ b/lib/graphql/resolvers/person.ex @@ -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 diff --git a/lib/graphql/schema/actors/person.ex b/lib/graphql/schema/actors/person.ex index 65fd6c54..a7d773c7 100644 --- a/lib/graphql/schema/actors/person.ex +++ b/lib/graphql/schema/actors/person.ex @@ -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,