From e5ccdccbc7e826e32a328a4b6c70a844e5a11300 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 3 Apr 2022 22:31:18 +0200 Subject: [PATCH] Allow to get a group by it's ID Signed-off-by: Thomas Citharel --- js/src/graphql/group.ts | 9 +++++++++ lib/graphql/resolvers/group.ex | 30 +++++++++++++++++++++++++----- lib/graphql/schema/actors/group.ex | 7 +++++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/js/src/graphql/group.ts b/js/src/graphql/group.ts index fdc4dac9b..52ca14960 100644 --- a/js/src/graphql/group.ts +++ b/js/src/graphql/group.ts @@ -226,6 +226,15 @@ export const FETCH_GROUP = gql` ${RESOURCE_METADATA_BASIC_FIELDS_FRAGMENT} `; +export const FETCH_GROUP_BY_ID = gql` + query FetchGroupById($id: ID!) { + groupById(id: $name) { + ...GroupFullFields + } + } + ${GROUP_FIELDS_FRAGMENTS} +`; + export const GET_GROUP = gql` query GetGroup( $id: ID! diff --git a/lib/graphql/resolvers/group.ex b/lib/graphql/resolvers/group.ex index 65741f7a9..b40955a61 100644 --- a/lib/graphql/resolvers/group.ex +++ b/lib/graphql/resolvers/group.ex @@ -16,15 +16,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do require Logger + @doc """ + Find a group + """ @spec find_group( any, %{:preferred_username => binary, optional(any) => any}, Absinthe.Resolution.t() ) :: {:error, :group_not_found} | {:ok, Actor.t()} - @doc """ - Find a group - """ def find_group( parent, %{preferred_username: name} = args, @@ -45,7 +45,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do {:ok, %Actor{}} -> {:error, :group_not_found} - {:error, _err} -> + {:error, err} -> + Logger.debug("Unable to find group, #{inspect(err)}") {:error, :group_not_found} end end @@ -59,11 +60,30 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do {:ok, %Actor{}} -> {:error, :group_not_found} - {:error, _err} -> + {:error, err} -> + Logger.debug("Unable to find group, #{inspect(err)}") {:error, :group_not_found} end end + def find_group_by_id(_parent, %{id: id} = args, %{ + context: %{ + current_actor: %Actor{id: actor_id} + } + }) do + with %Actor{suspended: false, id: group_id} = group <- Actors.get_actor_with_preload(id), + true <- Actors.is_member?(actor_id, group_id) do + {:ok, group} + else + _ -> + {:error, :group_not_found} + end + end + + def find_group_by_id(_parent, _args, _resolution) do + {:error, :group_not_found} + end + @doc """ Get a group """ diff --git a/lib/graphql/schema/actors/group.ex b/lib/graphql/schema/actors/group.ex index 6efeb0b2a..1129de042 100644 --- a/lib/graphql/schema/actors/group.ex +++ b/lib/graphql/schema/actors/group.ex @@ -244,6 +244,13 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do resolve(&Group.find_group/3) end + + @desc "Get a group by its preferred username" + field :group_by_id, :group do + arg(:id, non_null(:id), description: "The group local ID") + + resolve(&Group.find_group_by_id/3) + end end object :group_mutations do