Allow to get a group by it's ID

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-04-03 22:31:18 +02:00
parent 84a5c6f893
commit e5ccdccbc7
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 41 additions and 5 deletions

View File

@ -226,6 +226,15 @@ export const FETCH_GROUP = gql`
${RESOURCE_METADATA_BASIC_FIELDS_FRAGMENT} ${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` export const GET_GROUP = gql`
query GetGroup( query GetGroup(
$id: ID! $id: ID!

View File

@ -16,15 +16,15 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
require Logger require Logger
@doc """
Find a group
"""
@spec find_group( @spec find_group(
any, any,
%{:preferred_username => binary, optional(any) => any}, %{:preferred_username => binary, optional(any) => any},
Absinthe.Resolution.t() Absinthe.Resolution.t()
) :: ) ::
{:error, :group_not_found} | {:ok, Actor.t()} {:error, :group_not_found} | {:ok, Actor.t()}
@doc """
Find a group
"""
def find_group( def find_group(
parent, parent,
%{preferred_username: name} = args, %{preferred_username: name} = args,
@ -45,7 +45,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:ok, %Actor{}} -> {:ok, %Actor{}} ->
{:error, :group_not_found} {:error, :group_not_found}
{:error, _err} -> {:error, err} ->
Logger.debug("Unable to find group, #{inspect(err)}")
{:error, :group_not_found} {:error, :group_not_found}
end end
end end
@ -59,11 +60,30 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:ok, %Actor{}} -> {:ok, %Actor{}} ->
{:error, :group_not_found} {:error, :group_not_found}
{:error, _err} -> {:error, err} ->
Logger.debug("Unable to find group, #{inspect(err)}")
{:error, :group_not_found} {:error, :group_not_found}
end end
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 """ @doc """
Get a group Get a group
""" """

View File

@ -244,6 +244,13 @@ defmodule Mobilizon.GraphQL.Schema.Actors.GroupType do
resolve(&Group.find_group/3) resolve(&Group.find_group/3)
end 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 end
object :group_mutations do object :group_mutations do