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}
`;
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!

View file

@ -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
"""

View file

@ -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